如何为所有 DelegateCommand 和 DelegateCommand调用 RaiseCanExecuteChanged在基本 ViewModel 类中
我正在使用 Prism 和 MVVM 开发 WPF 应用程序。
应用程序的要求之一是能够以不同用户身份登录(具有不同的权限)。
现在大多数权限都是简单的允许或禁止显示特定视图。 所有这些都作为 DelegateCommand
实现,有时作为 DelegateCommand
实现,
如果用户有权显示特定视图,这些命令的 CanExecute 将返回 true。 我还有一个单独的 Sessionmanager 来保存用户信息和权限。
当用户登录时,我使用 EventAggregator 触发一个事件。 在所有 ViewModel 的基类中,我订阅该事件并使用反射 循环遍历 DelegateCommand 类型的 VM 的所有公共属性,并为该命令调用 RaiseCanExecuteChanged。
Type myType = this.GetType();
IList<PropertyInfo> props = new List<PropertyInfo>(myType.GetProperties());
foreach (PropertyInfo prop in props)
{
if (prop.PropertyType == typeof(DelegateCommand))
{
var cmd = (DelegateCommand)prop.GetValue(this, null);
cmd.RasieCanExecuteChanged();
}
}
这适用于所有非泛型 DelegateCommand 属性,但当然不会影响 DelegateCommand
。
我的问题是如何确定该属性的类型为 DelegateCommand
并转换为该特定类型以便能够调用 RasieCanExecuteChanged?
I am developing a WPF application using Prism and MVVM.
One of the requirements of the application is ability to login as different user (with different permissions).
Now most of the permissions are simple allow or disallow display particular view.
All of them are implemented as DelegateCommand
or sometime as DelegateCommand<T>
CanExecute for those commands returns true if user has permission to display particular view.
Also I have a singleton Sessionmanager that holds user information and permissions.
When user logged in I am firing an event using EventAggregator.
in the base Class for all ViewModels I am subscribing to that event and using reflection
loop through all public properties of VM that of type DelegateCommand and call RaiseCanExecuteChanged for that command.
Type myType = this.GetType();
IList<PropertyInfo> props = new List<PropertyInfo>(myType.GetProperties());
foreach (PropertyInfo prop in props)
{
if (prop.PropertyType == typeof(DelegateCommand))
{
var cmd = (DelegateCommand)prop.GetValue(this, null);
cmd.RasieCanExecuteChanged();
}
}
This works well for all non-generics DelegateCommand properties but of course does not affect DelegateCommand<T>
.
My question is How to determine that property is of type DelegateCommand<T>
and cast to that particular type to be able to call RasieCanExecuteChanged?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以检查属性的类型是否派生自
DelegateCommandBase
,如果是,则将其转换为DelegateCommandBase
并调用RaiseCanExecuteChanged
。You can check whether the property's type derives from
DelegateCommandBase
, and if so - cast it toDelegateCommandBase
and callRaiseCanExecuteChanged
.我更喜欢另一种方法,即依靠观察视图模型上的属性变化,然后仅当我监视的属性发生变化时才提高 raisecanexecutechanged ,我在这里发布了它:
https://stackoverflow.com/a/30394333/1716620
感谢您最终会得到这样的命令:
这样您就可以了控制哪个变量可以影响命令,而无需放置大量样板代码或在不需要时调用 RaiseCanExecuteChanged
i prefer another approach, that relay on watching property changes on the viewmodel and then rise the raisecanexecutechanged only when my monitored properties change, i posted about it here:
https://stackoverflow.com/a/30394333/1716620
thanks to that you'll end up having a command like this:
this way you have fine control over which variable can impact on a command, without having to put lot of boilerplate code or call RaiseCanExecuteChanged when not needed