我正在嘗試制作一個自定義控制元件以在多個地方使用它。問題是它與標簽配合得很好,但是在進入時它甚至不會運行,它給了我
No property, BindableProperty, or event found for "EntryText"
這是我的自定義控制元件 Xaml:
<ContentView xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
<ContentView.Content>
<StackLayout>
<Label x:Name="MyLabel" />
<Entry x:Name="MyEntry" />
</StackLayout>
</ContentView.Content>
</ContentView>
及其背后的代碼
public partial class MyCustomControl : ContentView
{
public static readonly BindableProperty LabelTextProperty = BindableProperty.Create(
nameof(LabelText),
typeof(string),
typeof(MyCustomControl),
propertyChanged: LabelTextPropertyChanged);
private static void LabelTextPropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
var control = (MyCustomControl)bindable;
control.MyLabel.Text = newValue?.ToString();
Debug.WriteLine("LabelTextPropertyChanged: " control.MyEntry);
Debug.WriteLine("LabelTextPropertyChanged: new value" newValue);
}
public string LabelText
{
get => base.GetValue(LabelTextProperty)?.ToString();
set
{
base.SetValue(LabelTextProperty, value);
}
}
public static BindableProperty EntryBindableProperty = BindableProperty.Create(
nameof(EntryText),
typeof(string),
typeof(MyCustomControl),
propertyChanged:EntryBindablePropertyChanged
);
private static void EntryBindablePropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
var control = (MyCustomControl)bindable;
Debug.WriteLine("EntryBindablePropertyChanged: " control.MyEntry);
Debug.WriteLine("EntryBindablePropertyChanged: new value" newValue);
}
public string EntryText
{
get => base.GetValue(EntryBindableProperty)?.ToString();
set
{
base.SetValue(EntryBindableProperty, value);
}
}
public MyCustomControl()
{
InitializeComponent();
}
}
及其用法
<StackLayout>
<local:MyCustomControl
LabelText="{Binding BindingLabel}"
EntryText="{Binding BindingEntry}"/>
</StackLayout>
注意:我試圖洗掉
</ContentView.Content>
從我的 xaml 中,因為我已經看到了一些這樣的例子,而且我也嘗試在建構式后面的代碼中設定系結
public MyCustomControl()
{
InitializeComponent();
MyEntry.SetBinding(Entry.TextProperty, new Binding(nameof(EntryText) , source: this));
}
但也沒有為條目作業。那么如果我想將值系結到視圖模型,我該如何解決這個問題并進行任何其他設定。提前致謝。
更新: @Jessie Zhang -MSFT 感謝您的幫助我非常感謝,但是我在 MyCustomControl 代碼中發現了一個錯誤 => EntryTextProperty 是我必須宣告 defaultBindingMode 才能將資料傳遞給 ViewModel 屬性。所以我將 BindableProperty 代碼更改為:
public static BindableProperty EntryTextProperty = BindableProperty.Create(
nameof(EntryText),
typeof(string),
typeof(MyCustomControl),
defaultBindingMode: BindingMode.OneWayToSource,
propertyChanged:EntryBindablePropertyChanged
);
uj5u.com熱心網友回復:
您沒有將Entry.TextandEntryText屬性系結在MyCustomControl.xaml:
請參考以下代碼:
<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="CardViewDemo.Controls.MyCustomControl"
x:Name="this"
>
<ContentView.Content>
<StackLayout BindingContext="{x:Reference this}">
<Label x:Name="MyLabel" Text="{Binding LabelText}" />
<Entry x:Name="MyEntry" Text="{ Binding EntryText}" />
</StackLayout>
</ContentView.Content>
</ContentView>
我用下面的代碼做了一個測驗,它作業正常。
<local:MyCustomControl
LabelText="test1"
EntryText="test2"/>
更新:
從檔案Create a property中,我們知道:
可系結屬性的命名約定是可系結屬性識別符號必須與 Create 方法中指定的屬性名稱匹配,并附加“Property”。
因此,您可以將代碼更改EntryBindableProperty 為EntryTextProperty,如下所示:
public static readonly BindableProperty EntryTextProperty = BindableProperty.Create(nameof(EntryText), typeof(string),typeof(MyCustomControl),string.Empty, BindingMode.TwoWay, null, EntryBindablePropertyChanged);
public string EntryText
{
get => base.GetValue(EntryTextProperty)?.ToString();
set
{
base.SetValue(EntryTextProperty, value);
}
}
而xaml代碼是:
<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="CardViewDemo.Controls.MyCustomControl"
x:Name="template"
>
<ContentView.Content>
<StackLayout >
<Label x:Name="MyLabel" Text="{Binding Source={x:Reference template},Path=LabelText}"/>
<Entry x:Name="MyEntry" Text="{Binding EntryText, Source={x:Reference template}}" >
</Entry>
</StackLayout>
</ContentView.Content>
</ContentView>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/436752.html
