所以我仔细阅读了我的一些代码,我意识到有一些ViewCell,我在其中使用
PropertyChanged
事件和其他情况下,我使用平面旧绑定。现在为了保持一致性,我想我将遵循两种方法之一。
例如我所说的通过PropertyChanged进行分配:
C类#
public string TitleText
{
get => base.GetValue(TitleTextProperty).ToString();
set => base.SetValue(TitleTextProperty, value);
}
public static readonly BindableProperty TitleTextProperty = BindableProperty.Create(
nameof(TitleText),
typeof(string),
typeof(MyViewCell),
default(string),
propertyChanged: TitleTextChange
);
private static void TitleTextChange(BindableObject bindable, object oldValue, object newValue)
{
MyViewCell cell = bindable as MyViewCell;
cell.titleLabel.Text = newValue?.ToString() ?? string.Empty;
}
XAML公司
<Label
x:Name="titleLabel"
HorizontalTextAlignment="Start"
Style="{StaticResource CellTitle}" />
通过绑定示例
C类#
public string TitleText
{
get => base.GetValue(TitleTextProperty).ToString();
set => base.SetValue(TitleTextProperty, value);
}
public static readonly BindableProperty TitleTextProperty = BindableProperty.Create(
nameof(TitleText),
typeof(string),
typeof(MyViewCell),
default(string)
);
<Label
x:Name="titleLabel"
Text="{Binding TitleText}"
HorizontalTextAlignment="Start"
Style="{StaticResource CellTitle}" />
注意:我的viewcell和自定义控件就是这样