[英]How to pass object as bindable property to Xamarin.Forms custom control
[英]Bindable Entry is not working inside custom control in Xamarin.forms?
我在自定義控件中有一個輸入字段。 我已經為它創建了可綁定屬性。
這是我的代碼:
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:
<MyView MyEntry={綁定一些東西}/>
但它不工作。? 有什么建議么?
也許您沒有正確設置綁定。 我制作了一個代碼示例供您參考。
我的觀點:
<?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;
}
}
截屏:
我的自定義控件遇到了這個問題,主要問題是我的綁定上下文設置不正確。
我用來檢查的方式是
我在 xaml 中給了我的控件一個x:name
在我輸入這個的InializeComponents()
方法之后,我去了構造函數中的 pagename.cs (后面的代碼)
Console.WriteLine($"Control-{ControlXname.BindingContext();}");
Console.WriteLine($"Page-{this.BindingContext();}");
您只是在為 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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.