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

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

问题描述

我需要在XAML像这样的风格:

 < Application.Resources>#如果DEBUG    <风格的TargetType ={X:输入工具提示}>
        < setter属性=的FontFamilyVALUE =宋体/>
        < setter属性=的FlowDirectionVALUE =LeftToRight/>
    < /样式和GT;
#其他
    <风格的TargetType ={X:输入工具提示}>
        < setter属性=的FontFamilyVALUE =宋体/>
        < setter属性=的FlowDirectionVALUE =从右至左/>
    < /样式和GT;
#万一< /Application.Resources>


解决方案

最近,我不得不这样做,它是如何的简单,当我不能很容易地找到任何明显的例子很惊讶。我所做的是以下添加到AssemblyInfo.cs中:

 #如果DEBUG
[大会:XmlnsDefinition(调试模式,命名空间)]
#万一

然后,使用标记,兼容性命名空间的AlternateContent标签基于空间定义的presense选择您的内容:

 <窗​​口x:类=Namespace.Class
        的xmlns =htt​​p://schemas.microsoft.com/winfx/2006/xaml/$p$psentation
        的xmlns:X =htt​​p://schemas.microsoft.com/winfx/2006/xaml        的xmlns:MC =htt​​p://schemas.openxmlformats.org/markup-compatibility/2006
        的xmlns:D =调试模式        宽度=400HEIGHT =400>        ...        < MC:AlternateContent>
            < MC:选择要求=D>
                <风格的TargetType ={X:输入工具提示}>
                    < setter属性=的FontFamilyVALUE =宋体/>
                    < setter属性=的FlowDirectionVALUE =LeftToRight/>
                < /样式和GT;
            < / MC:选择>
            < MC:回退>
                <风格的TargetType ={X:输入工具提示}>
                    < setter属性=的FontFamilyVALUE =宋体/>
                    < setter属性=的FlowDirectionVALUE =从右至左/>
                < /样式和GT;
            < / MC:回退>
        < / MC:AlternateContent>        ...
< /窗GT;

现在,定义调试时,调试模式也将定义,并且d的命名空间将是present。这使得AlternateContent标签选择code的第一个块。如果没有定义DEBUG,code的回退块将被使用。

本示例code未经测试,但它基本上是我用我的当前项目有条件地显示一些调试按钮一样的东西。

我没有看到一个博客张贴一些示例code,关于可忽略的标签依赖,但似乎少了很多清晰和易于因为此方法使用。

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>

解决方案

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

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>

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.

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天全站免登陆