Привязка настраиваемой записи в Xamarin Forms - PullRequest
0 голосов
/ 14 июля 2020

Я пытаюсь извлечь эту запись в кадре в настраиваемый элемент в Xamarin, чтобы получить многоразовую запись с рамкой только сверху и снизу:

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

Когда я пытаюсь использовать это настраиваемое поле, свойства Placeholder и Text устанавливаются правильно, но я не могу привязать их к атрибутам в моем классе:

// 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}" />

Я могу подтвердить, что Компания имеет значение, потому что с обычным текстовым полем работает корректно:

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

1 Ответ

1 голос
/ 14 июля 2020

Причина: в вашем случае вы устанавливаете BindingContext в CbSingleEntry

BindingContext = this;

, поэтому привязка в ContentPage больше не будет работать.

Решение:

Вы можете изменить код в CbSingleEntry

в xaml

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