C# UWP应用程序未更新视图
一个简单的练习:在UWP应用程序的文本块中显示当前时间。 我正在使用MVVMlight和PropertyChanged.Fody 作为本示例的基础,我在这里使用本文和MVVMlight/Fody实现: 我有一个主视图模型。在这里,我创建了DateTimeModel类的一个实例,如果引发属性更改事件(正在工作),我已经添加了一个调试输出C# UWP应用程序未更新视图,c#,uwp,mvvm-light,fody-propertychanged,C#,Uwp,Mvvm Light,Fody Propertychanged,一个简单的练习:在UWP应用程序的文本块中显示当前时间。 我正在使用MVVMlight和PropertyChanged.Fody 作为本示例的基础,我在这里使用本文和MVVMlight/Fody实现: 我有一个主视图模型。在这里,我创建了DateTimeModel类的一个实例,如果引发属性更改事件(正在工作),我已经添加了一个调试输出 使用系统诊断; 使用GalaSoft.MvvmLight; 使用Logic.Ui.Models.DateTime; 使用改变的财产; 名称空间逻辑.Ui { 公
使用系统诊断;
使用GalaSoft.MvvmLight;
使用Logic.Ui.Models.DateTime;
使用改变的财产;
名称空间逻辑.Ui
{
公共类MainViewModel:ViewModelBase,INotifyPropertyChanged
{
公共日期时间模型;
[DependsOn(name of(DateTimeModel))]
公共日期时间CurrentDateTime=>DateTimeModel.CurrentDateTime;
公共主视图模型()
{
DateTimeModel=新的DateTimeModel();
DateTimeModel.PropertyChanged+=(s,e)=>
{
Debug.WriteLine(“DateTime属性更改”);
};
}
#地区活动
公共事件属性更改事件处理程序属性更改;
#端区
}
}
为MainViewModel
的CurrentDateTime
引发PropertyChanged
事件,每当引发DateTimeModel
的PropertyChanged
事件时,您都会绑定到该事件:
public class MainViewModel : ViewModelBase, INotifyPropertyChanged
{
public DateTimeModel DateTimeModel;
[DependsOn(nameof(DateTimeModel))]
public DateTime CurrentDateTime => DateTimeModel.CurrentDateTime;
public MainViewModel()
{
DateTimeModel = new DateTimeModel();
DateTimeModel.PropertyChanged += (s, e) =>
{
Debug.WriteLine("DateTime PropertyChanged");
this.RaisePropertyChanged(nameof(CurrentDateTime)); //<---
};
}
#region Events
public event PropertyChangedEventHandler PropertyChanged;
#endregion
}
…并直接绑定到DateTimeModel
的CurrentDateTime
属性:
<TextBlock Text="{Binding DateTimeModel.CurrentDateTime}"/>
为
MainViewModel
的CurrentDateTime
引发PropertyChanged
事件,每当引发DateTimeModel
的PropertyChanged
事件时,您都会绑定到该事件:
public class MainViewModel : ViewModelBase, INotifyPropertyChanged
{
public DateTimeModel DateTimeModel;
[DependsOn(nameof(DateTimeModel))]
public DateTime CurrentDateTime => DateTimeModel.CurrentDateTime;
public MainViewModel()
{
DateTimeModel = new DateTimeModel();
DateTimeModel.PropertyChanged += (s, e) =>
{
Debug.WriteLine("DateTime PropertyChanged");
this.RaisePropertyChanged(nameof(CurrentDateTime)); //<---
};
}
#region Events
public event PropertyChangedEventHandler PropertyChanged;
#endregion
}
…并直接绑定到DateTimeModel
的CurrentDateTime
属性:
<TextBlock Text="{Binding DateTimeModel.CurrentDateTime}"/>
您面临的问题是
MainViewModel。CurrentDateTime
仅在分配MainViewModel.DateTimeModel
时收到通知,而不是在DateTimeModel
的属性更改时收到通知
一个允许你通知子属性更改的人,如下所示:
public class MainViewModel : ViewModelBase, INotifyPropertyChanged
{
// ... snip ...
[DependsOn(nameof(DateTimeModel))]
[DependsOn("DateTimeModel.CurrentDateTime")]
public DateTime CurrentDateTime => DateTimeModel.CurrentDateTime;
}
但是我认为删除MainViewModel.CurrentDateTime
并直接绑定到MainViewModel.DateTimeModel
要优雅得多
<TextBlock Text="{Binding DateTimeModel.CurrentDateTime}"/>
您面临的问题是
MainViewModel。CurrentDateTime
仅在分配MainViewModel.DateTimeModel
时收到通知,而不是在DateTimeModel
的属性更改时收到通知
一个允许你通知子属性更改的人,如下所示:
public class MainViewModel : ViewModelBase, INotifyPropertyChanged
{
// ... snip ...
[DependsOn(nameof(DateTimeModel))]
[DependsOn("DateTimeModel.CurrentDateTime")]
public DateTime CurrentDateTime => DateTimeModel.CurrentDateTime;
}
但是我认为删除MainViewModel.CurrentDateTime
并直接绑定到MainViewModel.DateTimeModel
要优雅得多
<TextBlock Text="{Binding DateTimeModel.CurrentDateTime}"/>
我用过直接装订——这对我来说是最优雅的方式。谢谢。我用的是直接装订——这对我来说是最优雅的方式。谢谢