英文:
How to display all installed fonts in a ComboBox
问题
我需要在 WinUI 3 的 ComboBox
中显示用户安装的所有字体。我正在使用以下代码:
<ComboBox x:Name="FontComboBox" ItemsSource="{Binding Source={x:Static Fonts.SystemFontFamilies}}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Source}" FontFamily="{Binding Source}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
但是这会导致错误:
未找到类型 'x:Static'。请验证您是否未丢失程序集引用,以及是否已构建所有已引用的程序集。
不确定什么是程序集引用。
我应该怎么做来修复这个问题?
英文:
I need to display all the user's installed fonts in a WinUI 3 ComboBox
. I'm using this code:
<ComboBox x:Name="FontComboBox" ItemsSource="{Binding Source={x:Static Fonts.SystemFontFamilies}}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Source}" FontFamily="{Binding Source}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
But this causes the error:
> The type 'x:Static' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built.
Not sure what an assembly reference is.
What would I do to fix this?
答案1
得分: 1
AFAIK,WinUI 3 不支持 x:Static。
所以,让我向您展示使用 Win2D NuGet 包实现此目标的另一种方式。
<Page
x:Class="FontFamiliesExample.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:FontFamiliesExample"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:win2d="using:Microsoft.Graphics.Canvas.Text"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
mc:Ignorable="d">
<StackPanel>
<ComboBox ItemsSource="{x:Bind win2d:CanvasTextFormat.GetSystemFontFamilies()}" />
</StackPanel>
</Page>
英文:
AFAIK, WinUI 3 doesn't support x:Static.
So, let me show you another way to achieve this using the Win2D NuGet package.
<Page
x:Class="FontFamiliesExample.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:FontFamiliesExample"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:win2d="using:Microsoft.Graphics.Canvas.Text"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
mc:Ignorable="d">
<StackPanel>
<ComboBox ItemsSource="{x:Bind win2d:CanvasTextFormat.GetSystemFontFamilies()}" />
</StackPanel>
</Page>
答案2
得分: 0
WinUI 3 不支持 x:Static
。而且 WinUI 3 也不支持 Fonts.SystemFontFamilies
。
如果您想显示所有安装的字体,我建议您尝试安装 Win2D
Nuget 包。
然后,您可以使用 CanvasTextFormat.GetSystemFontFamilies 方法 获取可用字体系列的列表。
以下是代码:
public List<string> Fonts
{
get
{
return CanvasTextFormat.GetSystemFontFamilies().OrderBy(f => f).ToList();
}
}
<ComboBox x:Name="FontsCombo"
ItemsSource="{x:Bind Fonts}">
</ComboBox>
英文:
WinUI 3 doesn't support x:Static
. And WinUI 3 also doesn't support Fonts.SystemFontFamilies
.
If you want to display all installed fonts. I suggest you could try to install the Win2D
Nuget package.
And then you could use CanvasTextFormat.GetSystemFontFamilies Method to get a list of font families available.
Here is the code:
public List<string> Fonts
{
get
{
return CanvasTextFormat.GetSystemFontFamilies().OrderBy(f => f).ToList();
}
}
<ComboBox x:Name="FontsCombo"
ItemsSource="{x:Bind Fonts}">
</ComboBox>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论