我需要在运行时下载字体文件,并将该字体应用于文本。我成功下载了字体文件,并在添加到Application.Current.Resources
后将其添加到Windows.ApplicationModel.Package.Current.InstalledLocation (Debug\AppX\Assets)
,但字体不适用于文本,但如果我手动将字体文件添加到应用程序Assets
而不是(Debug\AppX\Assets)
文件夹,则字体将成功应用。以下是我所做的完整代码
//Downloading font file
HttpClient client = new HttpClient();
var response = await client.GetByteArrayAsync("https://webfonts.sample.com/archivoblackregular/font.ttf");
//Creating font file in public space and writing all response to the file
StorageFolder PublicFontFolder = ApplicationData.Current.LocalFolder;
StorageFile fontfile = await PublicFontFolder.CreateFileAsync(fontFamily+".ttf",CreationCollisionOption.ReplaceExisting);
await File.WriteAllBytesAsync(fontfile.Path, response);
//Moving that file to the Assets folder as we don't have direct access to create file in Assets folder
StorageFolder AssetsFolder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync("Assets");
await fontfile.MoveAsync(AssetsFolder, fontfile.Name, NameCollisionOption.ReplaceExisting);
StorageFile AssetsFontFile = await AssetsFontFolder.GetFileAsync(fontfile.Name);
//Adding that font file to application resources
Application.Current.Resources[fontFamily] = new FontFamily(AssetsFontFile.Path + "#" + fontFamily);
我怎样才能完成我的要求?
1条答案
按热度按时间rjee0c151#
我已经知道我做错了什么而不是这么做
Application.Current.Resources[fontFamily] = new FontFamily(AssetsFontFile.Path + "#" + fontFamily);
我必须做的是,如果文件在本地文件夹中,则
Application.Current.Resources[fontFamily] = new FontFamily("ms-appdata:///<folder>/<subfolder>/<file>" + "#" + fontFamily);
,如果在assets文件夹中,则
Application.Current.Resources[fontFamily] = new FontFamily("ms-appx:///<subfolder>/<file>" + "#" + fontFamily);