CefSharp初识--把网页移到桌面的神器
在开发中我们可曾有过这样的需求,将某个网页嵌入到.Net应用中来,但Winform自带的web browser不怎么理想。CefSharp可以让我们在.Net应用中嵌入一个Chromium。它提供了WPF和Winform版的web browser 控件,能很好的渲染出HTML5效果而且和宿主程序有很强的交互能力。 git地址: https://github.com/cefsharp/CefSharp 。
在WPF中使用
在Nugget中输入CefSharp,找到CefSharp.WPF 并按照到工程中。
cefsharp不支持anycup,还需要设置一下目标平台为x86或x64. 具体请移步: http://www.cnblogs.com/yuefei/p/4123597.html
渲染效果
加入一个css3的动画: 转动的风车 。 元素结构还是很清晰,但动画效果还是没有浏览器流畅。
交互方法
cefsharp支持JavaScript和C#方法相互调用。首先需要注册一个绑定对象:
private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
{
var wb = new ChromiumWebBrowser
{
Address = @"file:///D:/VS2012/Support/Main/Portal/Presentation/Portal.Client/Resources/BindingTest.html"
};
wb.RegisterJsObject("bound", new BoundObject());
WBGrid.Children.Add(wb);
}
BoundObject:
public class BoundObject
{
public int MyProperty { get; set; }
public string MyReadOnlyProperty { get; internal set; }
public Type MyUnconvertibleProperty { get; set; }
public SubBoundObject SubObject { get; set; }
public ExceptionTestBoundObject ExceptionTestObject { get; set; }
public uint[] MyUintArray
{
get { return new uint[] { 7, 8 }; }
}
public int[] MyIntArray
{
get { return new[] { 1, 2, 3, 4, 5, 6, 7, 8 }; }
}
public Array MyArray
{
get { return new short[] { 1, 2, 3 }; }
}
public byte[] MyBytes
{
get { return new byte[] { 3, 4, 5 }; }
}
public BoundObject()
{
MyProperty = 42;
MyReadOnlyProperty = "I'm immutable!";
IgnoredProperty = "I am an Ignored Property";
MyUnconvertibleProperty = GetType();
SubObject = new SubBoundObject();
ExceptionTestObject = new ExceptionTestBoundObject();
}
public void TestCallback(IJavascriptCallback javascriptCallback)
{
const int taskDelay = 1500;
Task.Run(async () =>
{
await Task.Delay(taskDelay);
await javascriptCallback.ExecuteAsync("This callback from C# was delayed " + taskDelay + "ms");
});
}
public int EchoMyProperty()
{
return MyProperty;
}
public string Repeat(string str, int n)
{
string result = String.Empty;
for (int i = 0; i < n; i++)
{
result += str;
}
return result;
}
public string EchoParamOrDefault(string param = "This is the default value")
{
return param;
}
public void EchoVoid()
{
}
public Boolean EchoBoolean(Boolean arg0)
{
return arg0;
}
public Boolean? EchoNullableBoolean(Boolean? arg0)
{
return arg0;
}
public SByte EchoSByte(SByte arg0)
{
return arg0;
}
public SByte? EchoNullableSByte(SByte? arg0)
{
return arg0;
}
public Int16 EchoInt16(Int16 arg0)
{
return arg0;
}
public Int16? EchoNullableInt16(Int16? arg0)
{
return arg0;
}
public Int32 EchoInt32(Int32 arg0)
{
return arg0;
}
public Int32? EchoNullableInt32(Int32? arg0)
{
return arg0;
}
public Int64 EchoInt64(Int64 arg0)
{
return arg0;
}
public Int64? EchoNullableInt64(Int64? arg0)
{
return arg0;
}
public Byte EchoByte(Byte arg0)
{
return arg0;
}
public Byte? EchoNullableByte(Byte? arg0)
{
return arg0;
}
public UInt16 EchoUInt16(UInt16 arg0)
{
return arg0;
}
public UInt16? EchoNullableUInt16(UInt16? arg0)
{
return arg0;
}
public UInt32 EchoUInt32(UInt32 arg0)
{
return arg0;
}
public UInt32? EchoNullableUInt32(UInt32? arg0)
{
return arg0;
}
public UInt64 EchoUInt64(UInt64 arg0)
{
return arg0;
}
public UInt64? EchoNullableUInt64(UInt64? arg0)
{
return arg0;
}
public Single EchoSingle(Single arg0)
{
return arg0;
}
public Single? EchoNullableSingle(Single? arg0)
{
return arg0;
}
public Double EchoDouble(Double arg0)
{
return arg0;
}
public Double? EchoNullableDouble(Double? arg0)
{
return arg0;
}
public Char EchoChar(Char arg0)
{
return arg0;
}
public Char? EchoNullableChar(Char? arg0)
{
return arg0;
}
public DateTime EchoDateTime(DateTime arg0)
{
return arg0;
}
public DateTime? EchoNullableDateTime(DateTime? arg0)
{
return arg0;
}
public Decimal EchoDecimal(Decimal arg0)
{
return arg0;
}
public Decimal? EchoNullableDecimal(Decimal? arg0)
{
return arg0;
}
public String EchoString(String arg0)
{
return arg0;
}
// TODO: This will currently not work, as it causes a collision w/ the EchoString() method. We need to find a way around that I guess.
//public String echoString(String arg)
//{
// return "Lowercase echo: " + arg;
//}
public String lowercaseMethod()
{
return "lowercase";
}
public string ReturnJsonEmployeeList()
{
return "{"employees":[{"firstName":"John", "lastName":"Doe"},{"firstName":"Anna", "lastName":"Smith"},{"firstName":"Peter", "lastName":"Jones"}]}";
}
[JavascriptIgnore]
public string IgnoredProperty { get; set; }
[JavascriptIgnore]
public string IgnoredMethod()
{
return "I am an Ignored Method";
}