xamarin - 带有 OneWay 的 Xamarin 表单可绑定属性不起作用
问题描述
我想通过创建新的可绑定属性将 CustomLabel 绑定到 VM。
在 OneWay 模式下,第一个 VM 数据已正确更改 CustomLabel 的属性。但它从第二次开始就不起作用了。
尽管发生了 VM 事件,但 CustomView 的 Bindable 属性尚未触发其 PropertyChanged 事件。
不过,它在双向模式下可以正常工作。
测试了两天,一直在寻找原因,但我找不到它。
有人告诉我该怎么做吗?
// HomeViewModel.cs
public class HomeViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string _customName = "-";
public string CustomName
{
get
{
Debug.WriteLine("Get_CustomName");
return _customName;
}
set
{
if (value != _customName)
{
Debug.WriteLine("Set_CustomName");
_customName = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(CustomName)));
}
}
}
}
// MainPage.cs
public partial class MainPage : ContentPage
{
HomeViewModel Vm = new HomeViewModel();
public MainPage()
{
InitializeComponent();
BindingContext = Vm;
}
void ButtonTrue_Clicked(object sender, EventArgs e)
{
Vm.CustomName = "True";
}
void ButtonFalse_Clicked(object sender, EventArgs e)
{
Vm.CustomName = "False";
}
}
<!-- MainPage.xaml -->
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:Ex_Binding"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="Ex_Binding.MainPage">
<StackLayout Padding="50,0" VerticalOptions="Center">
<StackLayout Orientation="Horizontal" HorizontalOptions="Center">
<Label Text="Custom Result : " />
<local:CustomLabel x:Name="lbCustom" MyText="{Binding CustomName}" HorizontalOptions="Center" />
</StackLayout>
<StackLayout Orientation="Horizontal">
<Button Text="TRUE" BackgroundColor="LightBlue" HorizontalOptions="FillAndExpand" Clicked="ButtonTrue_Clicked" />
<Button Text="FALSE" BackgroundColor="LightPink" HorizontalOptions="FillAndExpand" Clicked="ButtonFalse_Clicked" />
</StackLayout>
</StackLayout>
</ContentPage>
// CustomLabel.cs
public class CustomLabel : Label
{
public static readonly BindableProperty MyTextProperty = BindableProperty.Create(nameof(MyText), typeof(string), typeof(CustomLabel), null, BindingMode.OneWay, propertyChanged: OnMyTextChanged);
private static void OnMyTextChanged(BindableObject bindable, object oldValue, object newValue)
{
var thisBindable = (CustomLabel)bindable;
if (thisBindable != null)
{
thisBindable.MyText = (string)newValue;
}
}
public string MyText
{
get => (string)GetValue(MyTextProperty);
set
{
SetValue(MyTextProperty, value);
Text = value;
}
}
}
解决方案
原因 :
thisBindable.MyText = (string)newValue;
因为您在MyText的值更改时设置了它的值。所以下次永远不会调用它(在 TwoWay 中,该方法将被多次调用)。
解决方案:
您应该直接设置Text in OnMyTextChanged
。
private static void OnMyTextChanged(BindableObject bindable, object oldValue, object newValue)
{
var thisBindable = (CustomLabel)bindable;
if (thisBindable != null)
{
thisBindable.Text = (string)newValue;
}
}
public string MyText
{
get => (string)GetValue(MyTextProperty);
set
{
SetValue(MyTextProperty, value);
//Text = value;
}
}
推荐阅读
- ios - 当应用程序位于同一个 git 存储库 - iOS 时,如何链接框架?
- javascript - Javascript:将 JSON 解析为字符串数组
- python - 请求不发送具有正确 ip 的请求
- java - 如何创建安全的 Lombok JPA 实体?
- .net - 如何在毛伊岛使用 NavigationPage?
- c++ - 我可以在没有默认初始化的情况下初始化数组的 std::unique_ptr 吗(我只想让它有虚拟值)
- r - 通过增加样品编号分配组 ID
- javascript - 如何在另一个嵌套屏幕中显示嵌套屏幕?
- snowflake-cloud-data-platform - 如何将 Snowsql 配置为在连接 Snowflake 后立即自动运行 sql 脚本?
- javascript - 如何在浏览器重新加载时发出数据?