关闭用户与 UWP 表单中的 Web 视图的交互



我正在使用Windows.UI.Xaml.Controls.WebView来查看一些Web内容。我想关闭所有用户与组件的交互。没有键盘事件,没有鼠标事件。我怎样才能做到这一点?

我尝试取消 GettingFocus 事件,但我在 Web 视图中运行的测试脚本仍然收到鼠标点击。

这是我测试过的代码的略微修改版本。 daWebView是一个WebView组件。

public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
this.RequiresPointer = RequiresPointer.Never;
Application.Current.RequiresPointerMode = ApplicationRequiresPointerMode.WhenRequested;
bool res = daWebView.Focus(FocusState.Unfocused);
daWebView.GettingFocus += DaWebView_GettingFocus;
daWebView.PointerMoved += DaWebView_PointerMoved;
Uri uri = new Uri("ms-appx-web:///index.html");
daWebView.Source = uri;
}
private void DaWebView_PointerMoved(object sender, PointerRoutedEventArgs e)
{
// never happens.
Debug.WriteLine("pointer moved");
}
private void DaWebView_GettingFocus(UIElement sender, GettingFocusEventArgs args)
{
// not working, it still gets focus and receive mouse movement.
args.Cancel = true;
}
}

关闭用户与 UWP 表单中的 Web 视图的交互

没有这样的 API 可以直接禁用与 WebView 的交互,更好的方法是使用 js 注入禁用 WebView 内容。您可以使用InvokeScriptAsync方法注入 js 方法来禁用交互。有关更多信息,请参阅此文档。

最新更新