gpt4 book ai didi

c# - 我怎样才能让 WPF 在 Debug模式下使用一种窗口样式,在 Release模式下使用另一种窗口样式?

转载 作者:太空狗 更新时间:2023-10-29 19:53:28 25 4
gpt4 key购买 nike

我的窗口有两种不同的样式:

  1. 常规 - 窗口有标题栏并且可以移动/调整大小
  2. 固定 - 窗口没有标题栏并且固定在屏幕中央

对于我的开发机器上的任何一个显示器来说,窗口都太宽了,但它非常适合目标/安装机器。因此,在调试时,我需要能够移动窗口,以便我可以看到上面的所有内容,但是当我发布应用程序时,我需要它以“全屏”模式运行(就像投影仪模式下的 PowerPoint 应用程序)。

有什么方法可以根据我是在调试还是 Release模式下编译来设置窗口的 Style 属性?我在想我也许可以使用绑定(bind),但我不太确定如何实现它。

最佳答案

创建样式选择器类:

namespace WpfApplication1
{
public class DebugReleaseStylePicker
{
#if DEBUG
internal static readonly bool debug = true;
#else
internal static readonly bool debug=false;
#endif

public Style ReleaseStyle
{
get; set;
}

public Style DebugStyle
{
get; set;
}


public Style CurrentStyle
{
get
{
return debug ? DebugStyle : ReleaseStyle;
}
}
}
}

在你的 App.xaml 中添加到您的 Application.Resources 您的调试和发布样式 + StylePicker 的实例并将 ReleaseStyle 和 DebugStyle 设置为以前设置的样式:

<Application.Resources>
<Style x:Key="WindowDebugStyle">
<Setter Property="Window.Background" Value="Red"></Setter>
</Style>

<Style x:Key="WindowReleaseStyle">
<Setter Property="Window.Background" Value="Blue"></Setter>
</Style>

<WpfApplication1:DebugReleaseStylePicker x:Key="stylePicker"
ReleaseStyle="{StaticResource WindowReleaseStyle}"
DebugStyle="{StaticResource WindowDebugStyle}"/>
</Application.Resources>

在您的窗口标记中,像这样设置 WindowStyle:

<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300"
Style="{Binding Source={StaticResource stylePicker}, Path=CurrentStyle}">
..
</Window>

您可以重用 DebugReleaseStylePicker 将样式设置为任何其他控件,而不仅仅是 Window。

关于c# - 我怎样才能让 WPF 在 Debug模式下使用一种窗口样式,在 Release模式下使用另一种窗口样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1373850/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com