You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在.NET Maui中从ViewModel修改Windows平台App.xaml的自定义颜色?

在.NET Maui Windows平台App.xaml中动态修改自定义颜色的方案

问题原因

你之前的代码无效,是因为Application.Current.Resources是.NET Maui跨平台的应用资源集合,而你在Windows平台Platforms->Windows->App.xaml里定义的资源,属于Windows专属的MauiWinUIApplication实例资源,两者不是同一个集合,所以找不到目标资源键,修改自然不生效。

可行实现方案

方案一:平台特定代码直接修改(适合快速实现)

  1. 在Windows项目的App.xaml.cs中添加静态修改方法:
using Microsoft.Maui.Graphics;
using Microsoft.UI;
using Microsoft.UI.Xaml.Media;

namespace YourAppName.Platforms.Windows;

public partial class App : MauiWinUIApplication
{
    // 静态方法用于修改平台资源颜色
    public static void UpdateAccentBrushColor(Color mauiColor)
    {
        var currentApp = Current as MauiWinUIApplication;
        if (currentApp?.Resources.TryGetValue("SystemControlForegroundAccentBrush", out var resource) == true)
        {
            if (resource is SolidColorBrush brush)
            {
                // 将Maui Color转换为WinUI Color
                var winuiColor = Colors.FromArgb(
                    (byte)(mauiColor.Alpha * 255),
                    (byte)(mauiColor.Red * 255),
                    (byte)(mauiColor.Green * 255),
                    (byte)(mauiColor.Blue * 255));
                brush.Color = winuiColor;
            }
        }
    }
}
  1. 在ViewModel中调用该方法(通过平台条件编译限定):
using Microsoft.Maui.Graphics;

private void UpdateCustomColorResource(Color color)
{
#if WINDOWS
    Platforms.Windows.App.UpdateAccentBrushColor(color);
#endif
}

方案二:绑定+转换器(MVVM友好)

如果希望更贴合MVVM模式,可以通过绑定ViewModel属性来动态更新颜色:

  1. 编写Maui Color转WinUI Color的转换器(Windows项目中):
using Microsoft.UI.Xaml.Data;
using Microsoft.Maui.Graphics;
using Microsoft.UI;

namespace YourAppName.Platforms.Windows;

public class MauiColorToWinUIColorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        if (value is Microsoft.Maui.Graphics.Color mauiColor)
        {
            return Colors.FromArgb(
                (byte)(mauiColor.Alpha * 255),
                (byte)(mauiColor.Red * 255),
                (byte)(mauiColor.Green * 255),
                (byte)(mauiColor.Blue * 255));
        }
        return Colors.Aqua;
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        throw new NotImplementedException();
    }
}
  1. 修改Windows平台的App.xaml,使用绑定替代固定颜色:
<maui:MauiWinUIApplication
    x:Class="YourAppName.Platforms.Windows.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:maui="using:Microsoft.Maui"
    xmlns:local="using:YourAppName.Platforms.Windows"
    xmlns:viewModels="using:YourAppName.ViewModels">

    <maui:MauiWinUIApplication.Resources>
        <!-- 注册转换器和ViewModel实例 -->
        <local:MauiColorToWinUIColorConverter x:Key="MauiColorConverter"/>
        <viewModels:MainViewModel x:Key="MainViewModel"/>
        
        <!-- 通过绑定动态获取颜色 -->
        <SolidColorBrush x:Key="SystemControlForegroundAccentBrush" 
                         Color="{Binding AccentColor, Source={StaticResource MainViewModel}, Converter={StaticResource MauiColorConverter}}"/>
        <StaticResource x:Key="NavigationViewSelectionIndicatorForeground" ResourceKey="SystemControlForegroundAccentBrush" />
    </maui:MauiWinUIApplication.Resources>

</maui:MauiWinUIApplication>
  1. 在ViewModel中定义可绑定的Color属性:
using Microsoft.Maui.Graphics;
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace YourAppName.ViewModels;

public class MainViewModel : INotifyPropertyChanged
{
    private Color _accentColor = Colors.Aqua;
    public Color AccentColor
    {
        get => _accentColor;
        set
        {
            _accentColor = value;
            OnPropertyChanged();
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    // 调用此方法即可更新颜色
    public void UpdateAccentColor(Color newColor)
    {
        AccentColor = newColor;
    }
}

内容的提问来源于stack exchange,提问作者10101

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 23:46:29