Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# UWP应用程序未更新视图_C#_Uwp_Mvvm Light_Fody Propertychanged - Fatal编程技术网

C# UWP应用程序未更新视图

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 { 公

一个简单的练习:在UWP应用程序的文本块中显示当前时间。 我正在使用MVVMlight和PropertyChanged.Fody

作为本示例的基础,我在这里使用本文和MVVMlight/Fody实现:

我有一个主视图模型。在这里,我创建了DateTimeModel类的一个实例,如果引发属性更改事件(正在工作),我已经添加了一个调试输出

使用系统诊断;
使用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}"/>

我用过直接装订——这对我来说是最优雅的方式。谢谢。我用的是直接装订——这对我来说是最优雅的方式。谢谢