[英]How to use various connection strings in WPF and DataSet in Debug and Release mode?
[英]How can I have WPF use one window style for Debug mode and another for Release mode?
我的窗口有兩種不同的樣式:
對於我的開發機器上的任何一個顯示器,窗口太寬,但它非常適合目標/安裝機器。 因此,在調試時,我需要能夠移動窗口以便我可以看到它上面的所有內容,但是當我發布應用程序時,我需要它以“全屏”模式運行(就像投影儀模式下的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。
在XAML中可能很難做到,但在實際代碼中,您可以執行以下操作:
#if DEBUG
window.Style = WindowStyles.Regular;
#endif
為什么不把它放在執行普通XAML代碼后執行的地方?
你可以像這樣創建一個標記擴展:
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}">
您可以在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>
作為旁注,這可能不適用於silverlight - AFIK assembly:XmlnsDefinition
不支持XmlnsDefinition
你能用#if DEBUG將屬性設置為不同的值並綁定到它嗎?
也許
#if DEBUG
style = 0;
#else
style = 1;
#endif
(請記住我這里沒有VS.)並使用一個值轉換器。
很多有用的答案......我想到了另一個想法,我以為我會扔掉那里:一個值轉換器加上一個綁定:
這是價值轉換器:
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>
這是做什么的:
所以,這里發生的是我將對Window本身的引用傳遞給值轉換器,並返回相應的樣式。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.