码迷,mamicode.com
首页 > 移动开发 > 详细

xamarin.form 使用WebView和APP交互

时间:2018-11-30 20:17:06      阅读:1671      评论:0      收藏:0      [点我收藏+]

标签:screens   iat   ict   main   sha   inactive   forms   change   mina   

  <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交互

标签:screens   iat   ict   main   sha   inactive   forms   change   mina   

原文地址:https://www.cnblogs.com/jiecaoge/p/10045830.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!