c# - 是否可以将 XAML 中的类中的类中的常量引用为 ax: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>
推荐阅读
- javascript - 如何从一行文本中删除一个字符
- hibernate - JPQL 多对多与 in 子句
- c# - C# FFT 实现未产生预期结果
- epplus - 使用 EPPlus 计算百分位数
- c# - FolderbrowserDialog 在发布版本(WindowsForms)中不展开“SelectedFolder”
- wordpress - 如何将 ACF 转发器设置为每行仅显示 X 个项目
- python - 在python中使用正则表达式匹配任何字符和/或未定义的换行符
- android - 如何在 Android Q 的外部存储中创建目录?
- liquibase - Liquibase 不适用于更改 Spring Boot 配置文件
- python - Python/Flask - 通过从 Jupyter Notebook 传递路径来上传文件