加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 18:13:37 所属栏目:百科 来源:网络整理
导读:我的窗口有两种不同的样式: 常规 – 窗口有标题栏,可以移动/调整大小 固定 – 窗口没有标题栏,固定在屏幕中央 对于我的开发机器上的任何一个显示器,窗口太宽,但它非常适合目标/安装机器.因此,在调试时,我需要能够移动窗口以便我可以看到它上面的所有内容,但
我的窗口有两种不同的样式:

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

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

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

解决方法

创建一个样式选择器类:
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.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>

在你的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>

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读