如何在MVVMCross WPF应用程序中实现关闭按钮? [英] How to implement Close button in MVVMCross WPF application?

查看:127
本文介绍了如何在MVVMCross WPF应用程序中实现关闭按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 WPF 应用程序中使用 MVVMCross ,我想提供一个 Close (或 Exit )中的按钮关闭整个应用程序.是否有使用 MVVMCross 功能的简单方法?

I am using MVVMCross in my WPF application, and I want to provide a Close (or Exit) button in the GUI to shut-down the entire application. Is there a straightforward way to do this using MVVMCross functionality?

我用MainViewModel尝试了以下方法:

public class MainViewModel : MvxViewModel
{
    private MvxCommand _closeCommand;
    public MvxCommand CloseCommand {
        get { 
            return _closeCommand ?? (_closeCommand = new MvxCommand(DoClose));
        }
    }

    public void DoClose() {
        Close(this);
    }
}

不幸的是,这无济于事.当我按下绑定到CloseCommand的按钮时,会调用DoClose方法,但是即使WPF App StartupUri设置为MainView.

Unfortunately, to no avail. The DoClose method does get called when I press a button that has been bound to CloseCommand, but the view-model (and corresponding view) fail to close, even when the WPF App StartupUri is set to the MainView.

推荐答案

许多MvvmCross平台不支持关闭自身的应用程序-iOS,WindowsPhone,Android,WindowsPhone等的行为对此并不满意-不支持那里有公共API-因此这不是常见的跨平台请求.

Many MvvmCross platforms don't support applications closing themselves - this is frowned upon behaviour in iOS, WindowsPhone, Android, WindowsPhone, etc - it isn't supported by public APIs there - so this isn't a common cross-platform request.

但是,如果您想在支持该功能的平台上实现此功能,那么我认为放置此功能的最佳位置可能是在演示者中或在自定义的UI注入服务中.

However, if you wanted to implement this functionality for those platforms that do support it, then I think perhaps the best place to put this would either be in the presenter or in a custom UI-injected service.

要将其放入自定义演示器中,只需:

To put it in a custom presenter, simply:

  • 创建自定义演示者类(基于
  • create a custom presenter class (based on MvxWpfViewPresenter.cs)
  • return an instance of this class in an override of the CreatePresenter method in your your Setup.cs
  • override the ChangePresentation method in your custom presenter - this method could perhaps use the 'well-known' MvxClosePresentationHint.cs which is what Close sends or it could use a new custom presentation hint you define and send via ChangePresentation(MvxPresentationHint hint) in a ViewModel.

要将此功能改为在某些自定义UI服务中使用,请参见 http://mvvmcross.wordpress.com中的N = 31 /

To put this functionality instead in some custom UI service, see N=31 in http://mvvmcross.wordpress.com/

如果您正在查看此关闭区域,那么您可能还需要考虑关闭应用程序(通过用户或操作系统操作)时应采取的步骤. MvvmCross确实暴露了框架中常见的生命周期监控器"钩子的开始,但是实际上实际上这些钩子是目前非常基本的-因此您可能会发现需要在此处构建自己的公共层.

If you are looking at this shutdown area, then you may also want to consider what steps to take when an application is shutdown (either through user or operating system action). MvvmCross does expose the beginnings of common 'lifetime monitor' hooks within the framework, but actually in practice these are very basic at present - so you may find you need to build your own common layer here.

这篇关于如何在MVVMCross WPF应用程序中实现关闭按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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