xamarin.form 使用WebView和APP交互

弱龄寄事外,委怀在琴书。这篇文章主要讲述xamarin.form 使用WebView和APP交互相关的知识,希望能为你提供帮助。

< WebView Source="https://www.baidu.com"> < /WebView>

可以用后台代码和webview互动,例如webview.Eval("alert(‘hello‘)");
xamarin.form原生webview有很多bug,建议使用第三方的view控件HybirdWebView
如果要使用这个view,需要安装XLabs.Serialization.JSON,和XLabs.Forms
然后在ios中将appdelegate修改为
// The UIApplicationDelegate for the application. This class is responsible for launching the // User Interface of the application, as well as listening (and optionally responding) to // application events from iOS. [Register("AppDelegate")] public partial class AppDelegate : XLabs.Forms.XFormsApplicationDelegate { // // This method is invoked when the application has loaded and is ready to run. In this // method you should instantiate the window, load the UI into it and then make the window // visible. // // You have 17 seconds to return from this method, or iOS will terminate your application. // public override bool FinishedLaunching(UIApplication app, NSDictionary options) { SetIoc(); global::Xamarin.Forms.Forms.Init(); LoadApplication(new App()); return base.FinishedLaunching(app, options); } private void SetIoc() { var resolverContainer = new SimpleContainer(); resolverContainer.Register< IJsonSerializer, JsonSerializer> (); Resolver.SetResolver(resolverContainer.GetResolver()); } }

在android中修改mainactive修改为
[Activity(Label = "App2", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)] public class MainActivity : XFormsApplicationDroid { protected override void OnCreate(Bundle savedInstanceState) { //TabLayoutResource = Resource.Layout.Tabbar; //ToolbarResource = Resource.Layout.Toolbar; SetIoc(); base.OnCreate(savedInstanceState); global::Xamarin.Forms.Forms.Init(this, savedInstanceState); LoadApplication(new App()); } private void SetIoc() { var resolverContainer= new SimpleContainer(); resolverContainer.Register< IJsonSerializer, JsonSerializer> (); Resolver.SetResolver(resolverContainer.GetResolver()); } }

LoadFinished事件加载完成事件
CallJsFunction("alert","你好");
在html中加入代码注册按钮回掉后台代码
function Callback(){ Native("datacallback","this is callback paramer"); }

然后在后台代码中加入
webView.RegisterCallback("datacallback",t=> {T就是参数});

【xamarin.form 使用WebView和APP交互】

    推荐阅读