从视图模型执行的UI代码上MVVMCross [英] Executing UI Code from ViewModel on MVVMCross
问题描述
我刚开始使用MvvmCross,但我没有找到有关如何我可以从一个视图模型执行的UI代码的任何信息。
I have just started using MvvmCross, but i didn't find any info about to how i can execute UI code from a ViewModel.
在卡利有协程这样我就可以访问视图,并保持从视图模型代码中分离出来的UI代码。我的第一个案例
我需要从一个ViewModel内命令打开dialow,什么是正确的方法是什么?
On Caliburn there are coroutine so i can access the view and keep the ui code separated from the viewmodel code. on my first case i need to open a dialow from a command inside a ViewModel, what is the correct way?
现在我正在开发的WinRT应用程序。
Right now i'm developing a WinRT app.
感谢
推荐答案
有没有任何硬/在这个快速发展规则中MvvmCross。
There isn't any hard/fast rule on this within MvvmCross.
通常,当我需要做的这一点,我使用Messenger的插件。
Generally, when I need to do this I use the Messenger plugin.
这个答案假定您使用的是最新的Alpha V3
代码。对于较旧的vNext代码,就必须做一些翻译 - 看下面的注解
This answer assumes you are using the latest Alpha v3
code. For older vNext code you'll have to do some translation - see notes below.
要使用这种方式:
我引用 Cirrious.MvvmCross.Plugins.Messenger.dll
从核心和UI的项目。
I reference Cirrious.MvvmCross.Plugins.Messenger.dll
from both Core and UI projects.
然后
Cirrious.MvvmCross.Plugins.Messenger.PluginLoader.Instance.EnsureLoaded();
然后在核心项目我添加了一个消息:
Then in the Core project I add a message:
public class InputIsNeededMessage : MvxMessage
{
public InputIsNeededMessage(object sender) : base(sender) {}
}
在视图模型,我可以通过构造函数注入或获得的使者:
In the ViewModel I can get the Messenger by constructor injection or by:
var messenger = Mvx.Resolve<IMvxMessenger>();
和我可以通过调用发送消息:
and I can send messages by calling:
messenger.Publish(new InputIsNeededMessage(this));
在查看我可以再次去报信,并使用订阅消息:
In the View I can again get to the messenger and subscribe to messages using:
var messenger = Mvx.Resolve<IMvxMessenger>();
_token = messenger.SubscribeOnMainThread<InputIsNeededMessage>(OnInputIsNeeded);
其中, _token
必须是一个成员变量 - 如果不是则认购事项将不会继续存在 - 认购本身的弱默认(所以你从来没有退订)
where _token
must be a member variable - if it isn't then the subscription won't persist - the subscription itself is weak by default (so you never have to unsubscribe)
和其中 OnInputIsNeeded
是这样的:
private void OnInputIsNeeded(InputIsNeededMessage message)
{
if (message.Sender != ViewModel)
return;
// do stuff here - you are already on the UI thread
}
上面的顺序是什么,我通常为正确的代码
The above sequence is what I normally do for 'proper code'
要开始使用Messenger的做/ EventAggregator能感觉到不舒服 - 它肯定花了一段时间来习惯它 - 但之后,我也习惯了它,那么我现在在任何地方使用它 - 发布/订阅的消息去耦是用于测试和将来的维护非常灵活代码(IMO)
To start with using a Messenger/EventAggregator can feel uncomfortable - it certainly took me a while to get used to it - but after I did get used to it, then I now use it everywhere - the pub/sub Message decoupling is very flexible for testing and for future maintenance of code (IMO)
作为替代这种方法上面我有时会走捷径:
As alternatives to this approach above I do sometimes take shortcuts:
- 有时我火从视图模型普通的C#的活动,并有这些
- 查看回应,有时我有特殊标记的属性,并从他们开火UI代码
对不起,使用 V3
语法 - 但转换为未来,这就是我现在编码...
Sorry for using v3
syntax - but the changeover is coming and it's what I'm now coding in...
要切换回 vNext
我想你可能需要:
To switch back to vNext
I think you might need to:
- 使用
IMessenger的
而不是IMvxMessenger
- 使用
BaseMessage
而不是MvxMessage
- 使用
订阅
而不是SubscribeOnMainThread $ C $的c> - 但是你需要马歇尔消息到UI线程自己。
- use
IMessenger
instead ofIMvxMessenger
- use
BaseMessage
instead of theMvxMessage
- use
Subscribe
instead ofSubscribeOnMainThread
- but then you will need to marshall the message onto the UI thread yourself.
这篇关于从视图模型执行的UI代码上MVVMCross的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!