2016-03-22 31 views
1

私は、テスト目的のために、デバッグおよびリリース構成のWPFコントロールに異なるビュー要素を表示しようとしています。私はTestingAlternateContentと呼ばれる単一のWPFアプリケーションのプロジェクトとVS2013のソリューションを、作成している、それをテストするために Does XAML have a conditional compiler directive for debug mode? (SO)WPF AlternateContentが動作しない

:私はガイドとしてこの記事を使用しています。私は、

<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> 

これをテストしている間:私は次のようにこの動作をテストするための簡単なコードサンプルを作成している私のMainWindow.xamlで

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

:私のAssemblyInfo.csの内部で、私は、次のコードを追加しました常にここに「リリースモード」と表示されます。どの構成(Debug、Relase)を使用しているかに関係なく、 AssemblyInfo #if DEBUGがデバッグ/リリース構成の間で変更されたときにそれに応じて変更されていることを確認しました。 VS2008/VS2013の下で.NET Framework 3.5/4.5バージョンで同じコードをテストしましたが、いずれも動作していません。 私は何が欠けていますか?誰でもここで間違っていることを知っているか、参照として作業コードを投稿できますか?

答えて

5

問題は、XAMLが解析された後XmlnsDefinitionAttributeが解析され、それは、同じアセンブリのために動作しないということです。

あなたはしかし、あなたのソリューション内の他の(参照)のプロジェクトでそのXmlnsDefinitionを作ることができ、そしてそれは

動作します:

  • PROJECTA(名前空間:TestingAlternateContent
    • が含まれていますMainWindow.Xaml
    • ProjectB
  • ProjectBの

    • TestingAlternateContentの名前空間にXmlsDefinitionAttributeを含みません:

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

私はちょうどそれをテストし、それが正常に動作し、いずれかへの変更をアセンブリ属性の宣言またはXamlに追加して、別のプロジェクトに追加するだけです。

+0

ありがとう、私はその間に同様のものを読んで、それが動作すれば、私はあなたの解決策を受け入れたものとしてマークします – estradjs

+1

あなたのソリューションは魅力的なものでした!ありがとう、私に多くの時間を節約する、本当に便利なトリック:) – estradjs

1

残念ながら、残念ながら、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> 

ここで添付プロパティは、メイン・ウィンドウクラスであるが、それはどこでもあなたが望むユーティリティクラスである可能性があります。

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(); 
     } 
    } 

} 
+0

私のためにもう一度試してみましたが、この場合、私は分離されたブロックを使用することをお勧めしました(大文字と小文字は異なる要素を持つグリッドなので、主に行/列が追加要素を変更して定義します)。私はあなたのソリューションをテストし、正常に動作するので、他の方法がうまくいかない場合は、私の問題を解決するために使用することができます:) – estradjs

関連する問題