| | |
| | | using System.Collections.Generic; |
| | | using System; |
| | | using System.Runtime; |
| | | using WebKit; |
| | | |
| | | namespace Shared |
| | | { |
| | | /// <summary> |
| | | /// Button 按键 |
| | | /// MyEchartsView |
| | | /// 2020-06-19 UIWebView替换为WKWebView |
| | | /// UIWebView已经弃用,后面再使用的APP将不能发布上架 |
| | | /// </summary> |
| | | public class MyEchartsView : View |
| | | { |
| | | JsonData jsonData = new JsonData { }; |
| | | UIWebView uIWebView; |
| | | WKWebView uIWebView; |
| | | public MyEchartsView() |
| | | { |
| | | uIWebView = new UIWebView { }; |
| | | //默认布局,宽高为0的话会异常 |
| | | var mFrame = new CoreGraphics.CGRect(base.X, base.Y, 10, 10); |
| | | uIWebView = new WKWebView(mFrame, new WKWebViewConfiguration()) { }; |
| | | uiView = uIWebView; |
| | | uIWebView.ScrollView.Bounces = false; |
| | | uIWebView.ScrollView.ScrollEnabled = false; |
| | | uIWebView.NavigationDelegate = new OnWKNavigationDelegate(this); |
| | | var filePath = NSBundle.MainBundle.PathForResource("echarts", "html"); |
| | | var url = new NSUrl(filePath); |
| | | var request = new NSUrlRequest(url); |
| | | uIWebView.LoadRequest(request); |
| | | uIWebView.LoadFinished += (sender, e) => { |
| | | uIWebView.EvaluateJavascript($"loadEcharts({Newtonsoft.Json.JsonConvert.SerializeObject(jsonData)},'undefined')"); |
| | | }; |
| | | var url = new NSUrl(filePath, false); |
| | | uIWebView.LoadFileUrl(url, url); |
| | | } |
| | | |
| | | public void Show(string title, string[] xArray, object[] yArray) |
| | | { |
| | | jsonData.Title = title; |
| | | jsonData.XArray = xArray; |
| | | jsonData.YArray = yArray; |
| | | uIWebView.EvaluateJavascript($"loadEcharts({Newtonsoft.Json.JsonConvert.SerializeObject(jsonData)},'undefined')"); |
| | | EvaluateJavascript(); |
| | | } |
| | | ///Users/hdl/MyFiles/My/Xamarin/MyApp/Library/Shared.IOS/UI/MyEchartsView.cs(30,30): Error CS0012: The type 'Attribute' is defined in an assembly that is not referenced.You must add a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. (CS0012) (Shared.IOS) |
| | | ///Users/hdl/MyFiles/My/Xamarin/MyApp/Demo/IOS/MTOUCH: Error MT2101: Can't resolve the reference 'System.Reflection.Emit.ILGenerator System.Reflection.Emit.DynamicMethod::GetILGenerator()', referenced from the method 'Newtonsoft.Json.Serialization.ObjectConstructor`1<System.Object> Newtonsoft.Json.Utilities.DynamicReflectionDelegateFactory::CreateParameterizedConstructor(System.Reflection.MethodBase)' in 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. (MT2101) (Demo.IOS) |
| | | ///Users/hdl/MyFiles/My/Xamarin/MyApp/Demo/IOS/MTOUCH: Error MT2101: Can't resolve the reference 'System.Reflection.Emit.ILGenerator System.Reflection.Emit.DynamicMethod::GetILGenerator()', referenced from the method 'Newtonsoft.Json.Serialization.ObjectConstructor`1<System.Object> Newtonsoft.Json.Utilities.DynamicReflectionDelegateFactory::CreateParameterizedConstructor(System.Reflection.MethodBase)' in 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. (MT2101) (Demo.IOS) |
| | | ///Users/hdl/MyFiles/My/Xamarin/MyApp/Demo/IOS/MTOUCH: Error MT2101: Can't resolve the reference 'System.Reflection.Emit.ILGenerator System.Reflection.Emit.DynamicMethod::GetILGenerator()', referenced from the method 'Newtonsoft.Json.Serialization.ObjectConstructor`1<System.Object> Newtonsoft.Json.Utilities.DynamicReflectionDelegateFactory::CreateParameterizedConstructor(System.Reflection.MethodBase)' in 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. (MT2101) (Demo.IOS) |
| | | |
| | | public string Unit |
| | | { |
| | | get |
| | |
| | | } |
| | | } |
| | | |
| | | |
| | | public void EvaluateJavascript() |
| | | { |
| | | WKJavascriptEvaluationResult handler = (NSObject result, NSError err) => { |
| | | if (err != null) |
| | | { |
| | | HDLUtils.WriteLine("EvaluateJavascript:" + err); |
| | | } |
| | | if (result != null) |
| | | { |
| | | HDLUtils.WriteLine("EvaluateJavascript:" + result); |
| | | } |
| | | }; |
| | | uIWebView.EvaluateJavaScript($"loadEcharts({Newtonsoft.Json.JsonConvert.SerializeObject(jsonData)},'undefined')", handler); |
| | | |
| | | } |
| | | |
| | | |
| | | public class OnWKNavigationDelegate : WKNavigationDelegate |
| | | { |
| | | [Weak] MyEchartsView _MyEchartsView; |
| | | |
| | | public OnWKNavigationDelegate(MyEchartsView view) |
| | | { |
| | | _MyEchartsView = view; |
| | | } |
| | | |
| | | |
| | | public override void DidFinishNavigation(WKWebView webView, WKNavigation navigation) |
| | | { |
| | | Console.WriteLine("WKWebView DidFinishNavigation"); |
| | | _MyEchartsView.EvaluateJavascript(); |
| | | } |
| | | } |
| | | |
| | | |
| | | [Serializable] |
| | | class JsonData |
| | | { |