【问题标题】:WPF AlternateContent not workingWPF AlternateContent 不起作用
【发布时间】:2016-03-22 09:03:45
【问题描述】:

我试图在调试和发布配置中的 WPF 控件中显示不同的视图元素以进行测试。我以这篇文章为指导: Does XAML have a conditional compiler directive for debug mode? (SO)

为了测试它,我创建了一个带有单个 WPF 应用程序项目的 VS2013 解决方案,称为 TestingAlternateContent。在我的 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 是否正常工作,当我在调试/发布配置之间进行更改时会相应更改。 我已经在 VS2008/VS2013 下使用 .NET Framework 3.5/4.5 版本测试了相同的代码,但没有一个有效。 我错过了什么?有人知道这里出了什么问题,或者可以发布一个工作代码作为参考吗?

【问题讨论】:

    标签: c# .net wpf xaml


    【解决方案1】:

    问题是XmlnsDefinitionAttribute是在解析XAML之后解析的,所以对同一个程序集不起作用。

    但是,您可以在解决方案中的任何其他(引用)项目中创建 XmlnsDefinition,它会起作用

    即:

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

      • 包含XmlsDefinitionAttribute,命名空间为TestingAlternateContent

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

    我刚刚测试了它,它工作正常,没有修改程序集属性声明或 Xaml,只是将它添加到不同的项目中

    【讨论】:

    • 谢谢,在此期间我已经阅读了类似的内容,我将在我的案例中对此进行测试,如果可行,我会将您的解决方案标记为已接受
    • 您的解决方案很有魅力!非常感谢,非常有用的技巧,为我节省了很多时间:)
    【解决方案2】:

    不幸的是,我认为 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>
    

    这里的附加属性在 MainWindow 类中,但它可以在任何你想要的实用程序类中。

    using System.Windows;
    
    namespace DebugTest
    {
        public partial class MainWindow : Window
        {
    
            public static bool GetIsDebugOnly(DependencyObject obj)
            {
                return (bool)obj.GetValue(IsDebugOnlyProperty);
            }
            public static void SetIsDebugOnly(DependencyObject obj, bool value)
            {
                obj.SetValue(IsDebugOnlyProperty, value);
            }
            public static readonly DependencyProperty IsDebugOnlyProperty = DependencyProperty.RegisterAttached("IsDebugOnly", typeof(bool), typeof(MainWindow), new PropertyMetadata(false, new PropertyChangedCallback((s, e) =>
            {
                UIElement sender = s as UIElement;
                if (sender != null && e.NewValue != null)
                {
                    bool value = (bool)e.NewValue;
                    if (value)
                    {
    #if DEBUG
                        bool isDebugMode = true;
    #else
                        bool isDebugMode = false;
    #endif
    
                        sender.Visibility = isDebugMode ? Visibility.Visible : Visibility.Collapsed;
                    }
                }
            })));
    
            public MainWindow()
            {
                InitializeComponent();
            }
        }
    
    }
    

    【讨论】:

    • Tnaks 的建议,这对我来说是另一个尝试,但在这种情况下,我更喜欢使用分隔块(这种情况是具有一些不同元素的网格,所以主要是行/列更改和定义附加元素)。我已经测试了你的解决方案并且工作正常,所以如果其他方法不起作用,它可以用来解决我的问题:)
    猜你喜欢
    • 1970-01-01
    • 2013-02-08
    • 2010-12-22
    • 2017-01-28
    • 1970-01-01
    • 2011-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多