Understanding the life cycle of BindingContext in Xamarin?

删除回忆录丶 提交于 2019-12-24 08:43:30

问题


I have a basic registration page when the user register her data, I save an object of this data in a static field of the Settings static class , so I can fill the common fields with other registration pages, like first_name, last_name, email...etc. so the user doesn't have to re-fill them again.

the very specific case I have is, one of these common pages is the basic registration page itself (with some fields excluded). first that's how the common fields look like:

<Entry Text="{Binding RegistrationData.first_name, Mode=TwoWay}"  Grid.Row="1" Grid.Column="0" Placeholder="{Resx:TranslateExtension Text=lbFirstName}"/>
<Entry Text="{Binding RegistrationData.last_name, Mode=TwoWay}"  Grid.Row="2" Grid.Column="0" Placeholder="{Resx:TranslateExtension Text=lbLastName}"/>
<Entry Text="{Binding RegistrationData.email, Mode=TwoWay}" Keyboard="Email"  Grid.Row="3" Grid.Column="0" Placeholder="{Resx:TranslateExtension Text=lbEmail}"/>

apparently the model implements INotifyPropertyChanged.

in the constructor of the Register page:

 public Register(int userType) //when I call this constructor the registration page represent some sub-registration pages
        {
            InitializeComponent();
            var oRegistrationViewModel = (RegistrationViewModel)BindingContext;
            if (Settings.UserData != null)
            {
                oRegistrationViewModel.RegistrationData = Settings.UserData;
                BindingContext = oRegistrationViewModel;
            }

but it doesn't work, when I reset the BindingContext the data is not bound to the fields.

I set the Settings.UserData in the OnRegister method of the RegisterCommand in the view-model, when called from the basic page (with default constructor)

I'm using GalaSoft.MvvmLight I set the BindingContext in XAML:

BindingContext="{Binding Register, Source={StaticResource Locator}}"

the Locator is a key of the ViewModelLocator, in the constructor:

    SimpleIoc.Default.Register<RegistrationViewModel>();

and this property:

public RegistrationViewModel Register
    {
        get
        {
            return SimpleIoc.Default.GetInstance<RegistrationViewModel>();
        }
    }

I need to know when the BindingContext is set with binding in XAML, Is this happen when calling InitializeComponent(), when I called it after resetting the BindingContext the data still not shown.

来源:https://stackoverflow.com/questions/47750059/understanding-the-life-cycle-of-bindingcontext-in-xamarin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!