龙空技术网

界面控件DevExpress WPF入门指南:MVVM 框架 - BindableBase

慧都科技 190

前言:

此刻小伙伴们对“publicclassvbnet”大约比较注重,朋友们都想要知道一些“publicclassvbnet”的相关文章。那么小编同时在网上汇集了一些对于“publicclassvbnet””的相关内容,希望同学们能喜欢,各位老铁们快快来学习一下吧!

DevExpress Universal Subscription官方最新版免费下载试用,历史版本下载,在线文档和帮助文件下载-慧都网

BindableBase类实现 INotifyPropertyChanged 接口,并提供 API 以最少的编码声明可绑定属性。

可绑定属性

要声明可绑定属性,请执行以下操作:

从 BindableBase 类继承您的 ViewModel;在属性 getter 和 setter 中使用 GetValue 和 SetValue 方法。

C#

using DevExpress.Mvvm;public class ViewModel : BindableBase {public string FirstName {get { return GetValue<string>(nameof(FirstName)); }set { SetValue(value, nameof(FirstName)); }}}

VB.NET

Imports DevExpress.MvvmPublic Class ViewModelInherits BindableBasePublic Property FirstName() As StringGetReturn GetValue(Of String)(NameOf(FirstName))End GetSet(ByVal value As String)SetValue(value, NameOf(FirstName))End SetEnd PropertyEnd Class

BindableBase 类公开使用 CallerMemberNameAttribute 的 GetValue 和 SetValue 重载,您不必将属性名称传递给这些方法的参数,这简化了可绑定属性声明:

C#

using DevExpress.Mvvm;public class ViewModel : BindableBase {public string FirstName {get { return GetValue<string>(); }set { SetValue(value); }}}

VB.NET

Imports DevExpress.MvvmPublic Class ViewModelInherits BindableBasePublic Property FirstName() As StringGetReturn GetValue(Of String)()End GetSet(ByVal value As String)SetValue(value)End SetEnd PropertyEnd Class

注意:CallerMemberNameAttribute 在 .NET Framework 4.5 及更高版本中可用。

属性值存储在内部字典中:

GetValue 方法使用此字典获取属性值;SetValue 方法使用属性名称作为键将属性值存储在字典中。

SetValue 方法返回指示属性是否已更改的 True 或 False 值。 如果为属性设置相同的值,SetValue 方法将返回 False,并且不会发送更改通知。

C#

using DevExpress.Mvvm;public class ViewModel : BindableBase {public string FirstName {get { return GetValue<string>(); }set {if (SetValue(value))NotifyFullNameChanged();else MessageBox.Show("Could not change value!");}}}

VB.NET

Imports DevExpress.MvvmPublic Class ViewModelInherits BindableBasePublic Property FirstName() As StringGetReturn GetValue(Of String)()End GetSet(ByVal value As String)If Not SetValue(value) ThenNotifyFullNameChanged()Else MessageBox.Show("Could not change value!")End IfEnd SetEnd PropertyEnd Class
属性值更改时运行自定义代码

SetValue 方法具有将回调方法作为参数的重载,在更改属性值后调用此回调。

C#

using DevExpress.Mvvm;public class ViewModel : BindableBase {public string FirstName {get { return GetValue<string>(); }set { SetValue(value, changedCallback: OnFirstNameChanged); }}void OnFirstNameChanged() {//...}}

VB.NET

Imports DevExpress.MvvmPublic Class ViewModelInherits BindableBasePublic Property FirstName() As StringGetReturn GetValue(Of String)()End GetSet(ByVal value As String)SetValue(value, changedCallback:= AddressOf OnFirstNameChanged)End SetEnd PropertySub OnFirstNameChanged()'...End SubEnd Class
手动引发 INotifyPropertyChanged.PropertyChanged

如果您需要为特定属性引发 INotifyPropertyChanged.PropertyChanged 事件,请使用 RaisePropertyChanged/RaisePropertiesChanged 方法。

C#

using DevExpress.Mvvm;public class ViewModel : BindableBase {public string FirstName {get { return GetValue<string>(); }set { SetValue(value, changedCallback: NotifyFullNameChanged); }}public string LastName {get { return GetValue<string>(); }set { SetValue(value, changedCallback: NotifyFullNameChanged); }}public string FullName { get { return FirstName + " " + LastName; } }void NotifyFullNameChanged() {RaisePropertyChanged(nameof(FullName));}}

VB.NET

Imports DevExpress.MvvmPublic Class ViewModelInherits BindableBasePublic Property FirstName() As StringGetReturn GetValue(Of String)()End GetSet(ByVal value As String)SetValue(value, changedCallback:= AddressOf NotifyFullNameChanged)End SetEnd PropertyPublic Property LastName() As StringGetReturn GetValue(Of String)()End GetSet(ByVal value As String)SetValue(value, changedCallback:= AddressOf NotifyFullNameChanged)End SetEnd PropertyPublic ReadOnly Property FullName() As StringGetReturn FirstName & " " & LastNameEnd GetEnd PropertyPrivate Sub NotifyFullNameChanged()RaisePropertyChanged(Function() FullName)End SubEnd Class
使用带有支持字段的属性

当一个属性被频繁访问或更新时,应用程序的性能可能会降低,因为属性值存储在字典中。 要提高应用程序的性能,请将属性值存储在支持字段中。

C#

using DevExpress.Mvvm;public class ViewModel : BindableBase {string _FirstName;public string FirstName {get { return _FirstName; }set { SetValue(ref _FirstName, value, changedCallback: NotifyFullNameChanged); }}}VB.NETImports DevExpress.MvvmPublic Class ViewModelInherits BindableBasePrivate _FirstName As StringPublic Property FirstName() As StringGetReturn _FirstNameEnd GetSet(ByVal value As String)SetValue(_FirstName, value, changedCallback:= AddressOf NotifyFullNameChanged)End SetEnd PropertyEnd Class
.NET Framework 4.0 中的 BindableBase

可绑定属性

使用 GetProperty 和 SetProperty 方法来实现可绑定属性。

C#

using DevExpress.Mvvm;public class ViewModel : BindableBase {public string FirstName {get { return GetProperty(() => FirstName); }set { SetProperty(() => FirstName, value); }}}

VB.NET

Imports DevExpress.MvvmPublic Class ViewModelInherits BindableBasePublic Property FirstName As StringGetReturn GetProperty(Function() FirstName)End GetSet(value As String)SetProperty(Function() FirstName, value)End SetEnd PropertyEnd Class

GetProperty 和 SetProperty 方法的第一个参数是一个 lambda 表达式,它返回用于标识目标属性名称的属性。 属性名称是使用静态 BindableBase.GetPropertyName<T> 方法在内部获取的。

属性值更改时运行自定义代码

SetProperty 方法返回指示属性是否已更改的 True 或 False 值。如果将该属性设置为相同的值,SetProperty 方法将返回 False,并且不会发送更改通知。 SetProperty 方法具有将回调方法作为参数的重载,此回调在字段更改后调用。

C#

using DevExpress.Mvvm;public class ViewModel : BindableBase {public string FirstName {get { return GetProperty(() => FirstName); }set { SetProperty(() => FirstName, value, OnFirstNameChanged); }}void OnFirstNameChanged() {//...}}

VB.NET

Imports DevExpress.MvvmPublic Class ViewModelInherits BindableBasePublic Property FirstName As StringGetReturn GetProperty(Function() FirstName)End GetSet(value As String)SetProperty(Function() FirstName, value, AddressOf OnFirstNameChanged)End SetEnd PropertySub OnFirstNameChanged()'...End SubEnd Class

手动引发 INotifyPropertyChanged.PropertyChanged

如果您需要为特定属性引发 INotifyPropertyChanged.PropertyChanged 事件,请使用 RaisePropertyChanged/RaisePropertiesChanged 方法。

C#

using DevExpress.Mvvm;public class ViewModel : BindableBase {public string FullName {get { return string.Format("{0} {1}", FirstName, LastName); }}public string FirstName {get { return GetProperty(() => FirstName); }set { SetProperty(() => FirstName, value, OnFirstNameChanged); }}public string LastName {get { return GetProperty(() => LastName); }set {if(SetProperty(() => LastName, value))RaisePropertyChanged(() => FullName);}}void OnFirstNameChanged() {RaisePropertyChanged(() => FullName);}}

VB.NET

Imports DevExpress.MvvmPublic Class ViewModelInherits BindableBasePublic ReadOnly Property FullName As StringGetReturn String.Format("{0} {1}", FirstName, LastName)End GetEnd PropertyPublic Property FirstName As StringGetReturn GetProperty(Function() FirstName)End GetSet(value As String)SetProperty(Function() FirstName, value, AddressOf OnFirstNameChanged)End SetEnd PropertyPublic Property LastName As StringGetReturn GetProperty(Function() LastName)End GetSet(value As String)If SetProperty(Function() LastName, value) ThenRaisePropertyChanged(Function() FullName)End IfEnd SetEnd PropertySub OnFirstNameChanged()RaisePropertyChanged(Function() FullName)End SubEnd Class

使用带有支持字段的属性

当属性被频繁更新时(由于从 lambda 表达式计算属性名称并将属性值存储在字典中),应用程序性能可能会降低。 要适应这些情况,请使用存储变量来存储属性,并使用 BindableBase.GetPropertyName<T> 方法从静态构造函数计算一次属性名称。

C#

using DevExpress.Mvvm;public class ViewModel : BindableBase {static string Property1Name;static ViewModel() {Property1Name = BindableBase.GetPropertyName(() => new ViewModel().Property1);}string property1;public string Property1 {get { return property1; }set { SetProperty(ref property1, value, Property1Name); }}}

VB.NET

Imports DevExpress.MvvmPublic Class ViewModelInherits BindableBaseShared Property1Name As StringShared Sub New()Property1Name = BindableBase.GetPropertyName(Function() New ViewModel().Property1)End SubDim _Property1 As StringPublic Property Property1 As StringGetReturn _Property1End GetSet(value As String)SetProperty(_Property1, value, Property1Name)End SetEnd PropertyEnd Class

DevExpress WPF

DevExpress WPF拥有120+个控件和库,将帮助您交付满足甚至超出企业需求的高性能业务应用程序。通过DevExpress WPF能创建有着强大互动功能的XAML基础应用程序,这些应用程序专注于当代客户的需求和构建未来新一代支持触摸的解决方案。 无论是Office办公软件的衍伸产品,还是以数据为中心的商业智能产品,都能通过DevExpress WPF控件来实现。

标签: #publicclassvbnet