是否可以将布尔类型与 XAML X:静态一起使用?



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

我的 AppShell 类中有以下内容:

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" - 或其他一些错误。

我是否偏离了基础,或者布尔值不是 x:静态的有效类型?我该如何编码?

从您链接的文档来看,您要完成的任务属于"公共静态字段",因此您需要指定定义字段IsDebug的类,在文档示例中类名是AppConstants,这不是"bool"类型的问题,您可以使用任何类型,只要它是属性所需的类型。

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

相关内容

最新更新