MvxCommand中间的UI操作 [英] UI action in middle of MvxCommand

查看:110
本文介绍了MvxCommand中间的UI操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MvvmCross,但这可能是常规命令绑定.

I am using MvvmCross, but this may be general command binding.

当用户单击按钮时,应用程序需要额外的输入数据,然后才能继续执行我想在实际命令中执行的操作.我无法在ViewModel的中间调用UI操作的问题,因此仅绑定MvxCommand(或任何ICommand)将无法工作.

When user click a button, the application require an extra input data before proceed to what I want to do in the actual command. The problem that I cannot call an UI action in middle of ViewModel, so just binding MvxCommand (or any ICommand) would not work.

一个人可能会问为什么: 1)我没有在用户界面上输入任何内容,用户可以在单击按钮之前输入数据->我没有空间. 2)设置默认数据,然后让用户稍后进行更改->虽然这是我的第一个操作,但用户往往忘记稍后进行更改!

One may ask why: 1) I don't put an input on the UI and user can enter data before click button -> I don't have space. 2) Make default data, and let user change it later -> This my first though, but user tend to forget to change it later!!

那么有人可以提出解决方案吗?我唯一能想到的就是忘记命令绑定,并在后面的代码中弹出ui以获取更多数据,然后在视图模型中调用方法!

So can someone come up with a solution? The only thing I can think of is forgetting command binding, and have code behind pop the ui for extra data, then call a method in view model!

谢谢

推荐答案

有几种方法可以做到这一点.

There are several ways to do this.

我个人首选的方式是使用交互请求",这是我从Microsoft模式和实践的Prism框架中学到的.

My personal preferred way is to use an "Interaction Request" - something that I learnt from the Prism framework from Microsoft patterns and practices.

在Mvx中,可以使用ViewModel上的IMvxInteraction属性来执行此操作. https://github中显示了一个示例. com/slodge/BindingTalk/blob/master/BindingTalk.Core/ViewModels/QuestionViewModel.cs

In Mvx, you can do this using an IMvxInteraction property on your ViewModel. An example of this is shown in https://github.com/slodge/BindingTalk/blob/master/BindingTalk.Core/ViewModels/QuestionViewModel.cs

每次请求交互时,ViewModel都会为View提供一个对象-在本例中为YesNoQuestion:

Each time an interaction is requested, the ViewModel provides an object to the View - in this case a YesNoQuestion:

public class YesNoQuestion
{
    public Action YesAction { get; set; }
    public Action NoAction { get; set; }
    public string QuestionText { get; set; }

    public YesNoQuestion()
    {
        YesAction = () => { };
        NoAction = () => { };
    }
}

ViewModel使用IMvxInteraction<TQuestion>属性公开请求者:

The ViewModel exposes the requester using an IMvxInteraction<TQuestion> property:

public class QuestionViewModel
    : MvxViewModel
{
    private MvxInteraction<YesNoQuestion> _confirm = new MvxInteraction<YesNoQuestion>();
    public IMvxInteraction<YesNoQuestion> Confirm
    {
        get { return _confirm; }
    }

    public IMvxCommand GoCommand
    {
        get
        {
            return new MvxCommand(() =>
                {
                    var question = new YesNoQuestion()
                        {
                            QuestionText = "Close me now?",
                            YesAction = () => Close(this),
                        };
                    _confirm.Raise(question);
                });
        }
    }
}

然后,每个平台上的视图都可以绑定并订阅交互请求属性.这有点奇怪-因为它使用弱引用来防止内存泄漏-特别是在iOS上,但在其他平台上也可能.

The view on each platform can then bind and subscribe to the interaction request property. This is a little fiddly - because it uses weak references to prevent memory leaks - especially on iOS, but also possible on other platforms too.

为此提供了一些示例Droid代码:

Some example Droid code for this is in:

  • https://github.com/slodge/BindingTalk/blob/master/BindingTalk.Droid/Views/2%20%20More%20Controls/QuestionView.cs
  • with AXML in https://github.com/slodge/BindingTalk/blob/master/BindingTalk.Droid/Resources/Layout/QuestionView.axml

很抱歉,这里的ConfirmationViewQuestionView名称混乱-第一个是Android View,第二个是Mvvm View和Android Activity.

Sorry for the confusing ConfirmationView and QuestionView names here - the first is an Android View, the second is an Mvvm View and an Android Activity.

另外,请注意,在Android中实现对话框时,您需要注意屏幕旋转-因为Android的Activity生命周期可能会使这里的事情混乱-最简单的机制(我发现)是您自己处理屏幕旋转,而不是允许Android处理它.

Also, please note that when implementing Dialogs in Android, then you need to be careful about screen rotation - as Android's Activity lifecycle can very much confuse things here - easiest mecahnism (I find) is to just handle screen rotation yourself rather than allowing Android to handle it.

这篇关于MvxCommand中间的UI操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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