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

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

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:

&lt;ComboBox x:Name=&quot;FontComboBox&quot; ItemsSource=&quot;{Binding Source={x:Static Fonts.SystemFontFamilies}}&quot;&gt;
    &lt;ComboBox.ItemTemplate&gt;
        &lt;DataTemplate&gt;
            &lt;TextBlock Text=&quot;{Binding Source}&quot; FontFamily=&quot;{Binding Source}&quot; /&gt;
        &lt;/DataTemplate&gt;
    &lt;/ComboBox.ItemTemplate&gt;
&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 包实现此目标的另一种方式。

&lt;Page
    x:Class=&quot;FontFamiliesExample.MainPage&quot;
    xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
    xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;
    xmlns:d=&quot;http://schemas.microsoft.com/expression/blend/2008&quot;
    xmlns:local=&quot;using:FontFamiliesExample&quot;
    xmlns:mc=&quot;http://schemas.openxmlformats.org/markup-compatibility/2006&quot;
    xmlns:win2d=&quot;using:Microsoft.Graphics.Canvas.Text&quot;
    Background=&quot;{ThemeResource ApplicationPageBackgroundThemeBrush}&quot;
    mc:Ignorable=&quot;d&quot;&gt;

    &lt;StackPanel&gt;
        &lt;ComboBox ItemsSource=&quot;{x:Bind win2d:CanvasTextFormat.GetSystemFontFamilies()}&quot; /&gt;
    &lt;/StackPanel&gt;

&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.

&lt;Page
    x:Class=&quot;FontFamiliesExample.MainPage&quot;
    xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
    xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;
    xmlns:d=&quot;http://schemas.microsoft.com/expression/blend/2008&quot;
    xmlns:local=&quot;using:FontFamiliesExample&quot;
    xmlns:mc=&quot;http://schemas.openxmlformats.org/markup-compatibility/2006&quot;
    xmlns:win2d=&quot;using:Microsoft.Graphics.Canvas.Text&quot;
    Background=&quot;{ThemeResource ApplicationPageBackgroundThemeBrush}&quot;
    mc:Ignorable=&quot;d&quot;&gt;

    &lt;StackPanel&gt;
        &lt;ComboBox ItemsSource=&quot;{x:Bind win2d:CanvasTextFormat.GetSystemFontFamilies()}&quot; /&gt;
    &lt;/StackPanel&gt;

&lt;/Page&gt;

答案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&lt;string&gt; Fonts
    {
        get
        {
            return CanvasTextFormat.GetSystemFontFamilies().OrderBy(f =&gt; f).ToList();
        }
    }




&lt;ComboBox x:Name=&quot;FontsCombo&quot;
        ItemsSource=&quot;{x:Bind Fonts}&quot;&gt;

    &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:

确定