问题
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