XAML Binding to a static resource

Christian Tauschek 21 Reputation points
2021-10-31T17:50:08.627+00:00

Hello,
i'm setting a value to a static property like this:

<Setter Property="BackgroundColor" Value="{x:Static e:MyClass1.Blau}">

This works fine, if MyClass1 is:

public static class MyClass1
{
 public static Color Blau { get; } = Color.Blue;
}

But i have a static class MyClass2 within MyClass1 like this:

    public static class MyClass1
    {
          public static class MyClass2
          {
                     public static Color Blau { get; } = Color.Blue;
          }
    }

in this case, i should be able to access with the following xaml-code:

<Setter Property="BackgroundColor" Value="{x:Static e:MyClass1.MyClass2.Blau}">

But this does not work, because get the errormessage "cannot resolve type MyClass1.MyClass2"
How can i solve this problem?

Christian

Developer technologies .NET Xamarin
0 comments No comments
{count} votes

Accepted answer
  1. JarvanZhang 23,971 Reputation points
    2021-11-01T02:34:49.52+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    get the errormessage "cannot resolve type MyClass1.MyClass2"

    When using a nested type in xaml, please using MyAssembly.MyClass+MyOtherClass style instead.

    Here is the sample code, you could refer to it.

       <Setter Property="BackgroundColor" Value="{x:Static e:MyClass1+MyClass2.Blau}">  
    

    Similar issue case: https://stackoverflow.com/questions/48080481/xamarin-xaml-xstatic-reference-to-property-in-a-nested-class

    Best Regards,

    Jarvan Zhang


    If the response is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.