Read brush from resource dictionary

1429 views c#
0

I have the following resource dictionary defined within the App.XAML page on my project:

<Application.Resources>
    <ResourceDictionary>
        <AcrylicBrush x:Key="CustomAcrylicDarkBackground"
                  BackgroundSource="HostBackdrop"
                  TintColor="#a5a4a4"
                  TintOpacity="0.7"
                  FallbackColor="Black"/>
        <AcrylicBrush x:Key="CustomAcrylicDarkInApp"
                  BackgroundSource="Backdrop"
                  TintColor="Gray"
                  TintOpacity="0.5"
                  FallbackColor="Gray"/>
        <AcrylicBrush x:Key="CustomAcrylicDarkButton"
                      BackgroundSource="Backdrop"
                      TintColor="DarkGray"
                      TintOpacity="0.8"
                      FallbackColor="DarkGray"/>

        <SolidColorBrush x:Key="DefaultTextColour"
                         Color="#b880fc"
                         Opacity="100"/>

        <ResourceDictionary.MergedDictionaries>
            <XamlControlsResources  xmlns="using:Microsoft.UI.Xaml.Controls"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

Im having issues with reading the "DefaultTextColour" into the code behind on my main page.

I have been trying to read the brush in by doing this these:

TransfersItem.Foreground = Application.Current.Resources["DefaultTextColour"];

TransfersItem.Foreground = (Brush)FindName("DefaultTextColour");

TransfersItem.Foreground = (Brush)Application.Current.Resources("DefaultTextColour");

None of which have worked so far. Im not sure how i can refence the resouce.

Is it due to the fact im trying to read this into a property?

Edit: It does say that the resource cannot be used like a method but im not sure how to workaround

answered question

1 Answer

3

TransfersItem.Foreground = Application.Current.Resources["DefaultTextColour"] as SolidColorBrush;

You need to tell the app what type of return you are expecting.

posted this

Have an answer?

JD

Please login first before posting an answer.