0

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>
3
  • Well, seems you know Binding but you use it by rolling a dice. Even the way you get the ViewModel of the page is very uncommon. Commented Jun 1 at 5:55
  • if OnTimedEvent is a handler in your code behind, you should be able to reference the slider by name Commented Jun 1 at 15:22
  • The OnTimedEvent is in the view model as I need to check the value against each of my items in the collection. How do I reference a slider in the main page in the viewmodel? Commented Jun 1 at 20:01

1 Answer 1

1

You could use the Value property to do this

<Slider
Value = "{Binding SliderValue}"
....
>

And then in your ViewModel:

[ObservableProperty]
public partial double SliderValue { get; set; }

And then in your ViewModel, configure the DragCompletedCommand of the slider.

Sign up to request clarification or add additional context in comments.

2 Comments

You should switch to the partial property syntax, e.g. public partial double SliderValue { get; set; } = 0.0;. See devblogs.microsoft.com/dotnet/…🎉
@StephenQuan Hey man, thank,s I will definitely check that :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.