-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustomSwitch.cs
55 lines (46 loc) · 1.69 KB
/
CustomSwitch.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using System;
using Xamarin.Forms;
namespace CustomSwitch
{
public class CustomToggledEventArgs : EventArgs
{
public CustomToggledEventArgs(bool value, bool isuser)
{
Value = value;
IsUser = isuser;
}
public bool Value { get; private set; }
public bool IsUser { get; private set; }
}
public class CustomSwitch : Switch
{
private bool isUser { get; set; } = true;
public bool IsCustomToggled
{
get => IsToggled;
set
{
//The Order of Code is Very Important, We are removing Event Handler from Original Switch
this.Toggled -= Handle_Toggled;
//Setting IsToggled with IsCustomToggled
IsToggled = value;
//Invoking Custom Event with is User Property
CustomToggled?.Invoke(this, new CustomToggledEventArgs(IsToggled, false));
//Enabling Event again
this.Toggled += Handle_Toggled;
}
}
public static readonly BindableProperty IsCustomToggledProperty = BindableProperty.Create("IsCustomToggled", typeof(bool), typeof(CustomSwitch), false, BindingMode.TwoWay);
public event EventHandler<CustomToggledEventArgs> CustomToggled;
public CustomSwitch()
{
//Subscribing to the original event
this.Toggled += Handle_Toggled;
}
private void Handle_Toggled(object sender, ToggledEventArgs e)
{
//This even only fires when user is changing switch
CustomToggled?.Invoke(this, new CustomToggledEventArgs(IsToggled, true));
}
}
}