android - Xamarin 表单 WebView - 加载的页面不会触发拖放事件
问题描述
我在 Xamarin 表单页面上有一个 WebView。我正在尝试响应该页面上 javascript 中的拖放事件。以下是示例中使用的页面的静态 HTML:
<html>
<body>
<h2 id='hello'>Hello</h2>
<button onclick='changeToRed()'>Press Me</button>
<p>Drag area</p>
<div class='container' id='List'>
<p class='draggable' draggable='true' ondragstart='changeToGreen()'>One</p>
<p id='two' class='draggable' draggable='true'><button>Two</button></p>
</div>
<script>
(function () {
changeHeadingBg('yellow');
function dragstart_handler(ev) {
changeToGreen();
}
window.addEventListener('DOMContentLoaded', () => {
const element = document.getElementById('two');
element.addEventListener('dragstart', dragstart_handler);
});
})();
function changeHeadingBg(color){
document.getElementById('hello').style.background = color;
}
function changeToRed(){
document.getElementById('hello').style.background = 'red';
}
function changeToGreen(){
document.getElementById('hello').style.background = 'green';
}
</script>
</body>
</html>
如果您将其保存为静态 html 页面并在浏览器中打开它,则两个文本元素都会通过更改元素背景颜色来响应 dragstart 事件(您可以通过单击按钮将其重置)。如果我将相同的 html 加载到 webview 中并尝试与之交互,则不会触发拖动事件。我尝试了各种设置事件的方法,包括页面上当前的两种方法,但这些事件永远不会触发。其他事件,例如按钮的 onclick 处理程序,似乎可以正常触发。谁能指出我正确的方向?
这是我的页面 xaml:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Test.MainPage">
<StackLayout>
<Frame BackgroundColor="#2196F3" Padding="24" CornerRadius="0">
<Label Text="Welcome to Xamarin.Forms!" HorizontalTextAlignment="Center" TextColor="White" FontSize="36"/>
</Frame>
<WebView x:Name="webView" WidthRequest="1000" HeightRequest="1000" />
</StackLayout>
</ContentPage>
这是它背后的代码 - 为嵌入 HTML 的讨厌方式道歉,但这似乎是创建一个自包含示例的最简单方法:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace Test {
public partial class MainPage : ContentPage {
public MainPage() {
InitializeComponent();
var html = new HtmlWebViewSource {
Html = GetHtml()
};
webView.Source = html;
}
private string GetHtml() {
return @"
<html>
<body>
<h2 id='hello'>Hello</h2>
<button onclick='changeToRed()'>Press Me</button>
<div class='container' id='List'>
<p class='draggable' draggable='true' ondragstart='changeToGreen()'>Zero</p>
<p id='two' class='draggable' draggable='true'>One</p>
</div>
<script>
(function () {
changeHeadingBg('yellow');
function dragstart_handler(ev) {
changeToGreen();
}
window.addEventListener('DOMContentLoaded', () => {
const element = document.getElementById('two');
element.addEventListener('dragstart', dragstart_handler);
});
})();
function changeHeadingBg(color){
document.getElementById('hello').style.background = color;
}
function changeToRed(){
document.getElementById('hello').style.background = 'red';
}
function changeToGreen(){
document.getElementById('hello').style.background = 'green';
}
</script>
</body>
</html>
";
}
}
}
解决方案
推荐阅读
- python-3.x - 我们可以一次在单个 lambda 函数中使用 for 循环和 if else 吗?因此它可以在循环内工作时检查值
- python - 并行比较两个拆分的数据列表
- bash - 运行命令以通过 bash 创建目录
- python - ElementClickInterceptedException:元素点击被拦截:
- javascript - 多个 CSS 类的每个元素的等高和窗口大小调整功能
- xml - 如何使用镜头折叠 XML 元素的节点?
- angular - 如何使用 Angular Material 让 mat-sidenav-content 占据 100% 的高度
- python - 使用指定的目标/文件名和超时时间限制将文件保存到磁盘的下载方法,无需先打开文件
- powershell - 多台 PC 上的测试路径输出不是我想要的
- python - 无法根据 url 参数在视图集自定义视图中创建记录