Home > Enterprise >  I want to make my Field (Switch) Static/Global, so that I can access it from MainPage.xaml.cs
I want to make my Field (Switch) Static/Global, so that I can access it from MainPage.xaml.cs

Time:01-05

So what I have now is this

<Switch Grid.Row="1"                
        Grid.Column="1"
        Grid.ColumnSpan="1"
        Margin="{OnIdiom Phone='0, 0, 0, 0', Tablet='0, 0, 0, 155'}"
        x:Name="notificationSwitch"
        Toggled="notificationSwitch_Toggled"
        IsToggled="{Binding State}"/>

Basically what I want to do is, I want to Run a Task with the Name (x:Name) of this Field "notificationSwitch" in another Class (MainPage.cs) but now I dont have access to it... What can I do?

I browsed a lot in the internet but unfortunately found nothing to answer my specific example

CodePudding user response:

You can declare a static Switch in your page. Such as:

public partial class Page1 : ContentPage
    {
        public static Switch mySwitch;
        public Page1()
        {
            InitializeComponent();
            mySwitch = notificationSwitch;
        }

    }

And you can access to the notificationSwitch in the MainPage such as:

Page1.mySwitch.IsToggled = true;
  • Related