我是靠谱客的博主 激动鸡翅,最近开发中收集的这篇文章主要介绍INotifyPropertyChanged通知,mvvm更新数据,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

INotifyPropertyChanged

在WPF MVVM模式开发中,使用他可以通知数据更新

  1. 定义一个NotifyBase工具类,继承INotifyPropertyChanged

    public class NotifyBase : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
    
        public void DoNotify([CallerMemberName] string name = "")
        {
            PropertyChanged?.Invoke(this,new PropertyChangedEventArgs(name));
        }
    }
    
  2. 在绑定的Model中继承定义好的NotifyBase

     public class SongModel : NotifyBase
     {
         /// <summary>
         /// 歌曲url
         /// </summary>
         private string _songUrl;
         public string SongUrl
         {
             get { return this._songUrl; }
             set
             {
                 _songUrl = value;
                 DoNotify();
             }
         }
    
         /// <summary>
         /// 本地下载后的mp3路径
         /// </summary>
         private string _localSongUrl;
         public string LocalSongUrl
         {
             get { return this._localSongUrl; }
             set
             {
                 _localSongUrl = value;
                 DoNotify();
             }
         }
    
         /// <summary>
         /// 歌曲图片
         /// </summary>
         private string _picUrl;
         public string PicUrl
         {
             get { return this._picUrl; }
             set
             {
                 _picUrl = value;
                 DoNotify();
             }
         }
    
    
     }
    

最后

以上就是激动鸡翅为你收集整理的INotifyPropertyChanged通知,mvvm更新数据的全部内容,希望文章能够帮你解决INotifyPropertyChanged通知,mvvm更新数据所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(72)

评论列表共有 0 条评论

立即
投稿
返回
顶部