您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關CefSharp v62修改的示例分析,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
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的支持:
namespace System.Threading.Tasks { public static class TaskEx { public static TaskAwaiter GetAwaiter(this Task task) { return new TaskAwaiter(task); } public static TaskAwaiter<T> GetAwaiter<T>(this Task<T> task) { return new TaskAwaiter<T>(task); } } public struct TaskAwaiter : INotifyCompletion { readonly Task task; internal TaskAwaiter(Task task) { this.task = task; } internal static TaskScheduler TaskScheduler { get { if (SynchronizationContext.Current == null) return TaskScheduler.Default; else return TaskScheduler.FromCurrentSynchronizationContext(); } } public bool IsCompleted { get { return task.IsCompleted; } } public void OnCompleted(Action continuation) { this.task.ContinueWith( delegate (Task task) { continuation(); }, TaskAwaiter.TaskScheduler); } public void GetResult() { try { task.Wait(); } catch (AggregateException ex) { throw ex.InnerExceptions[0]; } } } public struct TaskAwaiter<T> : INotifyCompletion { readonly Task<T> task; internal TaskAwaiter(Task<T> task) { this.task = task; } public bool IsCompleted { get { return task.IsCompleted; } } public void OnCompleted(Action continuation) { this.task.ContinueWith( delegate (Task<T> task) { continuation(); }, TaskAwaiter.TaskScheduler); } public T GetResult() { try { return task.Result; } catch (AggregateException ex) { throw ex.InnerExceptions[0]; } } } } namespace System.Runtime.CompilerServices { public interface INotifyCompletion { void OnCompleted(Action continuation); } public interface ICriticalNotifyCompletion : INotifyCompletion { [SecurityCritical] void UnsafeOnCompleted(Action continuation); } public interface IAsyncStateMachine { void MoveNext(); void SetStateMachine(IAsyncStateMachine stateMachine); } public struct AsyncVoidMethodBuilder { public static AsyncVoidMethodBuilder Create() { return new AsyncVoidMethodBuilder(); } public void SetException(Exception exception) { throw exception; } public void SetResult() { } public void SetStateMachine(IAsyncStateMachine stateMachine) { // Should not get called as we don't implement the optimization that this method is used for. throw new NotImplementedException(); } public void Start<TStateMachine>(ref TStateMachine stateMachine) where TStateMachine : IAsyncStateMachine { stateMachine.MoveNext(); } public void AwaitOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : INotifyCompletion where TStateMachine : IAsyncStateMachine { awaiter.OnCompleted(stateMachine.MoveNext); } public void AwaitUnsafeOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : ICriticalNotifyCompletion where TStateMachine : IAsyncStateMachine { awaiter.OnCompleted(stateMachine.MoveNext); } } public struct AsyncTaskMethodBuilder { TaskCompletionSource<object> tcs; public Task Task { get { return tcs.Task; } } public static AsyncTaskMethodBuilder Create() { AsyncTaskMethodBuilder b; b.tcs = new TaskCompletionSource<object>(); return b; } public void Start<TStateMachine>(ref TStateMachine stateMachine) where TStateMachine : IAsyncStateMachine { stateMachine.MoveNext(); } public void SetStateMachine(IAsyncStateMachine stateMachine) { // Should not get called as we don't implement the optimization that this method is used for. throw new NotImplementedException(); } public void AwaitOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : INotifyCompletion where TStateMachine : IAsyncStateMachine { awaiter.OnCompleted(stateMachine.MoveNext); } public void AwaitUnsafeOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : ICriticalNotifyCompletion where TStateMachine : IAsyncStateMachine { awaiter.OnCompleted(stateMachine.MoveNext); } public void SetResult() { tcs.SetResult(null); } public void SetException(Exception exception) { tcs.SetException(exception); } } public struct AsyncTaskMethodBuilder<T> { TaskCompletionSource<T> tcs; public Task<T> Task { get { return tcs.Task; } } public static AsyncTaskMethodBuilder<T> Create() { AsyncTaskMethodBuilder<T> b; b.tcs = new TaskCompletionSource<T>(); return b; } public void Start<TStateMachine>(ref TStateMachine stateMachine) where TStateMachine : IAsyncStateMachine { stateMachine.MoveNext(); } public void SetStateMachine(IAsyncStateMachine stateMachine) { // Should not get called as we don't implement the optimization that this method is used for. throw new NotImplementedException(); } public void AwaitOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : INotifyCompletion where TStateMachine : IAsyncStateMachine { awaiter.OnCompleted(stateMachine.MoveNext); } public void AwaitUnsafeOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : ICriticalNotifyCompletion where TStateMachine : IAsyncStateMachine { AwaitOnCompleted(ref awaiter, ref stateMachine); } public void SetResult(T result) { tcs.SetResult(result); } public void SetException(Exception exception) { tcs.SetException(exception); } } }
這段是對 Task 的擴展
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace CefSharp { public class TaskEx { public static Task<T> FromResult<T>(T t) { return Task.Factory.StartNew<T>(() => t); } public static Task Run(Action action) { var tcs = new TaskCompletionSource<object>(); new Thread(() => { try { action(); tcs.SetResult(null); } catch (Exception ex) { tcs.SetException(ex); } }) { IsBackground = true }.Start(); return tcs.Task; } public static Task<TResult> Run<TResult>(Func<TResult> function) { var tcs = new TaskCompletionSource<TResult>(); new Thread(() => { try { tcs.SetResult(function()); } catch (Exception ex) { tcs.SetException(ex); } }) { IsBackground = true }.Start(); return tcs.Task; } public static Task Delay(int milliseconds) { var tcs = new TaskCompletionSource<object>(); var timer = new System.Timers.Timer(milliseconds) { AutoReset = false }; timer.Elapsed += delegate { timer.Dispose(); tcs.SetResult(null); }; timer.Start(); return tcs.Task; } } }
把在C#內添加以上代碼里, 遇到 Task.Run 的時候,替換成 TaskEx.Run,遇到 Task.Delay 替換為TaskEx.Delay.
關于“CefSharp v62修改的示例分析”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。