Reputation: 2783
I am writing a Xamarin forms application which checks whether or not a user is in a specific geo-location. When a user walks inside an area, a XAML
switch is toggled from the off to on position.
farmSwitch.toggled = true;
This calls this function
farmSwitch.Toggled += (object sender, ToggledEventArgs e) => {
manualOnFarm = true;
Console.WriteLine("Switch.Toggled event sent");
changeOnFarmStatus(e);
};
I also need functionality where a user manually clicks the switch, which need to be differentiated from when the code automatically toggles farmSwitch
The reason is that I have a boolean that should only be true when the user manually clicks farmSwitch
, and should be false when the switch is toggled automatically
What are my options? Ideally there would be a farSwtich.clicked() function I could call for when a user manually clicks farmSwitch
, but no such luck
Upvotes: 3
Views: 3826
Reputation: 2326
Use @digitalsa1nt 's suggestion or, use "-=" the method before you switch it manually and use "+=" again.
Upvotes: 2
Reputation: 3276
I don't see how you can't just differentiate between this by using some kind of marker in the code you've written that programatically toggles the switch?
So you call this programatically but you also set a boolean marker to say this is a programatic change:
private bool HasBeenProgrammaticallyToggled = false;
public void ThisIsAProgrammaticToggle()
{
HasBeenProgrammaticallyToggled = true;
farmSwitch.toggled = true;
}
and in your little on toggled event just do:
farmSwitch.Toggled += (object sender, ToggledEventArgs e) => {
if(HasBeenProgrammaticallyToggled)
{
//This has been toggled programmatically, so reset our bool
HasBeenProgrammaticallyToggled = false;
}
else
{
// I am assuming this is what you use to determine a manual toggle?
manualOnFarm = true;
}
Console.WriteLine("Switch.Toggled event sent");
changeOnFarmStatus(e);
};
Wouldn't this work?
Upvotes: 3