C# WPF备用内容不工作

C# WPF备用内容不工作,c#,.net,wpf,xaml,C#,.net,Wpf,Xaml,出于测试目的,我试图在调试和发布配置中显示WPF控件中的不同视图元素。我用这篇文章作为指导: 为了测试它,我创建了一个VS2013解决方案,其中包含一个名为TestingAlternateContent的WPF应用程序项目。在my AssemblyInfo.cs中,我添加了以下代码: #if DEBUG [assembly: XmlnsDefinition("debug-mode", "TestingAlternateContent")] #endif 在我的MainWindow.x

出于测试目的,我试图在调试和发布配置中显示WPF控件中的不同视图元素。我用这篇文章作为指导:

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

#if DEBUG
    [assembly: XmlnsDefinition("debug-mode", "TestingAlternateContent")]
#endif
在我的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>

在测试时,我总是看到带有“releasemodehere!”消息的窗口,不管我使用的是哪种配置(Debug、Relase)。我已经检查了AssemblyInfo#如果调试正常工作,当我在调试/发布配置之间进行更改时会相应地更改。 我在VS2008/VS2013下用.NET Framework 3.5/4.5版本测试了相同的代码,但都没有成功。
我错过了什么?任何人都知道这里出了什么问题,或者可以发布一个工作代码作为参考?

我不认为XAML设计器有一个好的编译器指令不幸的是,我使用一个附加属性来更改可见性属性,实现了预期的结果,这非常好,因为它也显示在设计器中

<Window x:Class="DebugTest.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:local="clr-namespace:DebugTest"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Button local:MainWindow.IsDebugOnly="True" Width="100" Height="100" Content="Debug only"/>
</Grid>

问题是解析XAML后会解析
xmlnDefinitionAttribute
,因此它不适用于同一程序集

但是,您可以在解决方案中的任何其他(引用的)项目中使用
XmlnsDefinition
,这样它就可以工作了

即:

  • ProjectA(命名空间:
    TestingAlternateContent
    • 包含您的
      main窗口.Xaml
    • 参考项目B
  • 项目B

    • 包含命名空间为
      TestingAlternateContent
      XmlsDefinitionAttribute

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

我刚刚对它进行了测试,它工作得很好,没有对程序集属性声明或Xaml进行任何修改,只是将它添加到另一个项目上

Tnaks作为建议,这是我的另一次尝试,但在这种情况下,我更喜欢使用分离块(案例是一个包含一些不同元素的网格,因此主要是行/列更改并定义其他元素)。我已经测试了您的解决方案,效果很好,因此如果其他方法不起作用,它可以用来解决我的问题:)谢谢,我同时也读了一些类似的内容,我将在我的案例中对此进行测试,如果它有效,我会将您的解决方案标记为已接受您的解决方案作为一个魅力!非常感谢,真的很有用的技巧,节省了我很多时间:)
#if DEBUG
[assembly: XmlnsDefinition("debug-mode", "TestingAlternateContent")]
#endif