我有两种不同的窗口样式:
- 普通 - 窗口有标题栏且可以移动/调整大小
- 固定 - 窗口没有标题栏,固定在屏幕中央
这个窗口对于我的开发机上的任一监视器都太宽了,但它恰好适合目标/安装机。因此,在调试时,我需要能够移动窗口以便能看到其上的所有内容,但在发布应用程序时,我需要它以“全屏”模式运行(就像投影仪模式下的PowerPoint应用程序)。
是否有办法根据我是在Debug还是Release模式下编译来设置窗口的Style属性?我想可能可以使用绑定,但我不太确定如何实现它。
我有两种不同的窗口样式:
这个窗口对于我的开发机上的任一监视器都太宽了,但它恰好适合目标/安装机。因此,在调试时,我需要能够移动窗口以便能看到其上的所有内容,但在发布应用程序时,我需要它以“全屏”模式运行(就像投影仪模式下的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.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。在XAML中可能很难做到,但在实际代码中,你可以像这样做:
#if DEBUG
window.Style = WindowStyles.Regular;
#endif
WindowStyle = WindowStyle.SingleBorderWindow
对我不起作用。 - Firedragon你可以创建一个标记扩展,就像这个例子:
public class DebugStyleExtension : MarkupExtension
{
public object DebugResourceKey { get; set; }
public object ReleaseResourceKey { get; set; }
public object ProvideValue(IServiceProvider provider)
{
#if DEBUG
return Application.Current.FindResource(DebugResourceKey) as Style;
#else
return Application.Current.FindResource(ReleaseResourceKey) as Style
#endif
}
}
<Window ...
xmlns:my="clr-namespace:MyNamespace"
Style="{my:DebugStyle DebugResourceKey=DebugStyle, ReleaseResourceKey=NormalStyle}">
需要对象引用才能访问非静态字段、方法或属性“System.Windows.Application.FindResource(object)”
。有问题的代码是 Application.FindResource
。这个类文件需要与我的应用程序在同一个程序集中吗?谢谢。 - devuxer#else
后面紧接着的那行代码显示为灰色文本,并且没有响应智能感知。你有什么想法是什么原因吗?再次感谢。 - devuxer您可以在XAML文件以及代码后台中进行条件编译。 查看这篇文章
基本上,在您的Properties\AssemblyInfo.cs文件中,您可以这样做:
#if BETA
[assembly:XmlnsDefinition("BetaVersion", "Example.Technology")]
#endif
将xmlns添加到*.XAML文件中:
xmlns:beta="BetaVersion"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
<mc:Choice Requires="beta">
<Label>
This is ALPHA software. Confidential.
Internal use only. Do not distribute
<Label>
</mc:Choice>
assembly:XmlnsDefinition
不受支持。你能否使用#if DEBUG将属性设置为不同的值并绑定它?
也许可以。
#if DEBUG
style = 0;
#else
style = 1;
#endif
有很多有用的答案...我想到了另一个想法,我想提出来:一个值转换器加上一个绑定:
这是值转换器:
public class WindowToWindowStyle : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var window = (Window)value;
var style = (Style)window.FindResource("Window_FixedStyle");
#if DEBUG
style = (Style)window.FindResource("Window_Style");
#endif
return style;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return Binding.DoNothing;
}
}
这是我的窗口声明:
<Window
...
xmlns:local="clr-namespace:MyProj"
Style="{Binding RelativeSource={RelativeSource Self}, Converter={StaticResource ValueConverter_WindowToWindowStyle}}"
WindowStartupLocation="CenterScreen">
<Window.Resources>
<local:WindowToWindowStyle
x:Key="ValueConverter_WindowToWindowStyle" />
</Window.Resources>
...
</Window>
这段代码的作用:
这里发生的事情是,我将窗口本身的引用传递给值转换器,它返回相应的样式。