Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 58056

Label binding only works the first time I set it (?!)

$
0
0

Feels like every trivial task I try to accomplish with Xamarin lately turns out to be a huge time sink. I'm binding to a Label and I'm able to update it through the binding but only the first time. All subsequent sets do trigger the setter and the property changed handler but the Label isn't picking it up. The steps I'm doing are:

  1. Create INotifyPropertyChanged model class
  2. Create Label and SetBinding to the Status property of model class
  3. Set BindingContext = LoginStatus (model class)
  4. In LoginComplete event in AppDelegate I'm setting the Status property as initialization happens.

And here is my code:

public class LoginStatus : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    private string _status;
    public string Status {
        get { return _status; }
        set {
            if (value == null || value.Equals (_status, StringComparison.Ordinal)) {
                // Nothing to do - the value hasn't changed;
                return;
            }
            _status = value;
            OnPropertyChanged ();
        }
    }
    public void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        var handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

    private Label _statusLabel;

    public LoginPage ()
    {
        BackgroundColor = Color.White;
        LoginStatus = new LoginStatus ();

        var logo = new Image
        {
            Source = CustomResources.LogoColourSmall,
            VerticalOptions = LayoutOptions.Center
        };

        var fbLoginButton = new FacebookLoginButton { HorizontalOptions = LayoutOptions.Center };
        fbLoginButton.LoginComplete += (object sender, IFacebookProvider fbProvider) => 
        {
            this.LoginComplete(this, fbProvider);
        };

        _activityIndicator = new ActivityIndicator ();
        _statusLabel = new Label { TextColor = Color.Black, HorizontalOptions = LayoutOptions.Center };
        _statusLabel.SetBinding (Label.TextProperty, "Status");

        var layout = new StackLayout 
        {
            VerticalOptions = LayoutOptions.Center,
            Spacing = 10,
            Children = 
            { 
                logo, 
                fbLoginButton,
                _activityIndicator,
                _statusLabel
            }
        };

        BindingContext = LoginStatus;
        Content = layout;

        // Accomodate iPhone status bar.
        this.Padding = new Thickness(10, Device.OnPlatform(20, 0, 0), 10, 5);
    }

From AppDelegate DidFinishLaunching:

            var loginPage = App.GetLoginPage ();
            loginPage.LoginComplete += async (object sender, IFacebookProvider fbProvider) => 
            {
                loginPage.StartLoading();

                App.InitFacebook(fbProvider);
                loginPage.LoginStatus.Status = "Authenticating...";
                await App.ZumoService.Authenticate (MobileServiceAuthenticationProvider.Facebook);
                loginPage.LoginStatus.Status = "Syncing with the cloud...";
                await App.ZumoService.InitializeStoreAsync (); 
                loginPage.LoginStatus.Status = "Loading profile...";
                await App.ZumoService.CreateOrRetrieveAppUser ();
                Window.RootViewController = App.GetMainPage ().CreateViewController ();
                Window.MakeKeyAndVisible ();

                loginPage.StopLoading();
            };

Viewing all articles
Browse latest Browse all 58056

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>