.NET CORE(C#) WPF開發

閱讀導航

  1. 常用類屬性設置、獲取方式
  2. 二次封裝 INotifyPropertyChanged
  3. Demo 展示、源碼下載

1. 常用類屬性設置、獲取方式

<code>public class Student : INotifyPropertyChanged
{
private string name;
public string Name
{
get { return name; }
set
{
if(name != value)
{
name = value;
OnPropertyChanged("Name")
}
}
}

#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
}/<code>

這種方式總感覺有點囉嗦,如果 Name 屬性名修改,字符串 "Name" 還要手動修改,就算 Ctrl+H 替換也容易出錯,如果使用下面這種方式,是不是感覺更清爽一點?

<code>public class Student : PropertyNotifyObject 

{
public string Name
{
get { return this.GetValue(cu => cu.Name); }
set { this.SetValue(cu => cu.Name, value); }
}
}/<code>

2. 二次封裝INotifyPropertyChanged

封裝的基類PropertyNotifyObject出處我忘了,15年網上找的,源碼如下:

<code>using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq.Expressions;
using System.Windows;
using System.Windows.Threading;

namespace MVVMTest
{

#region 封裝WPF屬性

public class MyCommMetoh
{
//得到屬性的名稱
public static string GetPropertyName(Expression<func>> exp)
{
string _pName = "";
if (exp.Body is MemberExpression)
{
_pName = (exp.Body as MemberExpression).Member.Name;
}
else if (exp.Body is UnaryExpression)
{
_pName = ((exp.Body as UnaryExpression).Operand as MemberExpression).Member.Name;
}
return _pName;
}
}

public class NotifyPropertyBase : INotifyPropertyChanged
{
/// <summary>
/// Returns a dispatcher for multi-threaded scenarios

/// /<summary>
/// <returns>
public static Dispatcher GetDispatcher(DispatcherObject source = null)
{
//use the application's dispatcher by default
if (Application.Current != null) return Application.Current.Dispatcher;

//fallback for WinForms environments
if (source != null && source.Dispatcher != null) return source.Dispatcher;

//ultimatively use the thread's dispatcher
return Dispatcher.CurrentDispatcher;
}

#region INotifyPropertyChanged
public void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
GetDispatcher().BeginInvoke((Action)delegate
{
try
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
catch (Exception ex)
{
var msg = string.Format("發送UI屬性變化事件異常,屬性名稱: {0}, 異常詳細信息: {1}", propertyName, ex.ToString());
}
}
);

}
}
public void OnPropertyChanged(Expression<func>> expression)
{
if (PropertyChanged != null)
{
var propertyName = ((MemberExpression)expression.Body).Member.Name;
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public event PropertyChangedEventHandler PropertyChanged;
#endregion
}
public static class NotifyPropertyBaseEx
{

public static void OnPropertyChanged(this T npb, Expression<func>> exp) where T : NotifyPropertyBase, new()
{
string _PropertyName = MyCommMetoh.GetPropertyName(exp);
npb.OnPropertyChanged(_PropertyName);
}
}

public class PropertyNotifyObject : NotifyPropertyBase, IDisposable
{
public PropertyNotifyObject()
{
}

Dictionary<object> _ValueDictionary = new Dictionary<object>();

#region 根據屬性名得到屬性值
public T GetPropertyValue(string propertyName)
{
if (string.IsNullOrEmpty(propertyName)) throw new ArgumentException("invalid " + propertyName);
object _propertyValue;
if (!_ValueDictionary.TryGetValue(propertyName, out _propertyValue))
{
_propertyValue = default(T);
_ValueDictionary.Add(propertyName, _propertyValue);
}
return (T)_propertyValue;
}
#endregion

public void SetPropertyValue(string propertyName, T value)
{
if (!_ValueDictionary.ContainsKey(propertyName) || _ValueDictionary[propertyName] != (object)value)
{
_ValueDictionary[propertyName] = value;
OnPropertyChanged(propertyName);
}
}

#region Dispose
public void Dispose()
{
DoDispose();
}
~PropertyNotifyObject()
{
DoDispose();

}
void DoDispose()
{
if (_ValueDictionary != null)
_ValueDictionary.Clear();
}
#endregion
}
public static class PropertyNotifyObjectEx
{
public static U GetValue(this T t, Expression<func>> exp) where T : PropertyNotifyObject
{
string _pN = MyCommMetoh.GetPropertyName(exp);
return t.GetPropertyValue(_pN);
}

public static void SetValue(this T t, Expression<func>> exp, U value) where T : PropertyNotifyObject
{
string _pN = MyCommMetoh.GetPropertyName(exp);
t.SetPropertyValue(_pN, value);
}
}

#endregion
}
/<func>
/<func>
/<object>/<object>/<func>
/<func>
/<func>
/<code>

3. Demo展示、源碼下載

源碼下載:MVVMTest

展示效果:

除非註明,文章均由 Dotnet9 整理發佈,歡迎轉載。

轉載請註明本文地址:https://dotnet9.com/8572.html


時間如流水,只能流去不流回!

這段時間在家,除了睡覺,也不要忘了學習。


分享到:


相關文章: