问题描述
我试图在WPF控件中以调试和发布配置的形式显示不同的视图元素,以进行测试。我已将此帖子用作指导:
I am trying to show different view elements in a WPF control in debug and release configurations for testing purposes. I have used this post as a guide:Does XAML have a conditional compiler directive for debug mode? (SO)
为了对其进行测试,我创建了一个VS2013解决方案,其中包含一个名为TestingAlternateContent的WPF应用程序项目。在我的AssemblyInfo.cs内部,添加了以下代码:
In order to test it, I have created a VS2013 solution with a single WPF app project, called TestingAlternateContent. Inside my AssemblyInfo.cs I have added the following code:
#if DEBUG
[assembly: XmlnsDefinition("debug-mode", "TestingAlternateContent")]
#endif
在我的MainWindow.xaml中已经创建了一个简单的代码示例来测试此行为,如下所示:
In my MainWindow.xaml I have created a simple code sample to test this behaviour as follows:
<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下测试了相同的代码,但没有一个起作用。
我想念什么?任何人都知道这里有什么问题,或者可以发布工作代码作为参考?
While testing this, I always see the window with the "Release mode here!" message, regardless which configuration (Debug, Relase) I am using. I have checked that the AssemblyInfo #if DEBUG is working propperly, changing accordingly when I change between Debug/Release configurations.I have tested the same code under VS2008/VS2013 with .NET Framework 3.5/4.5 versions, and none have worked.What am I missing? Anyone knows what is wrong here or can post a working code as reference?
推荐答案
问题是 XmlnsDefinitionAttribute
是在解析XAML之后进行解析的,因此它不适用于同一程序集。
The problem is that the XmlnsDefinitionAttribute
is parsed after the XAML is parsed, so it doesn't work for the same assembly.
但是,您可以使 XmlnsDefinition
在您解决方案中的任何其他(引用)项目中,它将起作用
You can however, make that XmlnsDefinition
in any other (referenced) project in your solution, and it'll work
即:
- ProjectA(命名空间:
TestingAlternateContent
)
- 包含您的
MainWindow.Xaml
- 参考项目B
ProjectB
-
包含
XmlsDefinitionAttribute
,其命名空间为TestingAlternateContent
:
#if DEBUG [assembly: XmlnsDefinition("debug-mode", "TestingAlternateContent")] #endif
- 包含您的
我只是teste d,并且运行良好,无需对程序集属性声明或Xaml进行任何修改,只需将其添加到其他项目中即可。
I just tested it, and it works fine, no modifications to either the assembly attribute declaration or to the Xaml, just adding it on a different project
这篇关于WPF AlternateContent不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!