【问题标题】:uwp cursor hide when media is playinguwp 光标在媒体播放时隐藏
【发布时间】:2021-07-30 22:02:14
【问题描述】:

媒体开始播放几秒钟后,我试图隐藏鼠标指针。到目前为止,我已经尝试了以下应该完成这项工作的方法,但遗憾的是抛出了一个空引用异常。

Window.Current.CoreWindow.PointerCursor = null;  

项目最小目标:创作者更新

目标:秋季创作者更新。

用于媒体的控件:mediaplayerelement。

【问题讨论】:

    标签: uwp cursor media nullreferenceexception uwp-xaml


    【解决方案1】:

    到目前为止,我已经尝试了以下应该完成这项工作的方法,但遗憾的是抛出了一个空引用异常。

    问题是你没有在 UI 线程中调用这个方法。请尝试将PointerCursor 设置为null,如下所示。

    await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
    {
        Windows.UI.Xaml.Window.Current.CoreWindow.PointerCursor = null;
    });
    

    【讨论】:

      【解决方案2】:

      我向你建议我以前隐藏/取消隐藏鼠标光标的方式:

      Window.Current.CoreWindow.PointerMoved += CoreWindow_PointerMoved;
      Window.Current.CoreWindow.PointerCursor = null;
      
      private async void CoreWindow_PointerMoved(Windows.UI.Core.CoreWindow sender, PointerEventArgs args)
      {
          if (Window.Current.CoreWindow.PointerCursor == null)
              Window.Current.CoreWindow.PointerCursor = new CoreCursor(CoreCursorType.Arrow, 0);
          Stopwatch.Restart();
          await Task.Delay(1500);
          if (Stopwatch == null) return;
          if (Stopwatch.ElapsedMilliseconds >= 1500)
              Window.Current.CoreWindow.PointerCursor = null;
      }
      

      在这段代码中,我添加了一个全局 StopWatch 属性

      Stopwatch Stopwatch { get; set; } = new Stopwatch();

      您应该在某个地方处理秒表并取消注册诸如 OnNavigatedFrom 之类的事件

      Window.Current.CoreWindow.PointerMoved -= CoreWindow_PointerMoved;
      Stopwatch.Stop();
      Stopwatch = null;
      Window.Current.CoreWindow.PointerCursor = new CoreCursor(CoreCursorType.Arrow, 0);
      AppCore.Instance.MediaPlayer.Pause();
      Frame.GoBack();
      NavigationManager.BackRequested -= NavigationManager_BackRequested;
      NavigationManager.AppViewBackButtonVisibility = AppViewBackButtonVisibility.Collapsed;
      NavigationManager = null;
      
      

      【讨论】:

        猜你喜欢
        • 2011-11-05
        • 2017-04-05
        • 2012-02-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多