是否可以将 XAML 中类内的类内的常量作为 x:Static 引用?

问题描述

在我的代码中,我声明了一个常量:

namespace Test.AppService
{
    public static partial class Const
    {
        public static class Options
        {
            public const string PhraseVisible = "PV";
            public const string MeaningVisible = "MV";
            // Many more constants below here

我想在我的 XAML 中访问它,所以我输入了这个:

<ContentPage
    x:Class="Test.SettingsPage"
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:app="clr-namespace:Test.AppService;assembly=Test">
    <Label Text="{x:Static app:Const.Options.PracticePhraseVisible}" />

当我将鼠标悬停在它上面时,IDE 会向我显示它会将应用程序识别为 Test.AppService.Const

我真的很困惑,不知道如何将文本设置为常量的值。谁能给我一些建议?

这是我收到的关于该行的错误消息:

“无法解析类型“Const.Options”。(XFC0000)

解决方法

你只需要设置使用Binding Source并从分部类Const中取出静态类Options,就可以在同一个文件中但在分部类之外,在XAML文件“app: Options.PracticePhraseVisible”。

CS:

namespace Test.AppService
{
   public static partial class Const
  {
  }

  public static class Options
  {
        public const string PhraseVisible = "PV";
        public const string MeaningVisible = "MV";
        // Many more constants below here
  }
}

XAML:

<ContentPage
    x:Class="Test.SettingsPage"
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:app="clr-namespace:Test.AppService;assembly=Test">
    <Label Text="{Binding Source={x:Static app:Options.PracticePhraseVisible}}" />
</ContentPage>