I have multiple Switches in a Xaml page and I am struggling to grasp the concept of MVVM when using Switches and handling State Changes etc. I am trying within a view model to capture the object and argument within the ViewModel which is available within the xaml.cs file.
A standard Switch Xaml from Microsoft on Switches.
<Switch Toggled="OnToggled" />
The same page Xaml.cs File
void OnToggled(object sender, ToggledEventArgs e)
{
// I have the sender so can target the switch now and change values colours etc.
}
Now it is this massive cough for me leap into Mvvm which I am failing to grasp.
I have followed every single guide/demo/tutorial on Mvvm but I can not figure out how doing the near same thing using Maui Community Toolkit and using a View Model I can cut out the Xaml.cs element of my code. As I believe the page should not be aware of the View vice versa.
My current code is as follows :- SettingsPage.xaml
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MyProject.Views.SettingsPage"
xmlns:mct="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
xmlns:vm="clr-namespace:MyProject.ViewModels"
x:DataType="vm:SettingsPageViewModel"
BackgroundColor="{DynamicResource PageBackGround}">
<ContentPage.Content>
<StackLayout>
<Switch IsToggled = "True"
<Switch.Behaviors>
<mct:EventToCommandBehavior
x:TypeArguments="x:Object"
EventName="Toggled"
Command="{Binding ToggledCommand}" />
</Switch.Behaviors>
</Switch>
</StackLayout>
</ContentPage.Content>
</ContentPage>
SettingsPage.xaml.cs
namespace MyProject.Views;
public partial class SettingsPage : ContentPage
{
ILogger<SettingsPage> _logger;
public SettingsPage(SettingsPageViewModel settingsPageViewModel, ILogger<SettingsPage> logger)
{
InitializeComponent();
BindingContext = settingsPageViewModel;
_logger = logger;
_logger.LogInformation("Navigation to Settings Page.");
}
}
SettingsPageViewModel.cs
namespace MyProject.ViewModels
{
public partial class SettingsPageViewModel : BaseViewModel
{
ILogger<SettingsPageViewModel> _logger;
LogController LogController;
public SettingsPageViewModel(ILogger<SettingsPageViewModel> logger)
{
_logger = logger;
LogController = new LogController();
}
// Here I want to capture the object so that I can have multiple switches on the page and be able to identify them to change values etc for what states they are in.
[RelayCommand]
public void Toggled(ToggledEventArgs e)
{
var bangingMyheadAgainstAWall = "e";
}
}
}
I have also tried without using the EventToCommandBehaviour and just used standard Toggled, Istoggled, OnToggled, IWishIknewWhichSwitchWasToggled but I just can not get the properties through to the view model or I have backed myself in to a corner and am a little confused.
Any help/ push in the correct direction to help me handle this situation correctly would be greatly appreciated. Thanks in Advance.