最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501
当前位置: 首页 - 科技 - 知识百科 - 正文

CefSharp v62修改方法(支持.net4.0)

来源:懂视网 责编:小采 时间:2020-11-27 22:35:16
文档

CefSharp v62修改方法(支持.net4.0)

CefSharp v62修改方法(支持.net4.0):吐槽一下,博客园久了没有上,账号没了,重新申请一个. cesharp v62版本,内核采用最新的Cef 62,支持最新的Grid布局. 由于官方的cefsharp 采用.net4.5.2开发.怎么办怎么办.我只能用.net4.0.没办法啊,自己拿源码修改兼容呗. 仔细分析源码发现: 1.ne
推荐度:
导读CefSharp v62修改方法(支持.net4.0):吐槽一下,博客园久了没有上,账号没了,重新申请一个. cesharp v62版本,内核采用最新的Cef 62,支持最新的Grid布局. 由于官方的cefsharp 采用.net4.5.2开发.怎么办怎么办.我只能用.net4.0.没办法啊,自己拿源码修改兼容呗. 仔细分析源码发现: 1.ne

吐槽一下,博客园久了没有上,账号没了,重新申请一个.

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.

还有报 GetTypeInfo 这个错误的地方,删之就Ok了。

以上这篇CefSharp v62修改方法(支持.net4.0)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

声明:本网页内容旨在传播知识,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文档

CefSharp v62修改方法(支持.net4.0)

CefSharp v62修改方法(支持.net4.0):吐槽一下,博客园久了没有上,账号没了,重新申请一个. cesharp v62版本,内核采用最新的Cef 62,支持最新的Grid布局. 由于官方的cefsharp 采用.net4.5.2开发.怎么办怎么办.我只能用.net4.0.没办法啊,自己拿源码修改兼容呗. 仔细分析源码发现: 1.ne
推荐度:
标签: 修改 ce cefsharp
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top