Net maui How to swap dynamic resource and static resource value ?

Sami 881 Reputation points

I wanna to swap colors and text value but i have error

Xaml -----

<Grid x:Name="colorTitle" BackgroundColor="{StaticResource MyBlue}" >
<Label x:Name="tabTitle" Text="{DynamicResource Key=my-cart}" />

C # ------

if (TabCart.SelectedIndex == 0)

        tabTitle.Text = (string)App.Current.Resources["my-cart"];  
        colorTitle.BackgroundColor = (Color)App.Current.Resources["MyBlue"];  
    else if (TabCart.SelectedIndex == 1)  

        tabTitle.Text = (string)App.Current.Resources["my-orders"];  
        colorTitle.BackgroundColor = (Color)App.Current.Resources["MyPink"];  
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,220 questions
0 comments No comments
{count} votes

Accepted answer
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 39,066 Reputation points Microsoft Vendor


    The following code would successfully swap the dynamic resource and static resource value on my side.

       private void OnCounterClicked(object sender, EventArgs e)   
           //Swap the background color of the two controls  
           scroll.BackgroundColor = (Color)App.Current.Resources["NavigationBarTextColor"];  
           test_label.SetDynamicResource(Label.TextColorProperty, "NormalTextColor");  

    In addition, you could refer to Dynamic styles to get the best practice of using dynamic styles in MAUI.

    Best Regards,

    Alec Liu.

    If the answer is the right solution, 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.

0 additional answers

Sort by: Most helpful