2015-03-31 51 views
0

我使用PickSingleFileAndContinue()方法來選擇圖片並恢復到我的應用程序。在overrided OnActivated()我叫RestoreAsync(),並且調用ContinueFileOpenPicker()後,從ContinuationManager類:ContinuationManager恢復凍結WP8.1

var settingsPage = SimpleIoc.Default.GetInstance<SettingsViewModel>(); 
if (settingsPage != null && args is FileOpenPickerContinuationEventArgs) 
{ 
    settingsPage.ContinueFileOpenPicker(args as FileOpenPickerContinuationEventArgs); 
} 

要調試應用程序,我現在用的信息,從這個頁面:https://msdn.microsoft.com/en-us/library/dn631755.aspx

從供應商選擇的圖像後,應用程序使用StorageFile對象的正確參數成功調用ContinueFileOpenPicker,當我繼續一步一步完成時,在ViewModel的構造函數的最後一個方法處我無法繼續調試,因爲應用程序和有時VS2013凍結。我可以阻止並向下滑動應用程序,但無論需要等待應用程序。之後,應用程序崩潰。請,我無法捕捉異常...幫助。 。:(

+0

我tryed重複從OnLaunched()的所有代碼,它工作正常...當OnActivated()被調用時,也許rootFrame是空的......但是我應該重複OnLaunched的代碼嗎? – sahap 2015-03-31 16:04:21

回答

1

SettingsViewModel要繼承IFileOpenPickerContinuable

public class SettingsViewModel : Screen, IFileOpenPickerContinuable 

幀與查看相關的一些情況不是視圖模型 因此,你應該添加自定義方法,這個工作:

加入ContinuationManager.cs

internal void Continue(IContinuationActivatedEventArgs args, IFileOpenPickerContinuable filepickerPage) 
{ 
    if (args == null) 
     throw new ArgumentNullException("args"); 

    if (this.args != null && !handled) 
     throw new InvalidOperationException("Can't set args more than once"); 

    this.args = args; 
    this.handled = false; 
    this.id = Guid.NewGuid(); 

    if (wabPage == null) 
     return; 

    switch (args.Kind) 
    { 
     case ActivationKind.PickFileContinuation: 
      if (filepickerPage != null) 
      { 
       filepickerPage.ContinueFileOpenPicker(args as FileOpenPickerContinuationEventArgs); 
      } 
      break; 

     case ActivationKind.PickSaveFileContinuation: 
      break; 

     case ActivationKind.PickFolderContinuation: 
      break; 

     case ActivationKind.WebAuthenticationBrokerContinuation: 
      break; 
    } 
} 

確保是從

var settingsPage = SimpleIoc.Default.GetInstance<SettingsViewModel>(); 

返回SettingsViewModel是同一個實例那個叫PickSingleFileAndContinue,否則將無法正常工作,將繼續暫停,並等待着什麼返回控制。

然後在App.xaml.cs您可以添加:

protected override void OnActivated(IActivatedEventArgs e) 
{ 
     base.OnActivated(e); 
     // Add all of the Frame code 

     var continuationEventArgs = e as IContinuationActivatedEventArgs; 
     continuationManager = new ContinuationManager(); 
     SettingsViewModel settingsPage = SimpleIoc.Default.GetInstance<SettingsViewModel>(); 

     if (continuationEventArgs != null) 
     { 
      continuationManager.Continue(continuationEventArgs, settingsPage); 
     } 
    } 

但我要重複代碼OnLaunched?

沒有,只有OnActivate代碼應該叫外,其餘應保持原樣(但你可以做任何你想要的)