使用Xamarin.Forms和MvvmCross UWP从另一个应用程序打开应用程序

时间:2020-06-16 14:24:35

标签: xamarin mvvmcross

可以通过以下方式打开另一个应用程序:

public async Task<bool> LaunchUriAsync(string objectNumber)
{
  var option = new LauncherOptions();
  option.UI.PreferredPlacement = Placement.Right;
  option.DesiredRemainingView = ViewSizePreference.UseMore;
  return await Launcher.LaunchUriAsync(buildObjectAccessUri(objectNumber), option);
}


private static Uri buildObjectAccessUri(string objectId)
{
  return new Uri(String.Format("{0}://{1}/{2}", "myapp", "data", objectId), UriKind.Absolute);
}

在我执行的命令中:

await MyLauncherService.LaunchUriAsync("whatever");

在打开的应用程序中,我有一个自定义的开始

public class CustomAppStart : MvxAppStart
{
    protected override async Task NavigateToFirstViewModel(object hint = null)
    {
        // hint here is type of `Windows.ApplicationModel.Activation.ProtocolActivatedEventArgs`
    }
}

似乎对象hint的类型为Windows.ApplicationModel.Activation.ProtocolActivatedEventArgs

UWP项目的App.xaml.cs

sealed partial class App
{
    public App()
    {
      InitializeComponent();
    }  
}

public abstract class UwpApp : MvxApplication<Setup<Core.App>, Core.App>
{
    private ILoggerService mLoggerService;
    public UwpApp()
    {
      UnhandledException += (sender, args) =>
      {
        mLoggerService = Mvx.IoCProvider.Resolve<ILoggerService>();
        if (mLoggerService == null)
        {
          return;
        }
        mLoggerService.Fatal("Unhandled exception!", args.Exception);
      };

      DebugSettings.BindingFailed += (sender, args) => Debug.WriteLine(args.Message);
      TaskScheduler.UnobservedTaskException += (sender, args) =>
      {
        mLoggerService = Mvx.IoCProvider.Resolve<ILoggerService>();
        if (mLoggerService == null)
        {
          return;
        }
        foreach (var ex in args.Exception.Flatten().InnerExceptions)
        {
          mLoggerService.Error(ex.Message + " StackTrace:" + ex.StackTrace);
        }
        args.SetObserved();
      };
      Suspending += OnSuspending;
      ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.Auto;
    }
    protected override void OnSuspending(object sender, SuspendingEventArgs e)
    {
      var deferral = e.SuspendingOperation.GetDeferral();
      deferral.Complete();
    }
}

我为什么得到那个?

我应该是字符串...

错误在哪里?

我正在使用最新版本的xamarin和mvvm cross

1 个答案:

答案 0 :(得分:0)

在您的MvxApplication中,您可以覆盖GetAppStartHint来处理进入该方法的对象并按预期返回字符串。

类似的东西:

public abstract class UwpApp : MvxApplication<Setup<Core.App>, Core.App>
{
    protected override object GetAppStartHint(object hint = null)
    {
        // process hint
        //return string
    }
}