Xamarin.forms Xamarin表单自定义控件数据绑定问题
我有一个简单的自定义控件,我很难让一个公开的bindable命令属性正常工作 以下是设置:MainPage.xaml承载CustomControl.xaml(下面的米色区域)。CustomControl包含一个标签和一个按钮。主页包含CustomControl、条目、标签和按钮。所有控件都绑定到MainPageViewModel中的CustomControlText属性。因此,当属性更改时,所有控件都应更新 它主要起作用 请看下面的演示视频。我单击主页上的按钮,所有控件都会更新,包括自定义控件。当我更改输入值时,所有字段都会更新。但是,单击“从CustomControl增量”不会产生任何效果。它应该调用MainViewModel中的SubmitCommand 2 下面是我的所有代码(简单文件/新项目示例)。如何更改此代码,以便单击“从CustomControl增量”最终调用MainPageViewModel中的SubmitCommand2命令 MainPage.xamlXamarin.forms Xamarin表单自定义控件数据绑定问题,xamarin.forms,Xamarin.forms,我有一个简单的自定义控件,我很难让一个公开的bindable命令属性正常工作 以下是设置:MainPage.xaml承载CustomControl.xaml(下面的米色区域)。CustomControl包含一个标签和一个按钮。主页包含CustomControl、条目、标签和按钮。所有控件都绑定到MainPageViewModel中的CustomControlText属性。因此,当属性更改时,所有控件都应更新 它主要起作用 请看下面的演示视频。我单击主页上的按钮,所有控件都会更新,包括自定义控件。
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:App7"
x:Class="App7.MainPage">
<StackLayout>
<BoxView HeightRequest="100" />
<local:CustomControl
Margin="50"
WidthRequest="300"
TextData="{Binding CustomControlText}"
Command="{Binding SubmitCommand2}"
/>
<Entry Text="{Binding CustomControlText}" />
<Label Text="{Binding CustomControlText}" />
<Button Text="Increment from Main Page" Command="{Binding SubmitCommand}" />
</StackLayout>
</ContentPage>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="App7.CustomControl"
BackgroundColor="Beige"
x:Name="this">
<ContentView.Content>
<StackLayout>
<Entry x:Name="entryControl"
Placeholder="Enter Text"
Text="{Binding Source={x:Reference this}, Path=TextData}"
/>
<Button Text="Increment From CustomControl"
Command="{Binding Source={x:Reference this}, Path=Command}"
/>
</StackLayout>
</ContentView.Content>
</ContentView>
MainPageModel.cs
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
}
public class MainPageModel : FreshBasePageModel
{
public MainPageModel() { }
public string CustomControlText { get; set; }
private int _index = 0;
public Command SubmitCommand
{
get
{
return new Command(() =>
{
_index++;
CustomControlText = $"Hello World {_index}";
});
}
}
public Command SubmitCommand2
{
get
{
return new Command(() =>
{
_index++;
_index++;
CustomControlText = $"Hello World {_index}";
});
}
}
public override void Init(object initData)
{
CustomControlText = "Hello World";
base.Init(initData);
}
}
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class CustomControl : ContentView
{
public CustomControl()
{
TextData = "";
InitializeComponent();
}
public static readonly BindableProperty TextDataProperty = BindableProperty.Create(
propertyName: "TextData",
returnType: typeof(string),
declaringType: typeof(CustomControl),
defaultBindingMode: BindingMode.TwoWay,
defaultValue: "");
public string TextData
{
get { return base.GetValue(TextDataProperty).ToString(); }
set { base.SetValue(TextDataProperty, value); }
}
public static readonly BindableProperty CommandProperty = BindableProperty.Create(
propertyName: "Command",
returnType: typeof(Command),
declaringType: typeof(CustomControl),
defaultBindingMode: BindingMode.OneWay);
public Command Command { get; set; }
}
CustomControl.xaml
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:App7"
x:Class="App7.MainPage">
<StackLayout>
<BoxView HeightRequest="100" />
<local:CustomControl
Margin="50"
WidthRequest="300"
TextData="{Binding CustomControlText}"
Command="{Binding SubmitCommand2}"
/>
<Entry Text="{Binding CustomControlText}" />
<Label Text="{Binding CustomControlText}" />
<Button Text="Increment from Main Page" Command="{Binding SubmitCommand}" />
</StackLayout>
</ContentPage>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="App7.CustomControl"
BackgroundColor="Beige"
x:Name="this">
<ContentView.Content>
<StackLayout>
<Entry x:Name="entryControl"
Placeholder="Enter Text"
Text="{Binding Source={x:Reference this}, Path=TextData}"
/>
<Button Text="Increment From CustomControl"
Command="{Binding Source={x:Reference this}, Path=Command}"
/>
</StackLayout>
</ContentView.Content>
</ContentView>
CustomControl
的命令
属性也应使用base.GetValue
和base.SetValue
实现,方法与TextData
相同
public partial class CustomControl : ContentView
{
...
public static readonly BindableProperty CommandProperty =
BindableProperty.Create(
propertyName: nameof(Command),
returnType: typeof(ICommand),
declaringType: typeof(CustomControl),
defaultBindingMode: BindingMode.OneWay,
defaultValue: default(ICommand));
public ICommand Command
{
get => (ICommand) GetValue(CommandProperty);
set => SetValue(CommandProperty, value);
}
}
哦,是的,就是这样!