尝试读取或写入受保护的内存。这通常表明其他内存已损坏

时间:2017-04-21 09:31:26

标签: c# xaml uwp

我正在为MediaPlayerElement创建CustomMediaTransportControls。我需要一个按钮来创建CompactOverlay框架,所以我将以下代码添加到public sealed class CustomMediaTransportControls : MediaTransportControls

public async void CompactOverlayButton_ClickAsync(object sender, RoutedEventArgs e)
{
    await CoreApplication.CreateNewView().Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
    {
        var frame = new Frame();
        compactViewId = ApplicationView.GetForCurrentView().Id;
        frame.Navigate(typeof(VideoPlayerPage));
        Window.Current.Content = frame;
        Window.Current.Activate();
        ApplicationView.GetForCurrentView().Title = "";
    });
    bool viewShown = await ApplicationViewSwitcher.TryShowAsViewModeAsync(compactViewId, ApplicationViewMode.CompactOverlay);
}

但是当我使用这些代码时,我在XAML部分收到错误消息。 enter image description here 如果我没有使用上述功能,则没有错误。

解决方案:

  1. 在PowerShell中运行netsh winsock reset

  2. 清算解决方案和重建解决方案

  3. 供参考:

    这是我的整个代码

    1. CustomMediaTransportControls.cs - ResourceDictionary
    2. MediaPlayerDictionary.xaml - 来自MediaTransportControls
    3. 的派生类
    4. VideosPage.xaml - 已使用CustomMediaTransportControls的网页

1 个答案:

答案 0 :(得分:0)

不要在类中声明变量compactViewId,而应仅在本地声明它:

p̶u̶b̶l̶i̶c̶ ̶i̶n̶t̶ ̶c̶o̶m̶p̶a̶c̶t̶V̶i̶e̶w̶I̶d̶ ̶=̶ ̶A̶p̶p̶l̶i̶c̶a̶t̶i̶o̶n̶V̶i̶e̶w̶.̶G̶e̶t̶F̶o̶r̶C̶u̶r̶r̶e̶n̶t̶V̶i̶e̶w̶(̶)̶.̶I̶d̶;  /̶/̶   I̶n̶i̶t̶i̶a̶l̶i̶z̶i̶n̶g̶ ̶c̶o̶m̶p̶a̶c̶t̶V̶i̶e̶w̶I̶d̶ ̶t̶o̶ ̶t̶h̶e̶ ̶C̶u̶r̶r̶e̶n̶t̶ ̶V̶i̶e̶w̶ ̶I̶D̶

//Button click event for CompactOverlayButton to Create a Frame in CompactOverlay mode
public async void CompactOverlayButton_ClickAsync(object sender, RoutedEventArgs e)
{
    await CoreApplication.CreateNewView().Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
    {
        var frame = new Frame();
        frame.Navigate(typeof(MainPage));
        Window.Current.Content = frame;
        Window.Current.Activate();
        ApplicationView.GetForCurrentView().Title = "";
    });
    bool viewShown = await ApplicationViewSwitcher.TryShowAsViewModeAsync(ApplicationView.GetForCurrentView().Id, ApplicationViewMode.CompactOverlay);
}