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?
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?
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.
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;
}
}
@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 ), 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.
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.
Posts
Messagingcenter???
You can override your page's OnAppearing() method. This will be called when the other page is popped away.
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?
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.@AlisonFernandes could you point me to example with bindable properties without xaml? Thank you.
@AlisonFernandes This does not set my label text. The code is located in my MainPage class.
@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
), your
LocationLabel
is aString
property and yourlocationLabel
is aLabel
(please, for the anyone involved insanity sake, improve your variables namesLocationLabel
should implement theINotifyPropertyChanged
functionality, so it has a way to inform the binding system something has changed. This will require you to implementINotifyPropertyChanged
or use a framework likeMvvmLight
orMvvmCross
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.
Thank you very much for your help @AlisonFernandes . You were very helpful! Much appreciated
Thank you very much for your help @AlisonFernandes . You were very helpful! Much appreciated
Thank you very much for your help @AlisonFernandes .
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.