You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I came to use your customized version of the ToggleSwitch because the standard .NET MAUI Switch was not meeting my needs. Your drawn-control version seemed easy enough to modify to fit my needs, and indeed it is.
In my project, any time the user attempts to toggle a Switch/ToggleSwitch control, I'd like to display an alert to the user to verify if they actually want to take this action. Ideally, it would be nice to intercept the user before actually toggling the IsOn value on the switch. So, a basic flow would look something like this:
User initiates interaction with the ToggleSwitch by clicking it. This calls the OnToggleSwitchStartInteraction function in the ToggleSwitch class.
Before doing anything else, the ToggleSwitch raises an event that can be handled in user code, indicating that the toggle switch has received an interaction.
The user code sees this event and is allowed to respond to it. In my specific scenario, I display an alert to the user and ask the user if they actually want to toggle the switch.
The user code returns a value to the OnToggleSwitchStartInteraction function indicating whether the interaction will be allowed to proceed or whether it should be cancelled.
If allowed to proceed, then OnToggleSwitchStartInteraction proceeds as normal.
I have already implemented this in my own code, and I have it working. It looks like this:
I first define some event arguments we will use:
public class StartInteractionEventArgs : EventArgs
{
public bool AllowInteractionToContinue { get; set; } = true;
public StartInteractionEventArgs()
{
//empty
}
}
In ToggleSwitch, I have defined the following delegate:
public delegate Task AsyncEventHandler(object sender, StartInteractionEventArgs e);
public AsyncEventHandler UserHandleStartInteraction;
In ToggleSwitch, I change OnToggleSwitchStartInteraction to be async, and I redefine the code to look like this:
async void OnToggleSwitchStartInteraction(object sender, TouchEventArgs e)
{
if (IsEnabled)
{
StartInteractionEventArgs should_allow_interaction_event_args = new StartInteractionEventArgs();
if (UserHandleStartInteraction != null)
{
await UserHandleStartInteraction(this, should_allow_interaction_event_args);
}
if (should_allow_interaction_event_args.AllowInteractionToContinue)
{
IsOn = !IsOn;
}
}
}
In the code-behind, I subscribe to the UserHandleStartInteraction event:
public MainPage()
{
InitializeComponent();
BindingContext = new MainPageViewModel();
MySwitch.UserHandleStartInteraction += async (s, e) =>
{
await MySwitch_UserHandleStartInteraction(s, e);
};
}
private async Task MySwitch_UserHandleStartInteraction(object sender, SwitchTest.StartInteractionEventArgs e)
{
var result = await DisplayAlert("Are you sure?", string.Empty, "Yes", "No");
e.AllowInteractionToContinue = result;
}
And that's it.
While this may not be the best way to implement this feature, it gets the idea across. It allows the application to intercept a user's interaction and determine whether or not to allow it to proceed. I think this could be a valuable feature moving forward.
The text was updated successfully, but these errors were encountered:
I came to use your customized version of the ToggleSwitch because the standard .NET MAUI Switch was not meeting my needs. Your drawn-control version seemed easy enough to modify to fit my needs, and indeed it is.
In my project, any time the user attempts to toggle a Switch/ToggleSwitch control, I'd like to display an alert to the user to verify if they actually want to take this action. Ideally, it would be nice to intercept the user before actually toggling the
IsOn
value on the switch. So, a basic flow would look something like this:OnToggleSwitchStartInteraction
function in the ToggleSwitch class.OnToggleSwitchStartInteraction
function indicating whether the interaction will be allowed to proceed or whether it should be cancelled.OnToggleSwitchStartInteraction
proceeds as normal.I have already implemented this in my own code, and I have it working. It looks like this:
OnToggleSwitchStartInteraction
to beasync
, and I redefine the code to look like this:Note: this code also includes my fixes from #57
This is all the code that would need to be added to ToggleSwitch. Everything else would be in the user's application.
In my specific application, I use it like this:
UserHandleStartInteraction
event:And that's it.
While this may not be the best way to implement this feature, it gets the idea across. It allows the application to intercept a user's interaction and determine whether or not to allow it to proceed. I think this could be a valuable feature moving forward.
The text was updated successfully, but these errors were encountered: