从设置页面立即更改.NET MAUI的主页网格背景颜色。

huangapple go评论75阅读模式
英文:

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

&lt;Color x:Key=&quot;PrimaryColor&quot;&gt;#2196F3&lt;/Color&gt;
&lt;Color x:Key=&quot;SecondaryColor&quot;&gt;#FFC107&lt;/Color&gt;

And then update it on runtime

Application.Current.Resources[&quot;PrimaryColor&quot;] = Color.Red;

This Color should be used in your Controls with the DynamicResource keyword

BackgroundColor=&quot;{DynamicResource PrimaryColor}&quot;

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;
}

huangapple
  • 本文由 发表于 2023年2月27日 09:44:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75576152.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定