问题描述
我正在开发跨平台应用.在 webview 中,我想使用 @font-face.它在 Android 上运行良好,但不适用于 UWP.我的字体文件位于 Assets/Fonts 文件夹和样式表中,我使用以下代码:
@font-face {字体系列:'defont';src: url('Assets/Fonts/mher.ttf');}
在 Android 中我使用的是这个:
src: url('file:///android_asset/mher.ttf');
但是当我在 UWP 中尝试相同的方法时,它不起作用.请问有人知道吗?
更新:
下面是我使用普通 WebView 的 C# 代码.我正在从本地 html 文件中获取 html 内容,替换一些占位符并将新的 html 字符串显示到 WebView.
var browser = new WebView();var htmlSource = new HtmlWebViewSource();var assembly = IntrospectionExtensions.GetTypeInfo(typeof(WebViewPage)).Assembly;Stream stream = assembly.GetManifestResourceStream("MyApp.Assets.MyTemplate.html");字符串 templateHtml = "";使用 (var reader = new StreamReader(stream)){templateHtml = reader.ReadToEnd();}templateHtml = templateHtml.Replace("{pageHeading}", _post.Title);templateHtml = templateHtml.Replace("{body}", _post.Content);如果(_post.IsFeaturedImageAvailable){templateHtml = templateHtml.Replace("{imgSrc}", _post.FeaturedImageUrl);}别的{templateHtml = templateHtml.Replace("{imgSrc}", "");}templateHtml = templateHtml.Replace("pdfprnt-buttons", "pdfprnt-buttons 隐藏");htmlSource.Html = templateHtml;htmlSource.BaseUrl = DependencyService.Get().Get();browser.Source = htmlSource;内容=浏览器;
它有特殊的
这是您可以参考的代码示例.
I am working on cross platform app. In webview I want to use @font-face. It is working fine on Android but not working on UWP. My font file is in Assets/Fonts folder and in style sheet I am using the below code:
@font-face {
font-family: 'defont';
src: url('Assets/Fonts/mher.ttf');
}
In Android I was using this:
src: url('file:///android_asset/mher.ttf');
But when same approach I try in UWP, it is not working.Please anyone has any idea?
UPDATE:
Below is my C# code of using normal WebView. I am getting html content from local html file, replace some placeholders and display new html string to WebView.
var browser = new WebView();
var htmlSource = new HtmlWebViewSource();
var assembly = IntrospectionExtensions.GetTypeInfo(typeof(WebViewPage)).Assembly;
Stream stream = assembly.GetManifestResourceStream("MyApp.Assets.MyTemplate.html");
string templateHtml = "";
using (var reader = new StreamReader(stream))
{
templateHtml = reader.ReadToEnd();
}
templateHtml = templateHtml.Replace("{pageHeading}", _post.Title);
templateHtml = templateHtml.Replace("{body}", _post.Content);
if (_post.IsFeaturedImageAvailable)
{
templateHtml = templateHtml.Replace("{imgSrc}", _post.FeaturedImageUrl);
}else
{
templateHtml = templateHtml.Replace("{imgSrc}", "");
}
templateHtml = templateHtml.Replace("pdfprnt-buttons", "pdfprnt-buttons hide");
htmlSource.Html = templateHtml;
htmlSource.BaseUrl = DependencyService.Get<IBaseUrl>().Get();
browser.Source = htmlSource;
Content = browser;
It has special Uri Scheme
within UWP. For for getting the font in the Assets folder, you need use ms-appx-web://
. Please try the following code.
@font-face {
font-family: 'defont';
src: url('ms-appx-web:///Assets/Fonts/mher.ttf');
}
Update
I used HybridWebView
that could load local html file directly. Then add the css file in UWP project.
html,body{margin:0;padding:10px}
@font-face {
font-family: 'MyWebFont';
src: url('ms-appx-web:///Assets/hello.ttf')
}
body, p, h1, span {
font-family: 'MyWebFont';
}
This is code sample that you could refer.
这篇关于如何在 xamarin UWP 中使用字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!