我可以将 bool 类型与 XAML X:Static 一起使用吗?

Can I use a bool type with XAML X:Static?

我正在尝试使菜单项的可见性以 DEBUG 宏为条件。 (这是一个调试菜单项,因此在发布版本中不应该可见。)我正在尝试遵循 https://docs.microsoft.com/en-us/xamarin/xamarin-forms/xaml/xaml-basics/xaml-markup-extensions

中的 x:Static 示例

我的 AppShell Class:

namespace myappname
{
    public partial class AppShell : Xamarin.Forms.Shell
    {

#if DEBUG
        public static readonly bool IsDebug = true;
#else
        public static readonly bool IsDebug = false;
#endif

在 AppShell XAML 中,我尝试了多种变体

<FlyoutItem Title="Debug" Icon="DebugCheckedTests_16x.png" IsVisible="{x:Static local:bool.IsDebug}">

但我尝试过的所有方法要么给出错误 XFC0000 无法解析类型“bool”——要么给出其他错误。

我是不是离题太远了,或者 bool 不是 x:Static 的有效类型?我该如何编码?

从文档中,您 linked 您试图完成的工作位于“public 静态字段”下,因此您需要指定 class 字段 [=12] =] 已定义,在文档示例中,class 名称为 AppConstants,这与“bool”类型无关,您可以使用任何类型,只要它是 [= 期望的类型即可20=].

xmlns:localRoot="clr-namespace:myappname"
<FlyoutItem Title="Debug" Icon="DebugCheckedTests_16x.png"
            IsVisible="{x:Static localRoot:AppShell.IsDebug}>