CefSharp v62修改方法(支持.net4.0)
吐槽一下,博客园久了没有上,账号没了,重新申请一个.
cesharpv62版本,内核采用最新的Cef62,支持最新的Grid布局.由于官方的cefsharp采用.net4.5.2开发.怎么办怎么办.我只能用.net4.0.没办法啊,自己拿源码修改兼容呗.
仔细分析源码发现:
1.net4.5.2引入了async/await关键字.这个其实国外大神已经有源码放出来了,我们把代码直接引入cefsharp这个工程.就可以直接在4.0里使用async/await;
2.net4.5对taskapi做了扩展,我们只需要在.net4.0实现一下对应的api.就可以了.
3.源码里面用了很多4.5才有的GetTypeInfo扩展方法错误.它返回的类型是typeinfo,不用管它,把GetTypeInfo删掉.直接Type调用就可以了.
4.对Task静态方法的扩展,需要修改一下,静态方法的调用方式.
以上是要点.下面贴源码:
本段源码是对:async/await的支持:
namespaceSystem.Threading.Tasks
{
publicstaticclassTaskEx
{
publicstaticTaskAwaiterGetAwaiter(thisTasktask)
{
returnnewTaskAwaiter(task);
}
publicstaticTaskAwaiterGetAwaiter(thisTasktask)
{
returnnewTaskAwaiter(task);
}
}
publicstructTaskAwaiter:INotifyCompletion
{
readonlyTasktask;
internalTaskAwaiter(Tasktask)
{
this.task=task;
}
internalstaticTaskSchedulerTaskScheduler
{
get
{
if(SynchronizationContext.Current==null)
returnTaskScheduler.Default;
else
returnTaskScheduler.FromCurrentSynchronizationContext();
}
}
publicboolIsCompleted
{
get{returntask.IsCompleted;}
}
publicvoidOnCompleted(Actioncontinuation)
{
this.task.ContinueWith(
delegate(Tasktask){
continuation();
},TaskAwaiter.TaskScheduler);
}
publicvoidGetResult()
{
try
{
task.Wait();
}
catch(AggregateExceptionex)
{
throwex.InnerExceptions[0];
}
}
}
publicstructTaskAwaiter:INotifyCompletion
{
readonlyTasktask;
internalTaskAwaiter(Tasktask)
{
this.task=task;
}
publicboolIsCompleted
{
get{returntask.IsCompleted;}
}
publicvoidOnCompleted(Actioncontinuation)
{
this.task.ContinueWith(
delegate(Tasktask){
continuation();
},TaskAwaiter.TaskScheduler);
}
publicTGetResult()
{
try
{
returntask.Result;
}
catch(AggregateExceptionex)
{
throwex.InnerExceptions[0];
}
}
}
}
namespaceSystem.Runtime.CompilerServices
{
publicinterfaceINotifyCompletion
{
voidOnCompleted(Actioncontinuation);
}
publicinterfaceICriticalNotifyCompletion:INotifyCompletion
{
[SecurityCritical]
voidUnsafeOnCompleted(Actioncontinuation);
}
publicinterfaceIAsyncStateMachine
{
voidMoveNext();
voidSetStateMachine(IAsyncStateMachinestateMachine);
}
publicstructAsyncVoidMethodBuilder
{
publicstaticAsyncVoidMethodBuilderCreate()
{
returnnewAsyncVoidMethodBuilder();
}
publicvoidSetException(Exceptionexception)
{
throwexception;
}
publicvoidSetResult()
{
}
publicvoidSetStateMachine(IAsyncStateMachinestateMachine)
{
//Shouldnotgetcalledaswedon'timplementtheoptimizationthatthismethodisusedfor.
thrownewNotImplementedException();
}
publicvoidStart(refTStateMachinestateMachine)whereTStateMachine:IAsyncStateMachine
{
stateMachine.MoveNext();
}
publicvoidAwaitOnCompleted(refTAwaiterawaiter,refTStateMachinestateMachine)whereTAwaiter:INotifyCompletionwhereTStateMachine:IAsyncStateMachine
{
awaiter.OnCompleted(stateMachine.MoveNext);
}
publicvoidAwaitUnsafeOnCompleted(refTAwaiterawaiter,refTStateMachinestateMachine)whereTAwaiter:ICriticalNotifyCompletionwhereTStateMachine:IAsyncStateMachine
{
awaiter.OnCompleted(stateMachine.MoveNext);
}
}
publicstructAsyncTaskMethodBuilder
{
TaskCompletionSource
这段是对Task的扩展
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Threading;
usingSystem.Threading.Tasks;
namespaceCefSharp
{
publicclassTaskEx
{
publicstaticTaskFromResult(Tt)
{
returnTask.Factory.StartNew(()=>t);
}
publicstaticTaskRun(Actionaction)
{
vartcs=newTaskCompletionSource();
newThread(()=>{
try
{
action();
tcs.SetResult(null);
}
catch(Exceptionex)
{
tcs.SetException(ex);
}
})
{IsBackground=true}.Start();
returntcs.Task;
}
publicstaticTaskRun(Funcfunction)
{
vartcs=newTaskCompletionSource();
newThread(()=>
{
try
{
tcs.SetResult(function());
}
catch(Exceptionex)
{
tcs.SetException(ex);
}
})
{IsBackground=true}.Start();
returntcs.Task;
}
publicstaticTaskDelay(intmilliseconds)
{
vartcs=newTaskCompletionSource();
vartimer=newSystem.Timers.Timer(milliseconds){AutoReset=false};
timer.Elapsed+=delegate{timer.Dispose();tcs.SetResult(null);};
timer.Start();
returntcs.Task;
}
}
}
把在C#内添加以上代码里,遇到Task.Run的时候,替换成TaskEx.Run,遇到Task.Delay替换为TaskEx.Delay.
还有报GetTypeInfo这个错误的地方,删之就Ok了。
以上这篇CefSharpv62修改方法(支持.net4.0)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。