我嘗試LaunchUriAsync當我的應用程序使用此代碼開始:LaunchUriAsync定製方案時,Windows 8的Metro應用開始
/// <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 ");
}
}
我嘗試在App.xaml.cs使用這個「OnLaunched」的方法。
這適用於很多URI,但通過附加到http://tozon.info/blog/post/2011/10/06/Windows-8-Metro-declarations-Protocol.aspx我不能在應用程序啓動時將其用於具有自定義方案的URI。這會凍結UI。但是,當應用程序已經啓動,我把這個功能設置爲一個按鈕,這爲自定義方案工作。 我不知道爲什麼這不起作用的應用程序啓動