2014-01-15 4 views
5

私はこのWindows Store Appで作業しています。このアプリケーションを使用してファイルを開くときにOpen Withをサポートします。この絵が示すように、アプリケーションマニフェストファイルの宣言セクションにフォーマットをサポート:OpenWithを動作させるためにApp.csのOnFileActivatedイベントをオーバーライドする方法

enter image description here

そして、次のように私はApp.xaml.csファイルにOnFileActivatedイベントをオーバーライド:

protected override void OnFileActivated(FileActivatedEventArgs args) 
     { 
      if (args.Files.Count > 1) 
      { 
       MessageDialog messageDialog1 = new MessageDialog("DirectTouchPlayer can't open many files, the first file will be opened."); 
       messageDialog1.ShowAsync(); 
      } 
      OpenedMediaFile = (StorageFile)args.Files.First(); 
      MessageDialog messageDialog2 = new MessageDialog(OpenedMediaFile.Path + " " + " : Opened !"); 
      messageDialog2.ShowAsync(); 
     } 

これをイベントが発砲していないようですOpenWithを.MP4ファイルで実行すると、アプリケーションが起動しますが、スプラッシュ画面を終了できません。このようなイベントをデバッグしようとしました。thread について説明しますが、OnFileActivatedメソッドでマークされたブレークポイントでデバッガが停止しません。助けて。

アップデート1: OpenWithファイルに、アプリは非常にスプラッシュスクリーンできないことをした後、私は

enter image description here

今、このメッセージボックスエラーを抱えているアップデート2 :

protected override async void OnFileActivated(FileActivatedEventArgs args) 
     { 
      var loadMediaFileTask = new Task<IStorageItem>(() => 
                  { 
                   return args.Files.First(); 
                  }); 
      OpenedFile = await loadMediaFileTask; 
     } 

私はこのようなことに私のOnFileActivatedイベントハンドラを変更しました

OpenedFileがある場合:私はOnLaunchedイベントのナビゲーションを通じてパラメータとして渡す

public static IStorageItem OpenedFile { get; set; } 

if (rootFrame.Content == null) 
      { 
       // When the navigation stack isn't restored navigate to the first page, 
       // configuring the new page by passing required information as a navigation 
       // parameter 
       rootFrame.Navigate(typeof(PlayerPageView), OpenedFile); 
      } 

そしてPlayerPageViewはのMediaElementでビデオを消費しますページです

このアップデートもすべて動作しません。私は何をすべきか理解していません。私に事を明確にすれば幸いです。

+0

アプリに例外がスローされているようです。イベントビューアーを調べて、イベントビューアーの前にイベントがスローされているかどうかを確認します。ロード時だけであれば、ファイルが完了するまでバックグラウンドスレッドにファイルをロードする必要があるかもしれません(大規模である場合があります)。 UIをすばやく取得し、準備ができるまで読み込み画面を表示するためにできることはすべて実行してください。 –

+0

@NateDiamondあなたの助けに感謝し、私のコードを更新しました。私の質問はうまくいきません。私の更新をチェックアウトしてください。 – AymenDaoudi

答えて

6

OnLaunchedは、アプリがユーザーによって正常に開かれたときに呼び出されるメソッドであるため、呼び出されない可能性があります。 OnFileActivatedメソッドの内部にあるOnLaunchedの内部にある可能性のあるロジックとほとんど同じことをする必要があります。

は、彼らが提供するサンプルを約OnLaunchedこのノートを持っています

/// <summary> 
/// Invoked when the application is launched normally by the end user. Other entry points 
/// will be used when the application is launched to open a specific file, to display 
/// search results, and so forth. 
/// </summary> 
/// <param name="args">Details about the launch request and process.</param> 

ここsample version that Microsoft provides in their Windows 8.1 App Sample for Launching by Associationです:

/// <summary> 
// Handle file activations. 
/// </summary> 
protected override async void OnFileActivated(FileActivatedEventArgs args) 
{ 
    Frame rootFrame = Window.Current.Content as Frame; 
    // Do not repeat app initialization when the Window already has content, 
    // just ensure that the window is active 
    if (rootFrame == null) 
    { 
     // Create a Frame to act as the navigation context and navigate to the first page 
     rootFrame = new Frame(); 
     // Associate the frame with a SuspensionManager key 
     SuspensionManager.RegisterFrame(rootFrame, "AppFrame"); 
     if (args.PreviousExecutionState == ApplicationExecutionState.Terminated) 
     { 
      // Restore the saved session state only when appropriate 
      try 
      { 
       await SuspensionManager.RestoreAsync(); 
      } 
      catch (SuspensionManagerException) 
      { 
       //Something went wrong restoring state. 
       //Assume there is no state and continue 
      } 
     } 

     // Place the frame in the current Window 
     Window.Current.Content = rootFrame; 
    } 

    if (rootFrame.Content == null) 
    { 
     if (!rootFrame.Navigate(typeof(MainPage))) 
     { 
      throw new Exception("Failed to create initial page"); 
     } 
    } 

    var p = rootFrame.Content as MainPage; 
    p.FileEvent = args; 
    p.ProtocolEvent = null; 
    p.NavigateToFilePage(); 

    // Ensure the current window is active 
    Window.Current.Activate(); 
} 

私はできるだけこの限りエミュレート示唆しています。

もう1つ注目すべきことは、Activatedイベントでは、私はMessageDialogsのポップアップを提案していないことです。 UIスレッドが空いていると確信している場合は、到着したページ(それはExtendedSplashの画面さえも)に保存してください。

これは役立ち、幸せなコーディングに役立ちます。

+0

Thx @Nate Diamondが動作します。それはOnLoadedイベントで行われるはずの情報を渡されたMainPageを取得すること、それは本当に助けられた – AymenDaoudi

関連する問題