xamlxamarin.formsmarkup-extensions

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


I am trying to make a menu item's visibility conditioned on the DEBUG macro. (It's a debug menu item and so should not be visible in a release build.) I am trying to follow the x:Static example in https://learn.microsoft.com/en-us/xamarin/xamarin-forms/xaml/xaml-basics/xaml-markup-extensions

I have the following in my 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

In the AppShell XAML I have tried numerous variations of

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

But everything I have tried either gives me the error XFC0000 Cannot resolve type "bool" -- or some other error.

Am I way off base, or is bool not a valid type for x:Static? How do I code this?


Solution

  • From the docs you linked what you are trying to accomplish comes under "a public static field", thus you need to specify the class where the field IsDebug is defined, in the docs example the class name is AppConstants, it is not a matter of "bool" type you can use any type as long as it is the type expected by the property.

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