PopAsync - refresh page strings on navigating back

Hi,
I have a settings page on my app that has change language option. How can I reload the strings on the previous page when I change the language and navigate back to it with PopAsync() function?

Tagged:

Posts

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    Messagingcenter???

  • JahnOttoAndersenJahnOttoAndersen NOMember ✭✭

    You can override your page's OnAppearing() method. This will be called when the other page is popped away.

  • HristoEnevHristoEnev USMember

    Okay, that was helpful. I will probably use MessagingCenter, but what is the best approach to reload the content. Do I have to write all the containers and layouts and add them to the main Content property? Or I can just reload somehow the page itself?

  • AlisonFernandesAlisonFernandes USMember ✭✭

    The best approach is to use bindable properties / observable collections, common in MVVM pattern. Then you just need to update your ViewModels data, which will update the view.

    Other than that, you can force it by overriding the view OnAppearing() and manually changing the info you want, without recreating the view itself.

  • HristoEnevHristoEnev USMember

    @AlisonFernandes could you point me to example with bindable properties without xaml? Thank you.

  • AlisonFernandesAlisonFernandes USMember ✭✭
    edited May 2015
    public class FooPage : ContentPage
    {
        public FooPage()
        {
            BindingContext = new MyViewModel();
            var mainStack = new StackLayout();
    
            var textEntry = new Entry()
            {
                Placeholder = "some text"
            };
            textEntry.SetBinding<MyViewModel>(Entry.TextProperty, model => model.SomeTextProperty, BindingMode.TwoWay );
    
            var myButton = new Button()
            {
                Text = "Login"
            };
            myButton.SetBinding<MyViewModel>(Button.CommandProperty, model => model.BarCommand);
    
            mainStack.Children.Add(textEntry);
            mainStack.Children.Add(myButton);
            Content = mainStack;
        }
    }
    
  • HristoEnevHristoEnev USMember
    edited May 2015
    BindingContext = this;
    locationLabel.SetBinding<MainPage> (Label.TextProperty, model => model.LocationLabel);
    LocationLabel = "Location";
    

    @AlisonFernandes This does not set my label text. The code is located in my MainPage class.

  • AlisonFernandesAlisonFernandes USMember ✭✭

    @HristoEnev you're snippet is too short and lacks some useful information but, from what I can see, you're making a circular reference that doesn't implement all of the required functionality, more specifically, the bindable properties. Provided your LocationLabel is a String property and your locationLabel is a Label (please, for the anyone involved insanity sake, improve your variables names :smile: ), your LocationLabel should implement the INotifyPropertyChanged functionality, so it has a way to inform the binding system something has changed. This will require you to implement INotifyPropertyChanged or use a framework like MvvmLight or MvvmCross that already does that, thus, you shouldn't bind your View to itself, the View and ViewModel should be decoupled.

    You can read more about data binding here, just skip the XAML part, you should implement something similar to the C# code in there.

  • HristoEnevHristoEnev USMember

    Thank you very much for your help @AlisonFernandes . You were very helpful! Much appreciated :smile:

  • HristoEnevHristoEnev USMember

    Thank you very much for your help @AlisonFernandes . You were very helpful! Much appreciated :smile:

  • TinPTBTinPTB USMember

    Thank you very much for your help @AlisonFernandes .

  • yousufctecyousufctec USMember ✭✭✭

    You can pass your current page's view model to your new pop up screen through constructor and just change the view model which will help you reflect in the previous page and you can just close your pop up using PopAsync.

    Thanks!
    Yousuf.

Sign In or Register to comment.