user1943020
user1943020

Reputation:

How can I make a change to a static variable make a change on a Xamarin.Form?

I have a Xamarin forms page and on the page there is a static variable:

namespace City
{
    public static class MS
    {
        public static int secs;
    }
}

My XAML

<Label x:Name="secondsLabel" />

My C# code updates like this

while ( ) {
    // the code updates the value of secs here in the loop
    MS.secs++;
    secondsLabel.Text = MS.secs.ToString();
}

But the value doesn't change on the screen. Is there a way that I can bind to this static integer so that when secs is changed by the C# code then the screen will automatically be updated also?

Upvotes: 0

Views: 816

Answers (1)

Jason
Jason

Reputation: 89129

<Label x:Name="secondsLabel" Text="{Binding Source={x:Static local:MS.Secs}}" />

you can only bind to public properties, so you will need a property in your code behind or VM

public static int Secs {
  get {
    return secs;
  }
  set {
    secs = value;
    PropertyChanged();
  }
}

Upvotes: 1

Related Questions