C#?wpf定義ViewModelBase進行簡化屬性綁定
概述
綁定機制是wpf的核心,也是界面獨立的根本,尤其是使用了mvvm模式,沒有業(yè)務邏輯的干擾,使得界面得以專注于絢麗效果的實現(xiàn)。在xaml中編寫綁定語句后,在業(yè)務邏輯層需要定義相應的屬性與其綁定,需要繼承INotifyPropertyChanged,并通過PropertyChanged通知屬性改變。但是每個地方都去繼承實現(xiàn)INotifyPropertyChanged還是有點麻煩。
具體方法
1、繼承INotifyPropertyChanged
繼承INotifyPropertyChanged后需要定義一個事件。
public event PropertyChangedEventHandler PropertyChanged;
2、定義通知方法
定義一個動作屬性改變的方法。這一步驟將2個參數(shù),簡化成了一個字符串參數(shù)。
protected void RaisePropertyChangedEvent( string propertyName )
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
3、自動獲取屬性名
調用的時候要寫屬性名還是有點麻煩,進一步簡化,將屬性名參數(shù)省去。
在.net 4.5以下需要通過棧幀和反射獲取屬性名,在.net4.5或core1.0以上使用System.Runtime.CompilerServices.CallerMemberName屬性即可。
string GetCallerMemberName()
{
StackTrace trace = new StackTrace();
StackFrame frame = trace.GetFrame(2);//1代表上級,2代表上上級,以此類推
var propertyName = frame.GetMethod().Name;
if (propertyName.StartsWith("get_") || propertyName.StartsWith("set_") || propertyName.StartsWith("put_"))
{
propertyName = propertyName.Substring("get_".Length);
}
return propertyName;
}
通知方法作相應的修改如下。
protected void RaisePropertyChangedEvent(/*此屬性在.net 4.5以下不支持將其注釋即可*/[System.Runtime.CompilerServices.CallerMemberName] string propertyName = "")
{
if (propertyName == "")
{
propertyName = GetCallerMemberName();
}
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
注:在.net4.5以下只能調用GetCallerMemberName,使用者覺得影響性能則直接使用參數(shù)賦值即可。
完整代碼
public abstract class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChangedEvent([System.Runtime.CompilerServices.CallerMemberName] string propertyName = "")
{
if (propertyName == "")
{
propertyName = GetCallerMemberName();
}
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
string GetCallerMemberName()
{
StackTrace trace = new StackTrace();
StackFrame frame = trace.GetFrame(2);//1代表上級,2代表上上級,以此類推
var propertyName = frame.GetMethod().Name;
if (propertyName.StartsWith("get_") || propertyName.StartsWith("set_") || propertyName.StartsWith("put_"))
{
propertyName = propertyName.Substring("get_".Length);
}
return propertyName;
}
}使用示例
TimeTick .cs
public class TimeTick : ViewModelBase
{
public double Seconds
{
set { _seconds = value; RaisePropertyChangedEvent(); }
get { return _seconds; }
}
double _seconds;
public TimeTick()
{
DispatcherTimer time = new DispatcherTimer();
time.Tick += Time_Tick;
time.Interval = TimeSpan.FromMilliseconds(1000);
time.Start();
}
private void Time_Tick(object sender, EventArgs e)
{
//修改屬性的值
Seconds++;
}
}MainWindow.xaml.cs
public MainWindow()
{
InitializeComponent();
this.DataContext = new TimeTick();
}
MainWindow.xaml
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="360" Width="640">
<Grid >
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Text="{Binding Seconds}" FontSize="128" Foreground="#999999" Loaded="TextBlock_Loaded" ></TextBlock>
</Grid>
</Window>
顯示效果

到此這篇關于C# wpf定義ViewModelBase進行簡化屬性綁定的文章就介紹到這了,更多相關wpf ViewModelBase簡化屬性綁定內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
DevExpress實現(xiàn)TreeList父子節(jié)點CheckState狀態(tài)同步的方法
這篇文章主要介紹了DevExpress實現(xiàn)TreeList父子節(jié)點CheckState狀態(tài)同步的方法,需要的朋友可以參考下2014-08-08

