WPF AlternateContent不起作用

est*_*djs 4 .net c# wpf xaml

我试图在调试和发布配置中显示WPF控件中的不同视图元素以进行测试.我使用这篇文章作为指南: XAML是否有调试模式的条件编译器指令?(所以)

为了测试它,我创建了一个VS2013解决方案,其中包含一个名为TestingAlternateContent的WPF应用程序项目.在我的AssemblyInfo.cs中,我添加了以下代码:

#if DEBUG
    [assembly: XmlnsDefinition("debug-mode", "TestingAlternateContent")]
#endif
Run Code Online (Sandbox Code Playgroud)

在我的MainWindow.xaml中,我创建了一个简单的代码示例来测试此行为,如下所示:

<Window x:Class="TestingAlternateContent.MainWindow"
        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:debug="debug-mode"        
        mc:Ignorable="mc debug" 

        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <mc:AlternateContent>
            <mc:Choice Requires="debug">
                <TextBlock Text="Debug mode!!" />
            </mc:Choice>
            <mc:Fallback>
                <TextBlock Text="Release mode here!" />
            </mc:Fallback>
        </mc:AlternateContent>
    </Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)

在测试时,我总是看到窗口显示"释放模式!" 消息,无论我使用哪种配置(Debug,Relase).我已经检查过AssemblyInfo #if DEBUG正在正常工作,当我在Debug/Release配置之间进行更改时会相应地进行更改.我已经使用.NET Framework 3.5/4.5版本在VS2008/VS2013下测试了相同的代码,但没有一个有效.我错过了什么?任何人都知道这里有什么问题或者可以发布工作代码作为参考?

Jcl*_*Jcl 9

问题是在XmlnsDefinitionAttribute解析XAML之后解析,因此它不适用于同一个程序集.

但是,您可以XmlnsDefinition在解决方案中的任何其他(引用)项目中进行此操作,并且它将起作用

那是:

  • 项目A(命名空间:TestingAlternateContent)
    • 包含你的 MainWindow.Xaml
    • 参考ProjectB
  • 项目B

    • 包含以下XmlsDefinitionAttribute命名空间TestingAlternateContent:

      #if DEBUG
      [assembly: XmlnsDefinition("debug-mode", "TestingAlternateContent")]
      #endif
      
      Run Code Online (Sandbox Code Playgroud)

我只是测试了它,它工作正常,没有修改汇编属性声明或Xaml,只是将它添加到另一个项目