XAML 是否有用于调试模式的条件编译器指令? [英] Does XAML have a conditional compiler directive for debug mode?

查看:53
本文介绍了XAML 是否有用于调试模式的条件编译器指令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need something like this for styles in 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>

推荐答案

我最近不得不这样做,当我无法轻松找到任何清晰的示例时,我很惊讶它是如此简单.我所做的是将以下内容添加到 AssemblyInfo.cs:

I recently had to do this and was suprised at how simple it was when I couldn't easily find any clear examples. What I did was add the following to AssemblyInfo.cs:

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

然后,使用标记兼容命名空间的 AlternateContent 标记根据命名空间定义的存在来选择您的内容:

Then, use the markup-compatability namespace's AlternateContent tag to choose your content based on the presense of that namespace definition:

<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 标记选择第一个代码块.如果未定义 DEBUG,则将使用 Fallback 代码块.

Now, when DEBUG is defined, "debug-mode" will also be defined, and the "d" namespace will be present. This makes the AlternateContent tag choose the first block of code. If DEBUG is not defined, the Fallback block of code will be used.

这个示例代码没有经过测试,但它与我在当前项目中使用的有条件地显示一些调试按钮的代码基本相同.

This sample code was not tested, but it's basically the same thing that I'm using in my current project to conditionally show some debug buttons.

我确实看到了一篇博客文章,其中包含一些依赖于Ignorable"标签的示例代码,但与这种方法相比,它似乎不太清晰和易于使用.

I did see a blog post with some example code that relied on the "Ignorable" tag, but that seemed a lot less clear and easy to use as this method.

这篇关于XAML 是否有用于调试模式的条件编译器指令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆