Xamarin Forms自定义控件数据绑定问题

时间:2019-06-18 18:20:51

标签: xamarin.forms

我有一个简单的自定义控件,我无法使公开的可绑定Command属性起作用。

设置如下:MainPage.xaml托管CustomControl.xaml(下方的米色区域)。 CustomControl包含一个标签和一个按钮。 MainPage包含CustomControl,Entry,Label和Button。所有控件都绑定到MainPageViewModel中的CustomControlText属性。因此,随着该属性的更改,所有控件都应更新。

大部分有效...

观看下面的演示视频。我单击MainPage上的按钮,所有控件都会更新,包括自定义控件。当我更改Entry值时,所有字段都会更新。但是,单击“从CustomControl递增”没有任何作用。它应该在MainViewModel中调用SubmitCommand2。

enter image description here

我的所有代码都在下面(简单的文件/新项目示例)。我将如何更改此代码,以便单击“从CustomControl增量”最终调用MainPageViewModel中的SubmitCommand2命令?

MainPage.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>

MainPage.xaml.cs

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
    }
}

MainPageModel.cs

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);
    }
}

CustomControl.xaml

<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.xaml.cs

[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; }
}

1 个答案:

答案 0 :(得分:1)

Command的{​​{1}}属性也应使用CustomControlbase.GetValue来实现,与base.SetValue相同。

TextData