PRISM 代理命令 +样本数据

发布于 2024-12-18 19:32:30 字数 657 浏览 2 评论 0原文

我有一个 ViewModel,它使用 DelegateCommand 属性绑定到 ButtonCommand 属性。

问题是我的示例数据不喜欢 DelegateCommand 对象。它抱怨:类型“DelegateCommand”不包含任何可访问的构造函数。此外,唯一公开的属性是IsActive属性。

<local:MyViewModel xmlns:local="clr-namespace:MyNamespace"
                   xmlns:prism="http://www.codeplex.com/prism">
    <local:MyViewModel.Age>47</local:MyViewModel.Age>
    <local:MyViewModel.PurchaseAlcohalCommand>
        <prism:DelegateCommand IsActive="True" />
    </local:MyViewModel.PurchaseAlcohalCommand>
</local:MyViewModel>

I have a ViewModel that uses a DelegateCommand property to bind to a Button's Command property.

The problem is my sample data does not like the DelegateCommand object. It complains that: The type "DelegateCommand" does not include any accessible constructors. Also, the only exposed property is the IsActive property.

<local:MyViewModel xmlns:local="clr-namespace:MyNamespace"
                   xmlns:prism="http://www.codeplex.com/prism">
    <local:MyViewModel.Age>47</local:MyViewModel.Age>
    <local:MyViewModel.PurchaseAlcohalCommand>
        <prism:DelegateCommand IsActive="True" />
    </local:MyViewModel.PurchaseAlcohalCommand>
</local:MyViewModel>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

染柒℉ 2024-12-25 19:32:30

更改视图模型以公开 ICommand 而不是 DelegateCommand。 DelegateCommand只是ICommand的一个实现;如果您稍后想从 MvvmLight 切换到 RelayCommand,则不必关心您的视图和示例数据。

我不确定这是否能解决您的问题,但我怀疑可能会。另外,这只是一个很好的编程实践。

Change your view model to expose an ICommand instead of a DelegateCommand. DelegateCommand is just an implementation of ICommand; if you later want to switch to RelayCommand from MvvmLight your view and your sample data should not have to care.

I'm not sure that this will solve your problem, but I suspect it might. Plus it's just a good programming practice.

深海夜未眠 2024-12-25 19:32:30

根据 DelegateCommand 的设置方式,它在激活时不会执行任何操作。如果这是所需的行为,我的建议是干脆不声明它。 WPF 将优雅地处理绑定到 null ICommand 对象的情况。

或者,如果您需要将其绑定到实例化的 DelegateCommand,则可以对 DelegateCommand 进行子类化以包含无参数构造函数。

如果您希望它绑定到 DelegateCommand 并且希望该 DelegateCommand 在命令被触发时实际某些事情,那就有点麻烦了更复杂。您必须使用我之前提到的子类 DelegateCommand,但您还必须能够在 XAML 中定义委托。我认为那里有一些示例,但我猜它们涉及标记扩展之类的东西以及类似性质的东西。这种方法的投资回报可能会有点低,但你的里程可能会有所不同。

最后一种替代方法是通常的处理方式:在 ViewModel 的构造函数中定义 DelegateCommands

The way your DelegateCommand is setup, it won't do anything when it is activated. If that is the desired behavior, my suggestion would be to simply not declare it. WPF will gracefully handle being bound to a null ICommand object.

Alternatively, if you need it to bind to an instantiated DelegateCommand, you could subclass DelegateCommand to include a parameterless constructor.

If you wanted it to bind to a DelegateCommand and you wanted that DelegateCommand to actually DO something when the command is triggered, that would get a bit more complicated. You would have to use the subclassed DelegateCommand I mentioned before, but you would also have to be able to define a delegate in XAML. I think there are samples out there, but I would guess they involve things like markup extensions and things of those nature. Your return on investment in this approach may be a little low, but your mileage may vary.

One last alternative that is the way this is typically handled: define your DelegateCommands in the constructor of your ViewModel.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文