如何让WPF在调试模式下使用一种窗口样式而在发布模式下使用另一种窗口样式?

dev*_*xer 7 c# wpf xaml visual-studio

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

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

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

有没有办法Style根据我是否在Debug与Release模式下编译来设置窗口的属性?我以为我可以使用绑定,但我不太确定如何实现它.

Cla*_*ila 11

创建一个样式选择器类:

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;
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在你的App.xaml中添加你的Application.Resource你的调试和发布样式+ 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>
Run Code Online (Sandbox Code Playgroud)

在你的Window标记中设置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>
Run Code Online (Sandbox Code Playgroud)

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


Jon*_*eet 5

在XAML中可能很难做到,但在实际代码中,您可以执行以下操作:

#if DEBUG
    window.Style = WindowStyles.Regular;
#endif
Run Code Online (Sandbox Code Playgroud)

为什么不把它放在执行普通XAML代码后执行的地方?