在MVVM中调用UserControl的方法 [英] Calling a Method of a UserControl in MVVM

查看:213
本文介绍了在MVVM中调用UserControl的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在调用UserControl上的方法时遇到问题.听我说:

I'm having an issue with calling a method on a UserControl. Hear me out:

  1. 我在SomeView.xaml中有一个UserControl someControl

SomeView.xaml的DataContext是SomeViewModel.cs

SomeView.xaml's DataContext is SomeViewModel.cs

我希望能够以某种方式在某处致电someControl.DoStuff().

I want to be able to call someControl.DoStuff() somehow, somewhere.

DoStuff不是特定于UI的(如果它是特定于UI的,我可以从SomeView.Xaml.Cs的代码背后调用DoStuff,但在这种情况下,可能不是.)

DoStuff is not UI specific (I could have just called DoStuff from the code-behind of SomeView.Xaml.Cs if it was UI specific, but in this case, it may not be.)

有什么想法吗?

谢谢!

推荐答案

您可能不会喜欢这个答案,但是您的ViewModel应该不了解您的UI.如果您的UserControl上有非UI方法,则可能在错误的位置.

You're probably not going to like the answer, but your ViewModel should have no knowledge of your UI. If you have a non-UI method on your UserControl, it's probably in the wrong place.

我唯一想到的是您可能想要实现某种类型的消息传递(例如 MVVM Light中的消息传递)可能触发执行.

The only thing I could think of is that you may want to implement some type of messaging (like they have in MVVM Light) that could trigger the execution.

是那样,或者是重新考虑如何设计代码.

It's either that, or rethink how you've architected your code.

这篇关于在MVVM中调用UserControl的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆