繁体   English   中英

可绑定条目在 Xamarin.forms 的自定义控件中不起作用?

[英]Bindable Entry is not working inside custom control in Xamarin.forms?

我在自定义控件中有一个输入字段。 我已经为它创建了可绑定属性。

这是我的代码:

public string MyEntry
    {
        get => (string)GetValue(MyEntryProperty);
        set => SetValue(MyEntryProperty, value);
    }
    public static BindableProperty MyEntryProperty = BindableProperty.Create(
                                             propertyName: "MyEntry",
                                             returnType: typeof(string),
                                             declaringType: typeof(MyView),
                                             defaultValue: string.Empty,
                                             defaultBindingMode: BindingMode.TwoWay,
                                             propertyChanged:MyEntryPropertyChanged );


 public static void MyEntryPropertyChanged(BindableObject bindable, object oldValue, object newValue)
    {
       var cView = (MyView)bindable;
       cView.myent.Text = (string)newValue;
   }

然后在我的实际视图xaml:

<MyView MyEntry={绑定一些东西}/>

但它不工作。? 有什么建议么?

也许您没有正确设置绑定。 我制作了一个代码示例供您参考。

我的观点:

<?xml version="1.0" encoding="utf-8" ?>
<ContentView
x:Class="XamarinDemo.Custom_Control.MyView"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<StackLayout>
    <Entry
        x:Name="myent"
        HorizontalOptions="CenterAndExpand"
        Text="Welcome to Xamarin.Forms!"
        VerticalOptions="CenterAndExpand" />
    <Button />
</StackLayout>

</ContentView>

代码背后:和你的一样。

 public partial class MyView : ContentView
{
    public MyView()
    {
        InitializeComponent();
    }
    public string MyEntry
    {
        get => (string)GetValue(MyEntryProperty);
        set => SetValue(MyEntryProperty, value);
    }
    public static BindableProperty MyEntryProperty = BindableProperty.Create(
                                             propertyName: "MyEntry",
                                             returnType: typeof(string),
                                             declaringType: typeof(MyView),
                                             defaultValue: string.Empty,
                                             defaultBindingMode: BindingMode.TwoWay,
                                             propertyChanged: MyEntryPropertyChanged);


    public static void MyEntryPropertyChanged(BindableObject bindable, object oldValue, object newValue)
    {
        var cView = (MyView)bindable;
        cView.myent.Text = (string)newValue;
    }
}

用法:Page1.xaml

 <ContentPage.Content>
    <StackLayout>
        <local:MyView MyEntry="{Binding Something}"></local:MyView>
    </StackLayout>
</ContentPage.Content>

捆绑:

 public partial class Page1 : ContentPage
{
    public string Something { get; set; }
    public Page1()
    {
        InitializeComponent();
        Something = "Hello";
        this.BindingContext = this;
    }
}

截屏:

在此处输入图像描述

我的自定义控件遇到了这个问题,主要问题是我的绑定上下文设置不正确。

我用来检查的方式是

  1. 我在 xaml 中给了我的控件一个x:name

  2. 在我输入这个的InializeComponents()方法之后,我去了构造函数中的 pagename.cs (后面的代码)

Console.WriteLine($"Control-{ControlXname.BindingContext();}");
Console.WriteLine($"Page-{this.BindingContext();}");
  1. 在我执行完我的代码后,我前往 output 面板,然后按 ctrl + f 搜索单词“Control-”和“Page-”,然后我发现它们的绑定上下文不同。

您只是在为 Entry 设置值,而不是从 Entry 读取更改。 您必须在构造函数中添加以下内容。

// ... add this in your constructor...
myent.SetBinding(Entry.TextProperty, new Binding{
    Path = "MyEntry",
    Source = this,
    Mode = BindingMode.TwoWay
});



public string MyEntry
{
    get => (string)GetValue(MyEntryProperty);
    set => SetValue(MyEntryProperty, value);
}

// remove onPropertyChanged
public static BindableProperty MyEntryProperty = BindableProperty.Create(
                                         propertyName: "MyEntry",
                                         returnType: typeof(string),
                                         declaringType: typeof(MyView),
                                         defaultValue: string.Empty,
                                         defaultBindingMode: BindingMode.TwoWay);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM