簡體   English   中英

在Xamarin.Forms中綁定自定義視圖

[英]Binding a Custom View In Xamarin.Forms

我在將Xamarin表單中的自定義視圖中的數據綁定到包含頁面的視圖模型時遇到問題。

我的自定義視圖非常簡單,一對代表鍵值對的標簽:

<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="KeyValueView">
    <Grid VerticalOptions="Start">
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>

        <Label x:Name="KeyLabel" Text="{Binding Key}"  Grid.Column="0" HorizontalOptions="Start" />
        <Label x:Name="ValueLabel" Text="{Binding Value}" Grid.Column="1" HorizontalOptions="EndAndExpand" />
    </Grid>
</ContentView>

后面的代碼:

public partial class KeyValueView : ContentView
{
    public KeyValueView()
    {
        InitializeComponent();
        this.VerticalOptions = LayoutOptions.Start;
        this.BindingContext = this;
    }

    public static readonly BindableProperty ValueProperty =
                BindableProperty.Create<KeyValueView, string>(w => w.Value, default(string));

    public string Value
    {
        get {return (string)GetValue(ValueProperty);}
        set {SetValue(ValueProperty, value);}
    }

    public static readonly BindableProperty KeyProperty =
        BindableProperty.Create<KeyValueView, string>(w => w.Key, default(string));

    public string Key
    {
        get {return (string)GetValue(KeyProperty);}
        set {SetValue(KeyProperty, value);}
    }
}

這在頁面中消耗如下:

<views:KeyValueView Key="Order Number" Value="{Binding document_number}" />

問題是Key字符串按預期顯示,但值字符串不是。 我試過在document_number屬性上強制一個PropertyChanged事件,這沒有幫助。 我還嘗試在自定義視圖的鍵/值屬性的設置器中顯式設置標簽上的Text屬性:

public string Key
    {
        get {return (string)GetValue(KeyProperty);}
        set {
            SetValue(KeyProperty, value);
            KeyLabel.Text = value;
        }
    }

再次這沒有幫助,setter代碼似乎永遠不會被執行(我在它上面放置了一個斷點並且它沒有被擊中)

如果我添加一個開箱即用的控件,例如直接綁定到頁面中屬性的Label,則會正確顯示:

<Label Text="{Binding document_number}"/>
<views:KeyValueView Key="Order Number" Value="{Binding document_number}" />

誰能解釋為什么會發生這種情況(或者更確切地說沒有發生)?

不要在自定義控件內部分配綁定。 用這個:

public partial class KeyValueView : ContentView
{
    public KeyValueView()
    {
        InitializeComponent();
        this.VerticalOptions = LayoutOptions.Start;
    }

    public static readonly BindableProperty ValueProperty =
        BindableProperty.Create<KeyValueView, string>(w => w.Value, default(string));

    public string Value
    {
        get {return (string)GetValue(ValueProperty);}
        set {SetValue(ValueProperty, value);}
    }

    public static readonly BindableProperty KeyProperty =
        BindableProperty.Create<KeyValueView, string>(w => w.Key, default(string));

    public string Key
    {
        get {return (string)GetValue(KeyProperty);}
        set {SetValue(KeyProperty, value);}
    }
    protected override void OnPropertyChanged(string propertyName)
    {
        base.OnPropertyChanged(propertyName);

        if (propertyName == ValueProperty.PropertyName)
        {
            ValueLabel.Text = Value;
        }
        if (propertyName == KeyProperty.PropertyName)
        {
            KeyLabel.Text = Key;
        }
    }
}

<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="KeyValueView">
    <Grid VerticalOptions="Start">
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>

        <Label x:Name="KeyLabel" Grid.Column="0" HorizontalOptions="Start" />
        <Label x:Name="ValueLabel" Grid.Column="1" HorizontalOptions="EndAndExpand" />
    </Grid>
</ContentView>

將屬性(例如,值)視為對BindableProperty(ValueProperty)的引用。 如果你在Value setter BindableProperty中做了一些事情,那么BindableProperty將不會收到通知,如果你對BindableProperty做了一些事情(分配/更改ValueProperty),則不會調用屬性值setter。

如果要處理PropertyChanged,則可以覆蓋(OnPropertyChanged)或Actions(作為創建BindableProperty時的附加參數)。

我能夠通過這樣做來實現這一點

public KeyValueView()
{
    InitializeComponent();
    this.VerticalOptions = LayoutOptions.Start;
    this.Content.BindingContext = this;
}

然后在xaml.cs中為您正在使用自定義視圖的視圖執行此操作:

public ParentView()
{
    InitializeComponent();
    BindingContext = this;
}

對於一個給我帶來問題的Entry字段,我必須確保defaultBindingMode參數設置為TwoWay。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM