从Appdelegate打开ViewModel [英] Open ViewModel from Appdelegate
问题描述
我正在尝试通过Web-Url在Xamarin.Ios-Project中打开特定视图. url方案工作正常,我还检查了AppDelegate类中的url,如下所示:
I am trying to open a specific view in my Xamarin.Ios-Project via a Web-Url. The url-scheme works fine and i also check the url in the AppDelegate class as shown below:
[Register("AppDelegate")]
public partial class AppDelegate : MvxApplicationDelegate
{
// other methods ...
public override bool OpenUrl(UIApplication application, NSUrl url, string sourceApplication, NSObject annotation)
{
if (url == null) return false;
var uri = new Uri(url.ToString());
if(uri.Host.Equals("myscheme-host"))
{
var param = GetParameters(uri);
// What can i do here?
ShowViewModel<MyViewModel>();
return true;
}
}
}
从这一点上如何显示我的Viewmodel?我试图在静态类中设置一个值,并在下一个视图加载时检查它.但是,当应用程序在后台运行时,用户单击了Url-Scheme-Link,则不会引发任何事件(ViewDidLoad,ViewWillAppear,..).
How can I show my Viewmodel from this point? I tried to set a value in a static class and check it when the next view loads. But when the app is in the background bevore the user clicked the Url-Scheme-Link, no event (ViewDidLoad, ViewWillAppear, ..) will raise.
感谢您的帮助.非常感谢
Any help is appreciated. Thanks a lot
推荐答案
我正在使用自定义方案来启动应用程序(例如myscheme-host:xxxx之类的东西),因此我在OpenUrl上写的内容如下:
I am using a custom scheme to start an application (something like myscheme-host:xxxx) so what I would write on the OpenUrl is something like this:
public override bool OpenUrl(UIApplication application, NSUrl url, string sourceApplication, NSObject annotation)
{
if (url.AbsoluteString.StartsWith("myscheme-host:"))
{
this.window.RootViewController = new MySchemeViewController();
在您的情况下,您可能会根据"xxxx"参数创建一个新的视图模型,并将该视图模型传递给视图控制器(无论是直接从代码中还是通过将视图模型与控制器配对的解析器)
In your case you would probably create a new view model from the "xxxx" parameters and pass that view model to a view controller (whether that is straight from the code or through a resolver that pairs the view model with a controller).
这篇关于从Appdelegate打开ViewModel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!