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

Sami 856 Reputation points
2022-10-31T22:54:54.37+00:00

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

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"];  
    }
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,823 questions
0 comments No comments
{count} votes

Accepted answer
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 34,761 Reputation points Microsoft Vendor
    2022-11-01T03:02:45.7+00:00

    Hello,

    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