吐槽一下,博客园久了没有上,账号没了,重新申请一个.
cesharp v62版本,内核采用最新的Cef 62,支持最新的Grid布局. 由于官方的cefsharp 采用.net4.5.2开发.怎么办怎么办.我只能用.net4.0.没办法啊,自己拿源码修改兼容呗.
仔细分析源码发现:
1.net4.5.2 引入了 async/await 关键字. 这个其实国外大神已经有源码放出来了,我们把代码直接引入cefsharp 这个工程. 就可以直接在4.0里使用 async/await;
2.net4.5 对task api 做了扩展, 我们只需要在.net4.0实现一下对应的api.就可以了.
3. 源码里面用了很多4.5才有的GetTypeInfo 扩展方法错误. 它返回的类型是typeinfo,不用管它,把GetTypeInfo 删掉. 直接Type 调用就可以了.
4. 对Task静态方法的扩展,需要修改一下,静态方法的调用方式.
以上是要点.下面贴源码:
本段源码是对:async/await的支持:
1 namespace System.Threading.Tasks 2 { 3 public static class TaskEx 4 { 5 public static TaskAwaiter GetAwaiter(this Task task) 6 { 7 return new TaskAwaiter(task); 8 } 9 10 public static TaskAwaiterGetAwaiter (this Task task) 11 { 12 return new TaskAwaiter (task); 13 } 14 } 15 16 public struct TaskAwaiter : INotifyCompletion 17 { 18 readonly Task task; 19 20 internal TaskAwaiter(Task task) 21 { 22 this.task = task; 23 } 24 25 internal static TaskScheduler TaskScheduler 26 { 27 get 28 { 29 if (SynchronizationContext.Current == null) 30 return TaskScheduler.Default; 31 else 32 return TaskScheduler.FromCurrentSynchronizationContext(); 33 } 34 } 35 36 public bool IsCompleted 37 { 38 get { return task.IsCompleted; } 39 } 40 41 public void OnCompleted(Action continuation) 42 { 43 this.task.ContinueWith( 44 delegate (Task task) { 45 continuation(); 46 }, TaskAwaiter.TaskScheduler); 47 } 48 49 public void GetResult() 50 { 51 try 52 { 53 task.Wait(); 54 } 55 catch (AggregateException ex) 56 { 57 throw ex.InnerExceptions[0]; 58 } 59 } 60 } 61 62 public struct TaskAwaiter : INotifyCompletion 63 { 64 readonly Task task; 65 66 internal TaskAwaiter(Task task) 67 { 68 this.task = task; 69 } 70 71 public bool IsCompleted 72 { 73 get { return task.IsCompleted; } 74 } 75 76 public void OnCompleted(Action continuation) 77 { 78 this.task.ContinueWith( 79 delegate (Task task) { 80 continuation(); 81 }, TaskAwaiter.TaskScheduler); 82 } 83 84 public T GetResult() 85 { 86 try 87 { 88 return task.Result; 89 } 90 catch (AggregateException ex) 91 { 92 throw ex.InnerExceptions[0]; 93 } 94 } 95 } 96 } 97 98 namespace System.Runtime.CompilerServices 99 {100 public interface INotifyCompletion101 {102 void OnCompleted(Action continuation);103 }104 105 public interface ICriticalNotifyCompletion : INotifyCompletion106 {107 [SecurityCritical]108 void UnsafeOnCompleted(Action continuation);109 }110 111 public interface IAsyncStateMachine112 {113 void MoveNext();114 void SetStateMachine(IAsyncStateMachine stateMachine);115 }116 117 public struct AsyncVoidMethodBuilder118 {119 public static AsyncVoidMethodBuilder Create()120 {121 return new AsyncVoidMethodBuilder();122 }123 124 public void SetException(Exception exception)125 {126 throw exception;127 }128 129 public void SetResult()130 {131 }132 133 public void SetStateMachine(IAsyncStateMachine stateMachine)134 {135 // Should not get called as we don't implement the optimization that this method is used for.136 throw new NotImplementedException();137 }138 139 public void Start (ref TStateMachine stateMachine) where TStateMachine : IAsyncStateMachine140 {141 stateMachine.MoveNext();142 }143 144 public void AwaitOnCompleted (ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : INotifyCompletion where TStateMachine : IAsyncStateMachine145 {146 awaiter.OnCompleted(stateMachine.MoveNext);147 }148 149 public void AwaitUnsafeOnCompleted (ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : ICriticalNotifyCompletion where TStateMachine : IAsyncStateMachine150 {151 awaiter.OnCompleted(stateMachine.MoveNext);152 }153 }154 155 public struct AsyncTaskMethodBuilder156 {157 TaskCompletionSource
这段是对 Task 的扩展
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading; 6 using System.Threading.Tasks; 7 8 namespace CefSharp 9 {10 public class TaskEx11 {12 public static TaskFromResult (T t)13 {14 return Task.Factory.StartNew (() => t);15 }16 public static Task Run(Action action)17 {18 var tcs = new TaskCompletionSource ();19 new Thread(() => {20 try21 {22 action();23 tcs.SetResult(null);24 }25 catch (Exception ex)26 {27 tcs.SetException(ex);28 }29 })30 { IsBackground = true }.Start();31 return tcs.Task;32 }33 public static Task Run (Func function)34 {35 var tcs = new TaskCompletionSource ();36 new Thread(() =>37 {38 try39 {40 tcs.SetResult(function());41 }42 catch (Exception ex)43 {44 tcs.SetException(ex);45 }46 })47 { IsBackground = true }.Start();48 return tcs.Task;49 }50 public static Task Delay(int milliseconds)51 {52 var tcs = new TaskCompletionSource ();53 var timer = new System.Timers.Timer(milliseconds) { AutoReset = false };54 timer.Elapsed += delegate { timer.Dispose(); tcs.SetResult(null); };55 timer.Start();56 return tcs.Task;57 }58 }59 }
把在C#内添加以上代码里, 遇到 Task.Run 的时候,替换成 TaskEx.Run,遇到 Task.Delay 替换为TaskEx.Delay.
还有报 GetTypeInfo 这个错误的地方,删之就Ok了,
祝您顺利!