无需从远程服务器托管,您可以使用本地字体嵌入 CSS 并注入到浏览器控制。
(1.) 创建嵌入字体的 CSS
body, a, p, span, div, textarea {
font-family: MyCustomFont;
}
@font-face {
font-family: MyCustomFont;
src: url(data:application/x-font-woff;charset=utf-8;base64,d09GRgABAXXXXXXXXXXX......) format('woff');
font-weight: normal;
font-style: normal;
}
(2.) 在 WebView_NavigationCompleted 方法中注入 jQuery 和 CSS
private async void MyWebView_NavigationCompleted(WebView sender, WebViewNavigationCompletedEventArgs args)
{
// Inject jQuery file which located at local
StorageFile jquery = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///JavaScripts\\jquery-1.10.2.min.js"));
string jquery_string = await FileIO.ReadTextAsync(jquery);
await MyWebView.InvokeScriptAsync("eval", new string[] { jquery_string });
// Inject custom font embedded CSS
StorageFile myfontcss = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///CSS\\mycustomfont.css"));
string myfontcss_string = await FileIO.ReadTextAsync(myfontcss);
myfontcss_string = myfontcss_string.Replace("\n", "").Replace("\t", ""); //.Replace("'", "'").Replace("\"", """);
string cssRef = "$(\"<style id='myfont' type='text/css'>" + myfontcss_string + "</style>\").appendTo('head');";
await MyWebView.InvokeScriptAsync("eval", new string[] { cssRef });
}