无法绑定到 Bindable 属性 [Prism Application Xamarin Forms]
Failing to bind to Bindable Property [Prism Application Xamarin Forms]
我有一个自定义控件,想通过更改 ViewModel 的绑定值来操纵它。
我的代码结构如下所示。问题是,每当我更改 ViewModel 中的值时,自定义控件 BindableProperty 中都不会触发任何更改。
我哪里错了。
Custom Control (StepWizardControl.cs):
public class StepWizardControl : StackLayout
{
public static readonly BindableProperty ActiveStepNumberProperty = BindableProperty.Create(nameof(ActiveStep), typeof(int), typeof(StepWizardControl), 1, defaultBindingMode: BindingMode.TwoWay);
public int ActiveStep
{
get => (int)GetValue(ActiveStepNumberProperty);
set => SetValue(ActiveStepNumberProperty, value);
}
public StepWizardControl()
{
PropertyChanged += StepWizardControl_PropertyChanged;
}
private void StepWizardControl_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (e.PropertyName.Equals(nameof(ActiveStep)))
{
switch (ActiveStep)
{
case 1:
DoStep1();
break;
case 2:
DoStep2();
break;
default:
DoSomethingElse();
}
}
}
}
StepWizardPage.xaml (page in which control is used):
<?xml version="1.0" encoding="UTF-8" ?>
<ContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:prism="http://prismlibrary.com"
prism:ViewModelLocator.AutowireViewModel="True"
xmlns:controls="StepWizardControlNameSpace"
x:Class="MyClass">
<ContentPage.Content>
<StackLayout>
<controls:StepWizardControl ActiveStep="{Binding CurrentStep, Mode=TwoWay}"/>
<Button Text="Go Forward" Command="{Binding ToggleStepsCommand}"/>
</StackLayout>
</ContentPage.Content>
</ContentPage>
StepWizardPageViewModel:
public class StepWizardPageViewModel : PrismBaseViewModel, INavigatedAware
{
public int CurrentStep { get; set; }
public DelegateCommand ToggleStepsCommand { get; }
public StepWizardPageViewModel(INavigationService navigationService) : base(navigationService)
{
ToggleStepsCommand = new DelegateCommand(async () => { await OnToggleStepsCommand(); });
}
private async Task OnToggleStepsCommand()
{
CurrentStep++; //change the control UI to show current step
}
}
- 自定义控件中定义的
BindableProperty
的名称应该对应 属性 ,在你的代码中将 ActiveStepNumberProperty
更改为 ActiveStepProperty
public static readonly BindableProperty ActiveStepProperty =
BindableProperty.Create(
nameof(ActiveStep),
typeof(int),
typeof(StepWizardControl),
1,
BindingMode.TwoWay
);
public int ActiveStep
{
get => (int)GetValue(ActiveStepProperty);
set => SetValue(ActiveStepProperty, value);
}
- 它应该通知 viewmodel 的更改,然后
BindableProperty
将在之后更改。
public class StepWizardPageViewModel :BaseViewModel
{
private int currentStep;
public int CurrentStep {
get {
return currentStep;
}
set {
currentStep = value;
NotifyPropertyChanged(); //add the notify method defined in PrismBaseViewModel
}
}
//xxx
}
我有一个自定义控件,想通过更改 ViewModel 的绑定值来操纵它。
我的代码结构如下所示。问题是,每当我更改 ViewModel 中的值时,自定义控件 BindableProperty 中都不会触发任何更改。
我哪里错了。
Custom Control (StepWizardControl.cs):
public class StepWizardControl : StackLayout
{
public static readonly BindableProperty ActiveStepNumberProperty = BindableProperty.Create(nameof(ActiveStep), typeof(int), typeof(StepWizardControl), 1, defaultBindingMode: BindingMode.TwoWay);
public int ActiveStep
{
get => (int)GetValue(ActiveStepNumberProperty);
set => SetValue(ActiveStepNumberProperty, value);
}
public StepWizardControl()
{
PropertyChanged += StepWizardControl_PropertyChanged;
}
private void StepWizardControl_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (e.PropertyName.Equals(nameof(ActiveStep)))
{
switch (ActiveStep)
{
case 1:
DoStep1();
break;
case 2:
DoStep2();
break;
default:
DoSomethingElse();
}
}
}
}
StepWizardPage.xaml (page in which control is used):
<?xml version="1.0" encoding="UTF-8" ?>
<ContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:prism="http://prismlibrary.com"
prism:ViewModelLocator.AutowireViewModel="True"
xmlns:controls="StepWizardControlNameSpace"
x:Class="MyClass">
<ContentPage.Content>
<StackLayout>
<controls:StepWizardControl ActiveStep="{Binding CurrentStep, Mode=TwoWay}"/>
<Button Text="Go Forward" Command="{Binding ToggleStepsCommand}"/>
</StackLayout>
</ContentPage.Content>
</ContentPage>
StepWizardPageViewModel:
public class StepWizardPageViewModel : PrismBaseViewModel, INavigatedAware
{
public int CurrentStep { get; set; }
public DelegateCommand ToggleStepsCommand { get; }
public StepWizardPageViewModel(INavigationService navigationService) : base(navigationService)
{
ToggleStepsCommand = new DelegateCommand(async () => { await OnToggleStepsCommand(); });
}
private async Task OnToggleStepsCommand()
{
CurrentStep++; //change the control UI to show current step
}
}
- 自定义控件中定义的
BindableProperty
的名称应该对应 属性 ,在你的代码中将ActiveStepNumberProperty
更改为ActiveStepProperty
public static readonly BindableProperty ActiveStepProperty =
BindableProperty.Create(
nameof(ActiveStep),
typeof(int),
typeof(StepWizardControl),
1,
BindingMode.TwoWay
);
public int ActiveStep
{
get => (int)GetValue(ActiveStepProperty);
set => SetValue(ActiveStepProperty, value);
}
- 它应该通知 viewmodel 的更改,然后
BindableProperty
将在之后更改。
public class StepWizardPageViewModel :BaseViewModel
{
private int currentStep;
public int CurrentStep {
get {
return currentStep;
}
set {
currentStep = value;
NotifyPropertyChanged(); //add the notify method defined in PrismBaseViewModel
}
}
//xxx
}