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

.NET MAUI 8.0中如何设置Shell Flyout菜单项竖条颜色?

.NET MAUI 8.0 Windows平台Shell Flyout菜单项竖条颜色设置方案

这个竖条是Windows平台Shell Flyout的原生视觉元素,MAUI没有直接提供对应的绑定属性,得通过Windows平台自定义Renderer来修改,具体步骤如下:

  • 创建Windows平台自定义Shell Renderer
    在你的Windows项目中添加一个类,继承自ShellRenderer,重写CreateFlyoutContentRenderer方法来获取原生Flyout控件并修改样式:

    using Microsoft.Maui.Controls.Platform;
    using Microsoft.Maui.Controls.Platform.Windows;
    using Microsoft.UI.Xaml.Controls;
    using Microsoft.UI.Xaml.Media;
    
    namespace YourAppNamespace.WinUI;
    
    public class CustomShellRenderer : ShellRenderer
    {
        protected override IShellFlyoutContentRenderer CreateFlyoutContentRenderer()
        {
            var baseRenderer = base.CreateFlyoutContentRenderer();
            if (baseRenderer is ShellFlyoutContentRenderer winFlyoutRenderer)
            {
                // 获取Flyout的原生ListView控件
                if (winFlyoutRenderer.FlyoutContent is ListView flyoutListView)
                {
                    // 创建ListViewItem的样式,修改选中时的竖条颜色
                    var itemStyle = new Microsoft.UI.Xaml.Style(typeof(ListViewItem));
                    // 这里替换成你想要的颜色,比如Microsoft.UI.Colors.Green
                    itemStyle.Setters.Add(new Microsoft.UI.Xaml.Setter(ListViewItem.SelectionIndicatorBrushProperty, new SolidColorBrush(Microsoft.UI.Colors.Red)));
                    flyoutListView.ItemContainerStyle = itemStyle;
                }
            }
            return baseRenderer;
        }
    }
    
  • 注册自定义Renderer
    在Windows项目的MauiProgram.cs中,注册这个自定义Renderer:

    builder.ConfigureMauiHandlers(handlers =>
    {
    #if WINDOWS
        handlers.AddHandler(typeof(Shell), typeof(CustomShellRenderer));
    #endif
    });
    

原理说明:Windows平台的Shell Flyout底层是用原生ListView实现的,那个竖条就是ListViewItem的SelectionIndicatorBrush属性控制的,所以通过修改原生控件的样式就能实现颜色自定义。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 20:42:13