ContinuationManager恢复冻结WP8.1

时间:2015-03-31 15:29:28

标签: c# windows-runtime windows-phone-8.1

我正在使用PickSingleFileAndContinue()方法来挑选图片并恢复到我的应用程序。在重写的OnActivated()中,我调用RestoreAsync(),然后从ContinuationManager类调用ContinueFileOpenPicker():

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冻结。我可以阻止和向下滑动应用程序,但无论需要等待应用程序。之后,应用程序崩溃了。拜托,我不能抓住异常......帮助。 :(

1 个答案:

答案 0 :(得分:1)

您的SettingsViewModel应该继承IFileOpenPickerContinuable

public class SettingsViewModel : Screen, IFileOpenPickerContinuable

在某些情况下,框架与View而不是ViewModel相关联。 因此,您应该为此添加自定义方法:

添加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;
    }
}

确保从

返回的SettingsViewModel
var settingsPage = SimpleIoc.Default.GetInstance<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代码,其余部分应保持不变(但你可以做任何你想做的事)