代码之家  ›  专栏  ›  技术社区  ›  Dabblernl

如何在WPF ResourceDictionary中有条件地定义资源[[副本]

  •  1
  • Dabblernl  · 技术社区  · 6 年前

    对于XAML中的样式,我需要这样的东西:

    <Application.Resources>
    
    #if DEBUG
        <Style TargetType="{x:Type ToolTip}">
            <Setter Property="FontFamily" Value="Arial"/>
            <Setter Property="FlowDirection" Value="LeftToRight"/>
        </Style>
    #else
        <Style TargetType="{x:Type ToolTip}">
            <Setter Property="FontFamily" Value="Tahoma"/>
            <Setter Property="FlowDirection" Value="RightToLeft"/>
        </Style>
    #endif
    
    </Application.Resources>
    
    0 回复  |  直到 6 年前
        1
  •  129
  •   bj0    11 年前

    我最近不得不这样做,当我找不到任何清晰的例子时,我惊讶地发现这是多么简单。我所做的是将以下内容添加到AssemblyInfo.cs:

    #if DEBUG
    [assembly: XmlnsDefinition( "debug-mode", "Namespace" )]
    #endif
    

    <Window x:Class="Namespace.Class"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:d="debug-mode"
    
            Width="400" Height="400">
    
            ...
    
            <mc:AlternateContent>
                <mc:Choice Requires="d">
                    <Style TargetType="{x:Type ToolTip}">
                        <Setter Property="FontFamily" Value="Arial"/>
                        <Setter Property="FlowDirection" Value="LeftToRight"/>
                    </Style>
                </mc:Choice>
                <mc:Fallback>
                    <Style TargetType="{x:Type ToolTip}">
                        <Setter Property="FontFamily" Value="Tahoma"/>
                        <Setter Property="FlowDirection" Value="RightToLeft"/>
                    </Style>
                </mc:Fallback>
            </mc:AlternateContent>
    
            ...
    </Window>
    

    现在,当DEBUG被定义时,“DEBUG mode”也将被定义,并且“d”名称空间将出现。这使得AlternateContent标记选择第一个代码块。如果未定义调试,则将使用回退代码块。

    这个示例代码没有经过测试,但基本上和我在当前项目中使用的有条件地显示一些调试按钮是一样的。

        2
  •  3
  •   StayOnTarget    4 年前

    这在WPF/Silverlight/WP7中是不可能的。

    有趣的是,标准文件, ISO/IEC 29500 ( Office Open XML文件格式 ),介绍了如何在XML文档中处理这个问题,XAML确实支持该规范中的一项 mc:Ignorable 我们可以这样做:

    <Page xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
          xmlns:c="Comments"
          mc:Ignorable="c">
        <Button Content="Some Text"
                c:Content="Some other text" />
    </Page>
    

    注释掉属性。

    XAML解析器团队(SL4,WP7.1,WPF)选择使用该规范来解决他们忽略属性的需求,而不仅仅是编造一些东西。这就是为什么一些默认XAML页面定义了“mc”命名空间。我确实认为,如果有一天XAML支持规范中允许加载备用内容的其余部分,那就太酷了。

    这个 mc:可忽略

        3
  •  2
  •   Josh C.    13 年前

    你可以使用一个模板选择器。DataTemplateSelector类是您编写的代码。使用覆盖的模板选择方法,您可以将预处理器指令放入。

    http://msdn.microsoft.com/en-us/library/system.windows.controls.datatemplateselector.aspx

        4
  •  1
  •   Dallin Beuller    6 年前

    我觉得给出的答案不是最容易用的。下面是我使用自定义可附加依赖项属性的解决方案:

    using namespace Utility{
        public static class DebugVisibility
        {
            public static readonly DependencyProperty IsVisibleProperty = DependencyProperty.RegisterAttached(
        "Debug", typeof(bool?), typeof(DebugVisibility), new PropertyMetadata(default(bool?), IsVisibleChangedCallback));
    
            private static void IsVisibleChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
            {
                var fe = d as FrameworkElement;
                if (fe == null)
                    return;
    #if DEBUG
                fe.Visibility = Visibility.Visible;
    #else
                fe.Visibility = Visibility.Collapsed;
    #endif
            }
    
            public static void SetIsVisible(DependencyObject element, bool? value)
            {
                element.SetValue(IsVisibleProperty, value);
            }
    
            public static bool? GetIsVisible(DependencyObject element)
            {
                return (bool?)element.GetValue(IsVisibleProperty);
            }
        }
    }
    

    xaml的用法如下:

    <window ... xmlns:Util="clr-namespace:MyNamespace.Utility" >
        <Label Util:DebugVisibility.IsVisible="True">
    </window>
    

    我把它当作一个bool,以防你想在里面添加一些其他的可见性逻辑。这是一个很好的简单切换,可以绑定和附加到任何控件