I have a slider on my Content Page and I want to access the value of it from an ObservableCollection (used in the content page) to check the time value I need to compare with. Below is the function I am using in the Observable Collection but the reference to the slider does not work...
public void OnTimedEvent(object? source, System.Timers.ElapsedEventArgs e)
{
var VM = App.Current?.Resources["AppViewModel"] as AppViewModel;
if (VM != null && CurrentTimeOn.Seconds > VM.sliderValue)
{
Labcolor = Color.FromRgb(255, 0, 0);
OnPropertyChanged(nameof(Labcolor));
}
}
Below is the XAML for the Content page....
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
x:Class="MauiSubHub.PressAndReleaseButtonPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:MauiSubHub"
Title="SubHub"
IconImageSource="appicon.png"
xmlns:viewmodel="clr-namespace:MauiSubHub"
x:DataType="local:AppViewModel"
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
xmlns:system="clr-namespace:System;assembly=netstandard">
<Grid RowDefinitions="Auto,Auto,Auto,*">
<Entry Placeholder="Enter player name" Text="{Binding NewName, Mode=TwoWay}" Grid.Row="1" MinimumWidthRequest="400" HorizontalOptions="Start"/>
<Button
x:Name="btnAddPlayer"
Grid.Row="2"
BackgroundColor="Orange"
HorizontalOptions="Start"
LineBreakMode="WordWrap"
Pressed="AddPlayer"
Text="Add Player"
VerticalOptions="End" />
<Grid Grid.Row="3" RowDefinitions="Auto,*">
<Slider x:Name="slider"
Maximum="90"
HorizontalOptions="Start"
WidthRequest="400"/>
<Label x:Name="displayLabel"
Padding="20"
Text="{Binding x:DataType='Slider',
Source={x:Reference slider},
Path=Value,
StringFormat='Alert when {0:F0} minutes on'}"
HorizontalOptions="Start"
VerticalOptions="End" />
<CollectionView
x:Name="MyCollectionViews"
Grid.Row="1"
ItemsSource="{Binding UserItems}"
SelectedItem="{Binding SelectedListItem, Mode=TwoWay}"
SelectionMode="Single"
SelectionChanged="OnSelectionVIewChanged"
>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup Name="CommonStates">
<VisualState Name="Normal"></VisualState>
<VisualState Name="Selected">
<VisualState.Setters>
<Setter Property="BackgroundColor" Value="Blue"></Setter>
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<CollectionView.ItemTemplate>
<DataTemplate x:DataType="local:UserItem">
<Grid Padding="10" BackgroundColor="White" ColumnDefinitions="Auto,200">
<BoxView Grid.Row="0" Grid.Column="0"
Color="LightSkyBlue" />
<BoxView Grid.Row="0" Grid.Column="1"
Color="LightSkyBlue" />
<BoxView Grid.Row="1" Grid.Column="0"
Color="LightSkyBlue" />
<BoxView Grid.Row="1" Grid.Column="1"
Color="LightSkyBlue" />
<Label x:Name="label1" Grid.Column="0" HorizontalOptions="Start" >
<Label.FormattedText>
<FormattedString >
<Span FontAttributes="Bold" Text="{Binding Name}" />
<Span Text="{x:Static system:Environment.NewLine}"/>
<Span Text="{Binding PositionOn,StringFormat='Total Time On: {0:mm\\mss\\s}'}" />
<Span Text="{x:Static system:Environment.NewLine}"/>
<Span Text="{Binding PositionOff,StringFormat='Total Time Off: {0:mm\\mss\\s}'}" />
<Span Text="{x:Static system:Environment.NewLine}"/>
<Span Text="{Binding CurrentTimeOn,StringFormat='Current Time On: {0:mm\\mss\\s}'}" BackgroundColor="{Binding Labcolor, Mode=TwoWay}" />
</FormattedString>
</Label.FormattedText>
<Label.GestureRecognizers>
<SwipeGestureRecognizer Command="{Binding Source={x:Reference MyCollectionViews}, Path=BindingContext.DeleteCommand}" CommandParameter="{Binding .}"
/>
<TapGestureRecognizer Command="{Binding Source={RelativeSource AncestorType={x:Type ContentPage}}, Path=BindingContext.DeleteCommand}" CommandParameter="{Binding .}"
/>
</Label.GestureRecognizers>
</Label>
<Button x:Name="btnStartStop" Grid.Column="1" BackgroundColor="Red" HorizontalOptions="End" MinimumWidthRequest="100"
TextColor="White"
Text="Off">
<Button.GestureRecognizers>
<TapGestureRecognizer Command="{Binding StartStopCommandCommand}" CommandParameter="{x:Reference btnStartStop}" />
</Button.GestureRecognizers>
</Button>
</Grid>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</Grid>
</Grid>
</ContentPage>
OnTimedEventis a handler in your code behind, you should be able to reference the slider by name