【发布时间】:2021-07-25 10:29:47
【问题描述】:
<Label x:Name="lbl" Text="{Binding Source={Static local:ViewModel.Texty}}"
这里是 ViewModel 类
public class DownloadDataPageViewModel : INotifyPropertyChanged
{
public static event PropertyChangedEventHandler StaticPropertyChanged;
private static void OnStaticPropertyChanged(string propertyName)
{
StaticPropertyChanged?.Invoke(null,
new PropertyChangedEventArgs(propertyName));
}
private static string _texty;
public static string Texty
{
get => _texty;
set
{
_texty = value;
OnStaticPropertyChanged("Texty");
}
}
}
我面临的问题是它的标签显示“StaticCheckApp.ViewModel”而不是数据更改。
【问题讨论】:
-
INotifyPropertyChanged.PropertyChanged事件未调用。 -
这不是你应该使用的方式
INotifyPropertyChanged。它旨在通知实例属性中的属性更改,而不是静态属性!另外Binding关心Path的Binding内的属性更改通知。它的Source将被计算一次,如果我没记错的话永远不会改变。 -
我不确定如何实现这一点,我应该如何绑定到静态资源。我浏览了在线文档,我猜这是我能得到的最好的。
-
这可能会有所帮助:another question
-
您的 Texty 更新代码可能会有所帮助。
标签: c# xaml xamarin.forms