dev*_*xer 7 c# wpf xaml visual-studio
我的窗口有两种不同的样式:
对于我的开发机器上的任何一个显示器,窗口太宽,但它非常适合目标/安装机器.因此,在调试时,我需要能够移动窗口以便我可以看到它上面的所有内容,但是当我发布应用程序时,我需要它以"全屏"模式运行(就像投影仪模式下的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.
在XAML中可能很难做到,但在实际代码中,您可以执行以下操作:
#if DEBUG
window.Style = WindowStyles.Regular;
#endif
Run Code Online (Sandbox Code Playgroud)
为什么不把它放在执行普通XAML代码后执行的地方?
| 归档时间: |
|
| 查看次数: |
5225 次 |
| 最近记录: |