绑定Xamarin表单中的自定义条目 [英] Binding a custom Entry in Xamarin Forms

查看:81
本文介绍了绑定Xamarin表单中的自定义条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将此框架中的条目提取到Xamarin中的自定义元素中,以获取仅在顶部和底部具有边框的可重复使用的条目:

I'm trying to extract this Entry in a Frame into a custom element in Xamarin, to get a reusable Entry with border only on top and on bottom:

<Frame xmlns="..."
       HasShadow="False"
       CornerRadius="0"
       Padding="0, 1, 0, 1"
       BackgroundColor="#c0c0c0">
    <Entry Padding="20, 10, 20, 10"
           Placeholder="{Binding Placeholder}"
           Text="{Binding Text}"
           BackgroundColor="#ffffff" />
</Frame>

后面的代码:

public partial class CbSingleEntry : Frame
{
    public static readonly BindableProperty TextProperty = BindableProperty.Create("Text", typeof(string), typeof(CbSingleEntry));
    public static readonly BindableProperty PlaceholderProperty = BindableProperty.Create("Placeholder", typeof(string), typeof(CbSingleEntry));

    public string Text
    {
        get { return (string)GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }
    }

    public string Placeholder
    {
        get { return (string)GetValue(PlaceholderProperty); }
        set { SetValue(PlaceholderProperty, value); }
    }

    public CbSingleEntry()
    {
        InitializeComponent();

        BindingContext = this;
    }
}

当我尝试使用此自定义字段时,正确设置了占位符和文本属性,但无法将它们绑定到类中的属性上:

When I try to use this custom field, the Placeholder and Text property are correctly set, but I can't bind them to attributes in my class:

// this one works fine
<local:CbSingleEntry Placeholder="Company" Text="My Company" />
// Placeholder works, but Text is always empty
<local:CbSingleEntry Placeholder="Company" Text="{Binding Company}" />

我可以确认Company具有价值,因为使用正常的文本字段,它可以正常工作:

I can confirm that Company has a value, because with a normal text field it works correctly:

// This one works as expected, Text is displayed from binded attribute
<Entry Placeholder="Company" Text="{Binding Company}" />

推荐答案

原因:在您的情况下,您在CbSingleEntry

Cause : in your case , you set the BindingContext in CbSingleEntry

BindingContext = this;

因此ContentPage中的绑定将不再起作用.

So the binding in ContentPage will not work any more .

解决方案:

您可以在 CbSingleEntry

<Frame 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"
             
             x:Name="CustomView"  // set the name here
       
             x:Class="xxx">
 
       
           <Entry
             
           Placeholder="{Binding Source={x:Reference CustomView},Path=Placeholder}"
           Text="{Binding Source={x:Reference CustomView},Path=Text}"
           BackgroundColor="#ffffff" />
        
    
</Frame>

在后面的代码中

public partial class CbSingleEntry : Frame
{
    public static readonly BindableProperty TextProperty = BindableProperty.Create("Text", typeof(string), typeof(CbSingleEntry));
    public static readonly BindableProperty PlaceholderProperty = BindableProperty.Create("Placeholder", typeof(string), typeof(CbSingleEntry));

    public string Text
    {
        get { return (string)GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }
    }

    public string Placeholder
    {
        get { return (string)GetValue(PlaceholderProperty); }
        set { SetValue(PlaceholderProperty, value); }
    }

    public CbSingleEntry()
    {
        InitializeComponent();

       // BindingContext = this; don't need to set it any more
    }
}

这篇关于绑定Xamarin表单中的自定义条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆