如何在组合框中显示所有已安装的字体

huangapple go评论152阅读模式
英文:

How to display all installed fonts in a ComboBox

问题

我需要在 WinUI 3 的 ComboBox 中显示用户安装的所有字体。我正在使用以下代码:

  1. <ComboBox x:Name="FontComboBox" ItemsSource="{Binding Source={x:Static Fonts.SystemFontFamilies}}">
  2. <ComboBox.ItemTemplate>
  3. <DataTemplate>
  4. <TextBlock Text="{Binding Source}" FontFamily="{Binding Source}" />
  5. </DataTemplate>
  6. </ComboBox.ItemTemplate>
  7. </ComboBox>

但是这会导致错误:

未找到类型 'x:Static'。请验证您是否未丢失程序集引用,以及是否已构建所有已引用的程序集。

不确定什么是程序集引用。

我应该怎么做来修复这个问题?

英文:

I need to display all the user's installed fonts in a WinUI 3 ComboBox. I'm using this code:

  1. &lt;ComboBox x:Name=&quot;FontComboBox&quot; ItemsSource=&quot;{Binding Source={x:Static Fonts.SystemFontFamilies}}&quot;&gt;
  2. &lt;ComboBox.ItemTemplate&gt;
  3. &lt;DataTemplate&gt;
  4. &lt;TextBlock Text=&quot;{Binding Source}&quot; FontFamily=&quot;{Binding Source}&quot; /&gt;
  5. &lt;/DataTemplate&gt;
  6. &lt;/ComboBox.ItemTemplate&gt;
  7. &lt;/ComboBox&gt;

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 包实现此目标的另一种方式。

  1. &lt;Page
  2. x:Class=&quot;FontFamiliesExample.MainPage&quot;
  3. xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
  4. xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;
  5. xmlns:d=&quot;http://schemas.microsoft.com/expression/blend/2008&quot;
  6. xmlns:local=&quot;using:FontFamiliesExample&quot;
  7. xmlns:mc=&quot;http://schemas.openxmlformats.org/markup-compatibility/2006&quot;
  8. xmlns:win2d=&quot;using:Microsoft.Graphics.Canvas.Text&quot;
  9. Background=&quot;{ThemeResource ApplicationPageBackgroundThemeBrush}&quot;
  10. mc:Ignorable=&quot;d&quot;&gt;
  11. &lt;StackPanel&gt;
  12. &lt;ComboBox ItemsSource=&quot;{x:Bind win2d:CanvasTextFormat.GetSystemFontFamilies()}&quot; /&gt;
  13. &lt;/StackPanel&gt;
  14. &lt;/Page&gt;
英文:

AFAIK, WinUI 3 doesn't support x:Static.

So, let me show you another way to achieve this using the Win2D NuGet package.

  1. &lt;Page
  2. x:Class=&quot;FontFamiliesExample.MainPage&quot;
  3. xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
  4. xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;
  5. xmlns:d=&quot;http://schemas.microsoft.com/expression/blend/2008&quot;
  6. xmlns:local=&quot;using:FontFamiliesExample&quot;
  7. xmlns:mc=&quot;http://schemas.openxmlformats.org/markup-compatibility/2006&quot;
  8. xmlns:win2d=&quot;using:Microsoft.Graphics.Canvas.Text&quot;
  9. Background=&quot;{ThemeResource ApplicationPageBackgroundThemeBrush}&quot;
  10. mc:Ignorable=&quot;d&quot;&gt;
  11. &lt;StackPanel&gt;
  12. &lt;ComboBox ItemsSource=&quot;{x:Bind win2d:CanvasTextFormat.GetSystemFontFamilies()}&quot; /&gt;
  13. &lt;/StackPanel&gt;
  14. &lt;/Page&gt;

答案2

得分: 0

WinUI 3 不支持 x:Static。而且 WinUI 3 也不支持 Fonts.SystemFontFamilies

如果您想显示所有安装的字体,我建议您尝试安装 Win2D Nuget 包。

然后,您可以使用 CanvasTextFormat.GetSystemFontFamilies 方法 获取可用字体系列的列表。
以下是代码:

  1. public List<string> Fonts
  2. {
  3. get
  4. {
  5. return CanvasTextFormat.GetSystemFontFamilies().OrderBy(f => f).ToList();
  6. }
  7. }
  1. <ComboBox x:Name="FontsCombo"
  2. ItemsSource="{x:Bind Fonts}">
  3. </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:

  1. public List&lt;string&gt; Fonts
  2. {
  3. get
  4. {
  5. return CanvasTextFormat.GetSystemFontFamilies().OrderBy(f =&gt; f).ToList();
  6. }
  7. }
  8. &lt;ComboBox x:Name=&quot;FontsCombo&quot;
  9. ItemsSource=&quot;{x:Bind Fonts}&quot;&gt;
  10. &lt;/ComboBox&gt;

huangapple
  • 本文由 发表于 2023年7月5日 00:29:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76614473.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定