Forum Xamarin.Forms

How can I use binding with static property ?

I am using this code but its not working :

IsVisible="{x:Static local:GlobalResources.IsUserLoggedIn}"

This is the class :

public class GlobalResources { public static bool IsUserLoggedIn { get; set; } }

Best Answer


  • fdesbrossesfdesbrosses USMember ✭✭

    What do you mean "not working" ? Not updating when the value changes ? If so, it's normal cause you're not raising PropertyChanged event (GlobalResources should then implementINotifyPropertyChanged interface).

  • mostafamarjimostafamarji USMember

    Yes I am trying to update the value , and I also try to use NotifyPropertyChanged interface but it also doesn't work :

    `public class GlobalResources:INotifyPropertyChanged

        public static bool _isUserLoggedIn { get; set; }
        public  bool IsUserLoggedIn
            get { return _isUserLoggedIn; }
                _isUserLoggedIn = value;
        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string propertyName)
            if (PropertyChanged == null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
  • KrishTalaKrishTala INMember ✭✭

    Yes, its working.
    Thanks for saving my time.

  • dengerelidengereli TRMember ✭✭

    all are you really sure it is working but is it singleton?

    public static GlobalResources Current = new GlobalResources();

    Current will be produce every called. Please change this below.

    private static GlobalResources _current;
    public static GlobalResources Current => _current ?? (_current = new GlobalResources ());

  • DeepakDYDeepakDY INMember ✭✭✭


    its working
    thanks for help

  • This doesn't work for me. Can anyone elaborate why it doesn't work? I always get something like this:

    Xamarin.Forms.Xaml.XamlParseException has been thrown
    Position 17:35. Cannot assign property "BroadcasterID": Property does not exist, or is not assignable, or mismatching type between value and property

    Where BroadcasterID is the thing I'm trying to bind; IsVisible=... in the example above.

  • DirkWilhelmDirkWilhelm USMember ✭✭✭✭
    edited September 2019

    Without seeing your code it is hard to tell.

    Just by looking at your naming it seems to me that "BroadcasterID" is an int? If yes then binding to IsVisible cannot work. IsVisible needs a bool.

    But again, without seeing your code, its hard to tell.

  • I'm not binding to IsVisible; I was using that as a reference to what changed from the example code. It is, strangely, a string, and it is being bound to a label's text. This works with just a boring x:Static binding if the property itself is static, but then it never updates. If I rework it like the example, the Xaml parser complains. Apparently there's another syntax using parens that is supposed to do this properly, but that only works in WPF. I don't understand how this ecosystem is so disjointed...

Sign In or Register to comment.