微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决是否可以将 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>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。