0

簡潔なタイトルを思いつくのは難しいかもしれません!

1 つのソリューションに 2 つの WPF プロジェクトがあります。最初に、WpfApplication は Class1 を定義します。2 番目の WpfControlLibrary は、Class1 から継承する Class2、Class2 から継承する Class3、および UserControl1 を定義します。以下にそれらを示します。

WpfApplication Class1

namespace WpfApplication5
{
  public class Class1
  {
    public string Property1 { get; set; }
  }
}

WpfControlLibrary Class2 および Class3

namespace WpfControlLibrary1
{
  public class Class2
  {
    public string Property2 { get; set; }
  }
  public class Class3 : Class2
  {
    public string Property3 { get; set; }
  }
}

要約すると、クラス 1 には Property1 があり、Class2 には Property1 (継承による) と Property2 があり、Class3 には Property1 と Property2 (両方とも継承による) と Property3 があります。ユーザー コントロールの XAML は次のとおりです。

<UserControl x:Class="WpfControlLibrary1.UserControl1"
         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:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:b="clr-namespace:WpfApplication5;assembly=WpfApplication5"
         xmlns:l="clr-namespace:WpfControlLibrary1"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
  <UserControl.Resources>
      <b:Class1 x:Key="test1" Property1="xxx" />
      <l:Class2 x:Key="test2" Property1="xxx" Property2="yyy" />
      <l:Class3 x:Key="test3" Property2="yyy" Property3="zzz" />
      <l:Class3 x:Key="test4">
        <l:Class3.Property1>xxx</l:Class3.Property1>
        <l:Class3.Property2>yyy</l:Class3.Property2>
        <l:Class3.Property3>zzz</l:Class3.Property3>
      </l:Class3>
  </UserControl.Resources>
  <Grid>

  </Grid>

問題は、XAML がコンパイルされず、次のエラーが発生することです。

The property 'Property1' does not exist in XML namespace 'clr-namespace:WpfControlLibrary1'.
The property 'Property1' was not found in type 'Class2'.
The attachable property 'Property1' was not found in type 'Class3'.

XAML は、別のアセンブリから継承されたプロパティを処理できないようです。そうですか、それとも私があまりにも長い間それを見てきましたか。Property1 と event を設定するさまざまな形式を試しましたが、どれも機能しb:Property1=b:Class1.Property1=いないようです。それとも、あまりにも長くて複雑すぎるものを探していたのでしょうか?

4

1 に答える 1

0

上記で引用した私の悪い例にもかかわらず、私が起こったと思うことを説明するために、ここに私自身の答えを追加します。

XAML を使用して別のアセンブリからオブジェクトをインスタンス化すると、名前空間は次のようになります。

xmlns:b="clr-namespace:WpfApplication5;assembly=WpfApplication5"

という名前のアセンブリがあることに注意してください。また、アセンブリを bin フォルダーにコピーするようです。私の場合、おかしなことが起こり、コピーされたアセンブリは数時間前のものでした。したがって、C# はコンパイルできても、XAML はコンパイルできませんでした。このアセンブリのビルドが構成マネージャーで不思議なことにチェックされていないという魔法が起こりました。ただし、再度確認して再構築しても、exeは再構築されませんでした。この興奮の間に、Visual Studio がクラッシュして再起動しました (これは私だけですか、それとも VS2010 ではよくあることですか)。

最終的に、すべての DLL プロジェクトを削除し、メイン アプリを単独でビルドする必要がありました。その後、DLL プロジェクトを再度追加してコンパイルできます。その後、すべてが期待どおりに機能しました。

于 2012-06-28T12:35:48.083 に答える