无法使Webview可编辑

时间:2016-10-04 07:27:54

标签: javascript webview uwp contenteditable

我正在尝试将 webview 控制转换为可编辑模式(对于UWP)。这是我的代码,我注入了js来执行此操作:

private const string EditableParameter = "~editable~";
private const string SetBodyEditableScript = @"
        try
        {  
            document.body.contentEditable = '" + EditableParameter + @"';
        }
        catch(e)
        {

        }";

public MainPage()
{
    this.InitializeComponent();
    MakeWebviewEditable();
}

private const string EventNotificationFormat = @"window.external.notify('{0}');";
private async void MakeWebviewEditable()
{
    await InjectJavaScriptAsync(SetBodyEditableScript.Replace(EditableParameter, "true"));
}

private async Task InjectJavaScriptAsync(string jscript)
        {
            await WebView.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () =>
            {
                try
                {                        
                        string result = await WebView.InvokeScriptAsync("eval", new string[] { jscript });
                }
                catch (Exception ex)
                {

                }
            });
        }

但它不起作用且webview控件仍然不是editable

1 个答案:

答案 0 :(得分:0)

事实证明这很简单。只需将designmode设置为true:

document.designMode='on';