目录
- 概述
- 具体方法
- 完整代码
- 使用示例
概述
绑定机制是wpf的核心,也是界面独立的根本,尤其是使用了mvvm模式,没有业务逻辑的干扰,使得界面得以专注于绚丽效果的实现。在xaml中编写绑定语句后,在业务逻辑层需要定义相应的属性与其绑定,需要继承INotifyPropertyChanged,并通过PropertyChanged通知属性改变。但是每个地方都去继承实现INotifyPropertyChanged还是有点麻烦。
具体方法
1、继承INotifyPropertyChanged
继承INotifyPropeSkwwErPZWrtyChanged后需要定义一个事件。
public event PropertyChangedEventHandler PropertyChanged;
2、定义通知方法
定义一个动作属性改变的方法。这一步骤将2个参数,简化成了一个字符串参数。
protected void RaisePropertyChangedEvent( string propertyName )
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
3、自动获取属性名
调用的时候要写属性名还是有点麻烦,进一步简化,将属性名参数省去。
在.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 prop编程ertyName = 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 javascript= "")
{
if (propertyName == "")
{
propertyName = GetCallerMemberName();
}
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
注:在.net4.5以下只能调用GetCallerMemberName,使用者觉得影响性能则直接使用参数赋值即可。
完整代码
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 http://www.devze.com{ 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="C# wpf定义ViewModelBase进行简化属性绑定" 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简化属性绑定内容请搜索编程客栈(www.devze.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(wSkwwErPZWww.cppcns.com)!
加载中,请稍侯......
精彩评论