0

In my code I have a constant declared like this:

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

I would like to access that in my XAML so I entered this:

<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}" />

When I hover over it the IDE shows me that it recognizes app as being Test.AppService.Const

I'm really confused and don't know how I can set the text to the value of the constant. Can anyone give me advice on what to do?

Here's the error message I get for that line:

"Cannot resolve type "Const.Options". (XFC0000)

iamsophia
  • 786
  • 8
  • 25
  • Does this answer your question? [Xamarin XAML x:Static reference to property in a nested class](https://stackoverflow.com/questions/48080481/xamarin-xaml-xstatic-reference-to-property-in-a-nested-class) – Shaw Jan 08 '21 at 02:49

1 Answers1

-2

You only need to set using Binding Source and take out the static class Options from the partial class Const, you can have it in the same file but out of the partial class, and reference like this in the XAML file "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>
Ricardo Romo
  • 1,588
  • 12
  • 25
  • This doesn't work and it still comes up with a message saying "Cannot resolve type "Const.Options". (XFC0000) – iamsophia Jan 07 '21 at 18:33
  • I'm sorry but I downvoted this as the solution doesn't work for me and I think it's better that there's another solution hopefully. Perhaps if you have an idea you can remove this answer and try another. – iamsophia Jan 07 '21 at 19:36