【问题标题】:Bindable Entry is not working inside custom control in Xamarin.forms?可绑定条目在 Xamarin.forms 的自定义控件中不起作用?
【发布时间】:2020-07-01 17:42:47
【问题描述】:

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

这是我的代码:

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 中:

但它不起作用。?有什么建议吗?

【问题讨论】:

  • 属性不是Text,而是叫MyEntryProperty。这将是正确的绑定<MyEntry MyEntry={Binding Something}/>... 但我不确定它是否可能具有声明类型和具有相同名称的属性。
  • 你到底想做什么?似乎您只需要使用Entry 而无需使用自定义条目。
  • 更新了 xaml。基本上自定义控件具有输入字段,我正在尝试通过可绑定属性捕获输入字段文本,但它不起作用。

标签: c# xaml xamarin xamarin.forms


【解决方案1】:

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

我的视图:

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

截图:

【讨论】:

  • 由于它的输入字段我希望用户输入文本,并且它总是显示为空。
【解决方案2】:

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

我以前检查这个的方式是

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

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

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

【讨论】:

    【解决方案3】:

    您只是在为 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);
    

    【讨论】:

      猜你喜欢
      • 2020-02-06
      • 2019-12-10
      • 1970-01-01
      • 2019-12-26
      • 1970-01-01
      • 2018-02-17
      • 1970-01-01
      • 2021-02-18
      • 1970-01-01
      相关资源
      最近更新 更多