C# 如何在Xamarin';Android的Webview?
我正在通过xamarin Studio学习一些C#for android。我学习了有关微软文档的教程。在我添加OnkeyDown()方法之前,一切都很正常。编译器找不到web\u视图,但它就在上面 我怎样才能解决这个问题C# 如何在Xamarin';Android的Webview?,c#,visual-studio,xamarin,webview,C#,Visual Studio,Xamarin,Webview,我正在通过xamarin Studio学习一些C#for android。我学习了有关微软文档的教程。在我添加OnkeyDown()方法之前,一切都很正常。编译器找不到web\u视图,但它就在上面 我怎样才能解决这个问题 using System; using Android.App; using Android.Content; using Android.Runtime; using Android.Views; using Android.Webkit; using Android.Wid
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Webkit;
using Android.Widget;
using Android.OS;
using theWedding.Views;
using theWedding.Models;
namespace theWedding
{
[Activity(Label = "theWedding", MainLauncher = true)]
public class MainActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
var web_view = FindViewById<WebView>(Resource.Id.webview);
web_view.Settings.JavaScriptEnabled = true;
web_view.SetWebViewClient(new TheWeddingClient());
web_view.LoadUrl("https://patahapa.com/apps");
}
public class TheWeddingClient : WebViewClient
{
//for android <7
//public override bool ShouldOverrideUrlLoading(WebView view, string url)
//{
// view.LoadUrl(url);
// return false;
//}
//for android 7+
public override bool ShouldOverrideUrlLoading(WebView view, IWebResourceRequest request)
{
view.LoadUrl(request.Url.ToString());
return false;
}
}
public override bool OnKeyDown(Android.Views.Keycode keyCode, Android.Views.KeyEvent e)
{
if (keyCode == Keycode.Back && web_view.CanGoBack())
{
web_view.GoBack();
return true;
}
return base.OnKeyDown(keyCode, e);
}
}
}
使用系统;
使用Android.App;
使用Android.Content;
使用Android.Runtime;
使用Android.Views;
使用Android.Webkit;
使用Android.Widget;
使用Android.OS;
使用婚礼视图;
使用婚礼模型;
婚礼名称空间
{
[活动(Label=“theWedding”,MainLauncher=true)]
公共课活动:活动
{
创建时受保护的覆盖无效(捆绑包)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
var web_view=findviewbyd(Resource.Id.webview);
web_view.Settings.JavaScriptEnabled=true;
设置WebViewClient(新的TheWeddingClient());
web_view.LoadUrl(“https://patahapa.com/apps");
}
公共类TheWeddingClient:WebViewClient
{
//对于android,因为您在OnCreate
中声明webview,所以它的范围仅限于OnCreate
。这是基本的C范围
如果您改为在类级别声明webview,它将在类中的任何位置都可用
// declare at the class level
WebView webview;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
web_view = FindViewById<WebView>(Resource.Id.webview);
web_view.Settings.JavaScriptEnabled = true;
web_view.SetWebViewClient(new TheWeddingClient());
web_view.LoadUrl("https://patahapa.com/apps");
}
//在类级别声明
网络视图;
创建时受保护的覆盖无效(捆绑包)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
web_view=findviewbyd(Resource.Id.webview);
web_view.Settings.JavaScriptEnabled=true;
设置WebViewClient(新的TheWeddingClient());
web_view.LoadUrl(“https://patahapa.com/apps");
}
Okay@Jason它工作得很好..这是WebView web#view;谢谢。我希望能更深入地理解C#for android。