xamarin.form 使用WebView和APP交互
阿新 • • 發佈:2018-11-30
user var finished con 使用webview ioc pre res webview
<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交互