在 SaveState/LoadState 后无法设置 MediaElement Source [英] MediaElement Source cannot be set after SaveState/LoadState

查看:23
本文介绍了在 SaveState/LoadState 后无法设置 MediaElement Source的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(注意:所有代码都经过了严格的简化.)

在暂停/恢复后未设置 MediaElement 源.设置源后,CurrentState 会快速更改为Closed".

MediaElement source not being set after Suspend/Resume. The CurrentState quickly changes to "Closed" after the source is set.

我正在处理 MediaFailed 事件 — 它不会触发.我也在处理 MediaOpened 事件,该事件也不会触发.

I am handling the MediaFailed event — it doesn't fire. I am also handling the MediaOpened event, which doesn't fire either.

我有以下更新 MediaElement 源的方法.只要应用在暂停后尝试恢复,它就可以很好地工作.

I have the following method which updates the MediaElement's Source. It works really well as long as the app is not trying to resume after having been Suspended.

  private async void UpdateMediaElementSource(object sender, EventArgs e)
  {
     var videoSource = this.DefaultViewModel.CurrentSource; // a string
     var file = await StorageFile.GetFileFromPathAsync(videoSource);
     var videoStream = await file.OpenAsync(FileAccessMode.Read);

     this.videoMediaElement.SetSource(videoStream, file.ContentType);
     // The above line works many times as long as the app is not trying to Resume.
  }

当应用挂起时,它会调用 SaveState 方法:

When the app is Suspended it calls the SaveState method:

  protected async override void SaveState(Dictionary<String, Object> pageState)
  {
     pageState["MediaElementSource"] = this.DefaultViewModel.CurrentSource;

     // I also made the videoStream global so I can dispose it — but no dice.
     this.videoStream.Dispose();
     this.videoStream = null;
  }

当应用恢复时,它会调用 LoadState 方法:

When the app Resumes, it calls the LoadState method:

  protected async override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
  {
     string source = string.Empty;
     if (pageState != null)
     {
        if (pageState.ContainsKey("MediaElementSource"))
        {
           source = (string)pageState["MediaElementSource"];
        }
     }

     var document = PublicationService.GetDocument(this.currentDocumentIdNumber);

     this.DefaultViewModel = new DocumentViewModel(document);
     this.DefaultViewModel.CurrentMarkerSourceChanged += UpdateMediaElementSource;

     if (!string.IsNullOrEmpty(source))
     {
        // This causes the UpdateMediaElementSource() method to run.
        this.DefaultViewModel.CurrentSource = source;
     }
  }

感谢您对此问题的任何帮助.如果您需要更多详细信息,请告诉我.

I appreciate any help on this issue. Please let me know if you need more details.

推荐答案

因此,在将 mediaElement 的 Source 添加到可视化树之前,它已被设置.

So, it turns out that the mediaElement's Source was being set before it was added to the visual tree.

通常,执行此操作时这不是问题:

Usually, this is not an issue when doing this:

mediaElement.Source = whatever;

但是当你这样做时会出现问题:

but it IS an issue when you do this:

mediaElement.SetSource(stream, MimeType);

结论

当您调用 SetSource(...) 时,请确保您的 MediaElement 是 VisualTree 的一部分.

Conclusion

Make sure that your MediaElement is part of the VisualTree when you call SetSource(...).

让我上面的代码工作的一种简单方法是添加一个全局布尔值,一旦 mediaElement.Loaded 事件触发,该布尔值设置为 true.然后,在调用 SetSource() 的代码中,将其包装在 if(_mediaElementLoaded) 块中.

A simple way to get my above code to work is by adding a global bool that is set to true once the mediaElement.Loaded event has fired. Then, inside the code that calls SetSource(), wrap that in an if(_mediaElementLoaded) block.

这篇关于在 SaveState/LoadState 后无法设置 MediaElement Source的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆