如何使用C#代码而不是XAML启用窗口镶边?

Ine*_*elp 3 c# wpf xaml window-chrome

所以我使用WPFShell将chrome应用于自定义窗口.我从这篇文章中了解到,为了使用它,我必须引用Microsoft.Windows.Shell库并使用这个XAML代码:

<shell:WindowChrome.WindowChrome>
    <shell:WindowChrome
    ResizeBorderThickness="6"
    CaptionHeight="43"
    CornerRadius="25,25,10,10"
    GlassFrameThickness="0">
    </shell:WindowChrome>
</shell:WindowChrome.WindowChrome>
Run Code Online (Sandbox Code Playgroud)

我的问题是,如何使用C#代码而不是XAML启用chrome?(即如何在代码隐藏中应用chrome?)

Ine*_*elp 7

啊,傻我.很容易:

WindowChrome.SetWindowChrome(this, new WindowChrome());
Run Code Online (Sandbox Code Playgroud)


小智 5

我知道这是一个较旧的问题,但我注意到我无法WindowChrome.GetWindowChrome()在 .NET 4.5 中工作。我不确定这是否与System.Windows.Shell包含在PresentationFramework程序集中有关。但由于它不断返回null因此无法更新 chrome。

所以我的解决方案是添加一个“名称”,使其WindowChrome可以在代码隐藏中访问。

XAML:

<Window x:Class="SomeProject.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
    mc:Ignorable="d"Title="Some Window" WindowStyle="None" ResizeMode="CanResize" 
    AllowsTransparency="True">

    <WindowChrome.WindowChrome>
        <WindowChrome x:Name="chrome" ResizeBorderThickness="6" CaptionHeight="0"
                      GlassFrameThickness="0" CornerRadius="0" UseAeroCaptionButtons="False"/>
    </WindowChrome.WindowChrome>

</window>
Run Code Online (Sandbox Code Playgroud)

背后代码:

using System;
using System.Window;    

namespace SomeProject
{
    public partial class MainWindow: Window
    {
        public MainWindow()
        {
            //Get Existing 'WindowChrome' Properties.
            var captionHeight = chrome.CaptionHeight;

            //Set Existing 'WindowChrome' Properties.
            chrome.GlassFrameThickness = new Thickness(2d);

            //Assign a New 'WindowChrome'.
            chrome = new System.Windows.Shell.WindowChrome();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我希望这可以帮助有需要的人。