英文:
Change Main page grid's background color from settings page instantly .NET MAUI
问题
new to MAUI, i have a MainPage and i have a Settings page, lets say i want to change the background color of a grid inside MainPage. xaml from Settings page and have its affect applied instantly. how can i do it?
我是 MAUI 的新手,我有一个 MainPage 和一个 Settings 页面,假设我想从 Settings 页面更改 MainPage 内一个 Grid 的背景颜色,并立即应用效果。我该如何做?
i already know how to apply it on app restart by assigning the grid a name and then assign the new color to grid's background on MainPage's constructor using preferences . but how can i apply it without having to restart the app?
我已经知道如何在应用程序重新启动时应用它,方法是给 Grid 分配一个名称,然后在 MainPage 的构造函数中使用首选项为 Grid 的背景分配新颜色。但如何在无需重新启动应用的情况下应用它呢?
I've tried accessing it directly from settings page but that didn't work.
我尝试直接从 Settings 页面访问它,但那没有起作用。
英文:
new to MAUI, i have a MainPage and i have a Settings page, lets say i want to change the background color of a grid inside MainPage. xaml from Settings page and have its affect applied instantly. how can i do it?
i already know how to apply it on app restart by assigning the grid a name and then assign the new color to grid's background on MainPage's constructor using preferences . but how can i apply it without having to restart the app?
I've tried accessing it directly from settings page but that didn't work.
答案1
得分: 0
你可以使用DynamicResource来实现这一点,你可以定义颜色,然后从SettingsPage中进行更改,它将被更新。你可以在App.Xaml中定义你的颜色。
<Color x:Key="PrimaryColor">#2196F3</Color>
<Color x:Key="SecondaryColor">#FFC107</Color>
然后在运行时更新它
Application.Current.Resources["PrimaryColor"] = Color.Red;
这个颜色应该在你的控件中使用DynamicResource关键字
BackgroundColor="{DynamicResource PrimaryColor}"
希望这有所帮助!
英文:
You can use DynamicResource for this, You can define the Color and Then change it from SettingsPage and it will be updated, You can define your colors in your App.Xaml
<Color x:Key="PrimaryColor">#2196F3</Color>
<Color x:Key="SecondaryColor">#FFC107</Color>
And then update it on runtime
Application.Current.Resources["PrimaryColor"] = Color.Red;
This Color should be used in your Controls with the DynamicResource keyword
BackgroundColor="{DynamicResource PrimaryColor}"
Hope this helps!
答案2
得分: 0
在你的 Settings 页面的 .xaml.cs 文件中:
Navigation.PushAsync(new MainPage(Colors.BlueViolet));
在 MainPage.xaml.cs 文件中:
public MainPage(Color backgroundColor)
{
InitializeComponent();
grid.BackgroundColor = backgroundColor;
}
英文:
In your .xaml.cs of Settings page:
Navigation.PushAsync(new MainPage(Colors.BlueViolet));
MainPage.xaml.cs:
public MainPage (Color backgroundColor)
{
InitializeComponent ();
grid.BackgroundColor = backgroundColor;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论