xamarin.form 使用WebView和APP交互

Posted jiecaoge

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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交互的主要内容,如果未能解决你的问题,请参考以下文章

Xamarin.Form 蓝牙ble

Xamarin.Form与Xamarin.Android或Xamarin.IOS的区别简述

xamarin.form 添加拍照功能

如何在 Xamarin.Form 应用程序中使用 text 属性为项目中的所有控件全局设置 FontFamily?

xamarin.form创建项目和打包

是否可以使用 xamarin.form 在我的应用程序中打开另一个应用程序?