question

87743156 avatar image
0 Votes"
87743156 asked WenyanZhang-MSFT commented

Change the content screen while leaving the top level as it is

236753-%E1%84%92%E1%85%AA%E1%84%86%E1%85%A7%E1%86%AB-%E1%84%8F%E1%85%A2%E1%86%B8%E1%84%8E%E1%85%A5-2022-09-01-105915.png




I have the following picture.
I would like it to change from a blue page to a red page when the button is clicked while maintaining the yellow color on the top.

Is it possible to implement it in this form?

dotnet-xamarin
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @87743156 , I have not heard from you for a couple of days. Please let me know if there is anything that I can help here.

0 Votes 0 ·

1 Answer

WenyanZhang-MSFT avatar image
0 Votes"
WenyanZhang-MSFT answered WenyanZhang-MSFT commented

Hello @87743156 ,

There are two elements on the view, you can find the second one by specifying the x:Name, then change the background color of this element.
For example:

 <StackLayout > 
         <!-- this view is a customized control called FirstContentView which derived from ContentView. 
         You could use any other element according to your needs -->
         <views:FirstContentView >
         </views:FirstContentView>
         <!--set name for the second view, the default color is CadetBlue-->
         <views:SecondContentView x:Name="SecondView" BackgroundColor="CadetBlue">
         </views:SecondContentView> 
 </StackLayout >

Change color when clicking button : SecondView.BackgroundColor = Colors.Red;

It's recommended that you can use interactive MVVM and two-way data bindings. Please create a ViewModel as the BindingContext of your page, then add Color property and implement the INotifyPropertyChanged interface, you can check the HslViewModel source code in the sample. The command interface is supported by Button, you can use the command interface and change the color.

In addition, you said "blue page" and "red page", if you want to push to a new ContentPage and change the background color of the second ContentPage, you can try Navigation and change the background color in the constructor.

Best Regards,
Wenyan Zhang


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.


· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Is it Xamarin Code?

There is no such thing as verticalStackLayout.

0 Votes 0 ·

It is StackLayout , I have edited the answer, you can check it. If you have any other issues, please feel free to let me know.

0 Votes 0 ·