C# 表单验证webview中的文本字段以显示iOS本机弹出窗口
我在网上做了一些研究,但没有发现任何能直接回答我问题的东西。有没有办法通过Xamarin.Forms验证WebView表单 在我的具体场景中,我想确保如果WebView中有一个文本字段为空,那么我将显示一个本机iOS警报(DisplayAlert) 我的异步任务如下所示:C# 表单验证webview中的文本字段以显示iOS本机弹出窗口,c#,android,ios,xamarin,xamarin.forms,C#,Android,Ios,Xamarin,Xamarin.forms,我在网上做了一些研究,但没有发现任何能直接回答我问题的东西。有没有办法通过Xamarin.Forms验证WebView表单 在我的具体场景中,我想确保如果WebView中有一个文本字段为空,那么我将显示一个本机iOS警报(DisplayAlert) 我的异步任务如下所示: private async Task<string> FormValidation () { WebView wv = new WebView(); string eleme
private async Task<string> FormValidation ()
{
WebView wv = new WebView();
string elementID = "mobileShipFromCsp1";
string x = await wv.EvaluateJavaScriptAsync($"document.getElementById('{elementID}').value;");
Console.WriteLine("This is x: " + x);
if(x == "")
{
await DisplayAlert("", "This should work", "Ok");
}
Console.WriteLine("This is x: " + x);
return x;
}
专用异步任务FormValidation()
{
WebView wv=新的WebView();
string elementID=“mobileShipFromCsp1”;
字符串x=wait wv.EvaluateJavaScriptAsync($“document.getElementById(“{elementID}”).value;”;
Console.WriteLine(“这是x:+x”);
如果(x==“”)
{
等待DisplayAlert(“,”这应该有效“,”确定“);
}
Console.WriteLine(“这是x:+x”);
返回x;
}
但在提交的新闻发布会上没有显示警告
我无法将submit按钮绑定到此任务,因为xaml中没有按钮(因为我们调用的是webview)
这是我的XAML:
<?xml version="1.0" encoding="UTF-8"?>
<views:WebViewContainerBaseView xmlns="http://xamarin.com/schemas/2014/forms"
<ContentPage.Content>
<StackLayout>
<webviews:WebviewContainer x:Name="shippingView" VerticalOptions="FillAndExpand"
BackgroundColor="white"/>
</StackLayout>
</ContentPage.Content>
</views:WebViewContainerBaseView>
看看MicrosoftDocs(),我假设您需要处理WebView的navigating或navigated属性。这另一个链接可能也有帮助(),虽然我也注意到在你的代码中你实例化了一个新的WebView对象,但它是否真的有适合你的JS代码的html来正确地定位它呢?这是一个很好的问题。这也是我想知道的。在XAML中,Webview的名称是“shippingView”。但是,当我尝试键入shippingView.enclosurejavascriptasync时,它说在这个上下文中找不到enclosureAsyncy。我不确定,但很好。我假设shippingView是webview类型的?如果是,并且在获取其求值函数时仍然存在问题,则可能必须按照这些链接的描述创建这些自定义渲染器。在任务中创建的新webview不是在xaml中使用的(webview:WebviewContainer)。