C# 启动windows 8 metro应用程序时启动自定义方案同步

C# 启动windows 8 metro应用程序时启动自定义方案同步,c#,windows-8,microsoft-metro,uri,C#,Windows 8,Microsoft Metro,Uri,当我的应用程序以以下代码启动时,我尝试启动UriaSync: /// <summary> /// Navigate to the given URI in a web browser task if the uri is valid /// We can use all scheme provide by windows excepted file:/// /// </summary> /// <param name="uri">Uri of the page

当我的应用程序以以下代码启动时,我尝试启动UriaSync:

/// <summary>
/// Navigate to the given URI in a web browser task if the uri is valid
/// We can use all scheme provide by windows excepted file:///
/// </summary>
/// <param name="uri">Uri of the page</param>
public async void TryNavigateToBrowser(string uri)
{
  if (uri != null && uri != "")
  {
    try
    {
      /* Firstly we try to made a launch async for custom URI scheme */
      var success = await Windows.System.Launcher.LaunchUriAsync(new Uri(uri));

      /* Secondly if it doesn't works we try the same but in UI thread
       * If a custom scheme is tried to be displayed in UI thread UI should crash */
      if (!success)
      {
        CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,
        () =>
        {
          /* we made a synchronous call to LaunchUriAsync in UI thread */
          var successUI = Windows.System.Launcher.LaunchUriAsync(new Uri(uri)).AsTask().Result;

          if (!successUI)
          {
            TryNavigateToBrowser(uri);
          }
        });
      }
    }
    catch (Exception)
    {
      /* If URI isn't well formated we try to found a file to launch it */
      NavigateToLocalUri(uri);
    }
  }
}

/// <summary>
/// Use to provide customer sended URI for opening file in app storage
/// </summary>
/// <param name="uri"></param>
internal static async void NavigateToLocalUri(string uri)
{
  try
  {
    StorageFile storageFile = await ApplicationData.Current.LocalFolder.GetFileAsync(uri);
    if (storageFile != null)
    {
      var success = Windows.System.Launcher.LaunchFileAsync(storageFile);
    }
  }
  catch (Exception)
  {
    /* If no file can be displayed we send a capptain error */
    throw new CapptainException("URI cannot be opened ");
  }
}
//
///如果URI有效,则导航到web浏览器任务中的给定URI
///除以下文件外,我们可以使用windows提供的所有方案:///
/// 
///页面的Uri
公共异步void TryNavigateToBrowser(字符串uri)
{
如果(uri!=null&&uri!=“”)
{
尝试
{
/*首先,我们尝试为自定义URI方案创建异步启动*/
var success=wait Windows.System.Launcher.launchurisync(新Uri(Uri));
/*第二,如果它不起作用,我们尝试相同的,但在UI线程
*如果试图在UI中显示自定义方案,则UI线程将崩溃*/
如果(!成功)
{
CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,
() =>
{
/*我们在UI线程中对Launchurisync进行了同步调用*/
var successUI=Windows.System.Launcher.launchurisync(新Uri(Uri)).AsTask().Result;
如果(!successUI)
{
TryNavigateToBrowser(uri);
}
});
}
}
捕获(例外)
{
/*如果URI格式不好,我们会尝试找到一个文件来启动它*/
导航本地uri(uri);
}
}
}
/// 
///用于为在应用程序存储中打开文件提供客户发送的URI
/// 
/// 
内部静态异步void NavigateToLocalUri(字符串uri)
{
尝试
{
StorageFile StorageFile=Wait ApplicationData.Current.LocalFolder.GetFileAsync(uri);
if(storageFile!=null)
{
var success=Windows.System.Launcher.LaunchFileAsync(storageFile);
}
}
捕获(例外)
{
/*如果无法显示任何文件,我们将发送capptain错误*/
抛出新的CapptainException(“无法打开URI”);
}
}
我尝试在App.xaml.cs“OnLaunched”方法中使用此方法

这对很多URI都有效,但当应用程序启动时,通过附加到我不能将其用于自定义方案的URI。这冻结了用户界面。但当应用程序已经启动,我把这个功能设置为一个按钮,这个自定义方案的工作。
我不知道为什么这在app start上不起作用,我知道这是一篇比较老的帖子,你可能已经猜到了,但我想我有一个答案。我以前遇到过类似的事情

如果你的OnActivate和OnProtocolActivate方法与那篇文章中看到的完全相同,那么如果应用程序还没有运行,它将无法工作。这是因为你的应用程序没有框架。如果没有框架,则无需执行任何编程工作来设置要加载的视图。如果应用程序已经在运行,那么就已经有了一个框架可以为您工作,这就是为什么它在已经启动的情况下可以工作

看看新项目附带的OnLaunched代码。如果不存在,它们会创建一个新框架。我相信,如果您只是在OnProtocolActivated方法的开头添加以下代码,那么如果应用程序尚未启动,它应该可以工作

private void OnProtocolActivated(ProtocolActivatedEventArgs args)
{
    Frame rootFrame = Window.Current.Content as Frame;

    if (rootFrame == null)
    {                
        rootFrame = new Frame();
        Window.Current.Content = rootFrame;
    }
/// Your code goes after
希望有帮助