如何使用C#代码而非XAML来启用窗口装饰?

5

所以我正在使用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>

我的问题是,如何使用C#代码而不是XAML来启用Chrome?(即如何在代码后台中应用Chrome?)
2个回答

11
啊,我太蠢了。这很容易:
WindowChrome.SetWindowChrome(this, new WindowChrome());

6
我知道这是一个老问题,但我注意到在.NET 4.5中无法使用WindowChrome.GetWindowChrome()。我不确定这是否与PresentationFramework程序集中包含了System.Windows.Shell有关。但由于它一直返回null,因此没有办法更新窗口的外观。

所以我的解决方案是为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>

代码后台:

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

我希望这能帮助需要的人。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接