大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
这篇文章将为大家详细讲解有关CefSharp v62修改的示例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
创新互联公司10多年成都定制网页设计服务;为您提供网站建设,网站制作,网页设计及高端网站定制服务,成都定制网页设计及推广,对成都花箱等多个行业拥有丰富的网站维护经验的网站建设公司。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 TaskAwaiterGetAwaiter (this Task task) { return new TaskAwaiter (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 : INotifyCompletion { readonly Task task; internal TaskAwaiter(Task task) { this.task = task; } public bool IsCompleted { get { return task.IsCompleted; } } public void OnCompleted(Action continuation) { this.task.ContinueWith( delegate (Task 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 (ref TStateMachine stateMachine) where TStateMachine : IAsyncStateMachine { stateMachine.MoveNext(); } public void AwaitOnCompleted (ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : INotifyCompletion where TStateMachine : IAsyncStateMachine { awaiter.OnCompleted(stateMachine.MoveNext); } public void AwaitUnsafeOnCompleted (ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : ICriticalNotifyCompletion where TStateMachine : IAsyncStateMachine { awaiter.OnCompleted(stateMachine.MoveNext); } } public struct AsyncTaskMethodBuilder { TaskCompletionSource
这段是对 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 TaskFromResult (T t) { return Task.Factory.StartNew (() => t); } public static Task Run(Action action) { var tcs = new TaskCompletionSource (); new Thread(() => { try { action(); tcs.SetResult(null); } catch (Exception ex) { tcs.SetException(ex); } }) { IsBackground = true }.Start(); return tcs.Task; } public static Task Run (Func function) { var tcs = new TaskCompletionSource (); 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 (); 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修改的示例分析”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。