WPF AlternateContent不起作用

estradjs

我试图在WPF控件的调试和发布配置中显示不同的视图元素,以进行测试。我已将此帖子用作指导:XAML是否为调试模式提供了条件编译器指令?(所以)

为了对其进行测试,我使用一个名为TestingAlternateContent的WPF应用程序项目创建了VS2013解决方案。在我的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>

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

cl

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

但是,您可以XmlnsDefinition在解决方案中的任何其他(引用)项目中进行修改,然后它将起作用

那是:

  • 项目(命名空间:TestingAlternateContent
    • 包含您的 MainWindow.Xaml
    • 参考项目B
  • 项目B

    • 包含XmlsDefinitionAttribute带有的命名空间TestingAlternateContent

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

我刚刚对其进行了测试,并且工作正常,无需对程序集属性声明或Xaml进行任何修改,只需将其添加到其他项目中即可

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章