標籤:handler c# ber mob element mda tca bsp put
- UIView的使用。
載入本地html的方法:
var context = (JSContext)WebView.ValueForKeyPath((NSString)"documentView.webView.mainFrame.javaScriptContext"); context.ExceptionHandler = (JSContext context2, JSValue exception) => { Console.WriteLine("JS exception: {0}", exception); }; //可以使用 js調用 c# 代碼,和安卓一樣,通過使用 myCSharpObject對象調用c#方法 var myExporter = new MyJSExporter(); context[(NSString)"myCSharpObject"] = JSValue.From(myExporter, context); //var nsUrl = new NSUrl("demoTest.html", false); //var nsRequest = NSUrlRequest.FromUrl(nsUrl); NSUrl url = NSBundle.MainBundle.GetUrlForResource("main", "html"); string htmlString = NSString.FromData(NSData.FromUrl(url), NSStringEncoding.UTF8); WebView.LoadHtmlString(htmlString, NSBundle.MainBundle.BundleUrl);
聲明介面
/// <summary> /// js調用c#代碼的方法介面 /// </summary> [Protocol()] interface IMyJSVisibleProtocol : IJSExport { [Export("myFunc")] int MyFunc(); [Export("Arity2:With:")] NSObject Arity2With(NSObject arg1, NSObject arg2); }
實現方法
/// <summary> /// 方法實現 /// </summary> class MyJSExporter : NSObject, IMyJSVisibleProtocol { public int MyFunc() { Console.WriteLine("Called!"); return 42; } public NSObject Arity2With(NSObject arg1, NSObject arg2) { Console.WriteLine("Arity 2 function called with " + arg1 + " " + arg2); return (NSNumber)42; } }
js調用
<script type="text/javascript"> function callXamObject() { // `myCSharpObject` injected into JS context by C# code `context [(NSString) "myCSharpObject"] = JSValue.From (...etc...` alert("123"); var resultCalculatedInCSharp = myCSharpObject.myFunc(); document.getElementById("Output").innerHTML = resultCalculatedInCSharp; } function callArity2Method() { //Note how this is mapped by [Export ("Arity2:With:")] var result = myCSharpObject.Arity2With("foo", "bar"); } </script>
- html中使用的js和css資源,產生使用 BundleResource
vs2017 ios使用曆程