MvvmCross:如何将Android上下文传递给MvxCommand? [英] MvvmCross: How to pass Android context down to MvxCommand?

查看:87
本文介绍了MvvmCross:如何将Android上下文传递给MvxCommand?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个服务界面:

I have created a service interface:

public interface IMessageDialogService
{
    void ShowDialog(object context,string title,string message,string buttonTitle);
}

我已经在Android和iOS上实现了该接口.该上下文仅在需要Android上下文才能显示消息对话框的Android上使用.我将此接口传递给ViewModel以便由IoC注入.我的问题是在与平台无关的ViewModel中,该模型调用WebService,然后处理返回值.它检查错误值的返回值,并需要显示一个消息对话框. iOS不需要任何上下文即可显示UIAlertView,但是在Android上如何获取要作为第一个参数传递的Android上下文?

I have implemented that interface on both Android and iOS. The context is only used on Android where an Android Context is needed to display a message dialog. I pass this interface into my ViewModel to be injected by IoC. My problem is in my platform independent ViewModel which calls a WebService and then handles the return value. It checks the return value for an error condition and needs to display a message dialog. iOS does not need any context to display a UIAlertView, but on Android how do I get a hold of an Android context to pass in as the first argument?

是否有更简便的方法来显示ViewModel中的简单信息对话框?

Is there an easier way to display a simple informational dialog from a ViewModel?

推荐答案

在检查了WebBrowserTask的源代码之后,看来我总是可以通过以下方式获取当前的Activity:

After inspecting the source for the WebBrowserTask, it looks like I can always grab the current Activity by:

var activity = Mvx.Resolve<IMvxAndroidCurrentTopActivity> ().Activity;

因此我不必将其传递下去,而是让我在Android上的IMessageDialogService实现抓取它并使用它来显示消息对话框.

so I don't have to pass it down, but have my implementation of IMessageDialogService on Android grab it and use it to display the message dialog.

这篇关于MvvmCross:如何将Android上下文传递给MvxCommand?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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