【发布时间】:2018-08-24 14:17:14
【问题描述】:
如何在不编写代码的情况下将属性值传递给自定义控件子项?
内容页面:
<views:ButtonView Image="newViolationSmall.png" Text="{Binding ParentsText}/>
然后是ButtonView.xaml
<StackLayout
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="SmartwebsCrossPlatform.Portable.Views.ButtonView">
<Button Image="{Binding Image}"/>
<Label Text="{Binding Text}" />
</StackLayout>
还有 ButtonView.cs
public partial class ButtonView : StackLayout {
...
//text
public static readonly BindableProperty TextProperty = BindableProperty.Create( "Text", typeof( string ), typeof( ButtonView ), null, BindingMode.Default, null, null, null, null );
public string Text {
get {
return (string)GetValue( TextProperty );
}
set {
SetValue( TextProperty, value );
}
}
//image
public static readonly BindableProperty ImageProperty = BindableProperty.Create( "Image", typeof( string ), typeof( ButtonView ) );
public string Image {
get {
return (string)GetValue( ImageProperty );
}
set {
SetValue( ImageProperty, value );
}
}
这不显示任何文本或图像。一个相当烦人的替代方法是同时命名 Button 和 Label,覆盖 ButtonView.OnPropertyChanged,并从代码中显式设置它们的属性,但是当我浏览此页面时,它会导致 System.ObjectDisposedException(对象名称:'Android.Graphics.Bitmap')。
@yyou 的解决方案适用于 const 值,但不适用于 parent 的属性。
【问题讨论】:
标签: xamarin.forms inotifypropertychanged