Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
193 views
in Technique[技术] by (71.8m points)

c# - Executing UI Code from ViewModel on MVVMCross

I have just started using MvvmCross, but i didn't find any info about to how i can execute UI code from a ViewModel.

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?

Right now i'm developing a WinRT app.

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There isn't any hard/fast rule on this within MvvmCross.

Generally, when I need to do this I use the Messenger plugin.

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.

To use this approach:

I reference Cirrious.MvvmCross.Plugins.Messenger.dll from both Core and UI projects.

Then I add a line somewhere in Setup.cs (e.g. in InitializeLastChance) to:

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);

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)

and where OnInputIsNeeded is something like:

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'

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:

  • sometimes I fire normal C# events from the ViewModel and have the View respond to these
  • sometimes I have special marker properties and fire the UI code from them

Sorry for using v3 syntax - but the changeover is coming and it's what I'm now coding in...

To switch back to vNext I think you might need to:

  • use IMessenger instead of IMvxMessenger
  • use BaseMessage instead of the MvxMessage
  • use Subscribe instead of SubscribeOnMainThread - but then you will need to marshall the message onto the UI thread yourself.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...