1

WPF で、「ユーザー構成」、「システム構成」、「アカウント構成」の 3 つの UserControls を作成しました。これらすべてのユーザー コントロールには、[保存] ボタンと [キャンセル] ボタンがあります。これらのボタンをクリックすると、それぞれのクラスで宣言および定義されたルーティング イベントが発生します。保存ボタンをクリックすると「ConfigurationSaved」イベントが発生し、キャンセル ボタンをクリックすると「ConfigurationCancelled」イベントが発生します。

これらのイベントが発生すると、ユーザー コントロールをホストするコンテナーが構成を保存します。

すべてのクラスのルーティング イベント定義のコード スニペットは次のとおりです。

AccountConfigurationView:

public partial class AccountConfigurationView : UserControl
{
    static AccountConfigurationView()
    {
        ConfigurationSavedEvent = EventManager.RegisterRoutedEvent("ConfigurationSaved",
        RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(AccountConfigurationView));

        ConfigurationClosedEvent = EventManager.RegisterRoutedEvent("ConfigurationClosed",
        RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(AccountConfigurationView));
    }

    #region ROUTED_EVENTS_RELATED
    public static readonly RoutedEvent ConfigurationSavedEvent;
    public static readonly RoutedEvent ConfigurationClosedEvent;

    public event RoutedEventHandler ConfigurationSaved
    {
        add { AddHandler(ConfigurationSavedEvent, value); }
        remove { RemoveHandler(ConfigurationSavedEvent, value); }
    }

    public event RoutedEventHandler ConfigurationClosed
    {
        add { AddHandler(ConfigurationClosedEvent, value); }
        remove { RemoveHandler(ConfigurationClosedEvent, value); }
    }
    #endregion
}

システム構成ビュー:

public partial class SystemConfigurationView : UserControl
{
    static SystemConfigurationView()
    {
        ConfigurationSavedEvent = EventManager.RegisterRoutedEvent("ConfigurationSaved",
        RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(SystemConfigurationView));

        ConfigurationClosedEvent = EventManager.RegisterRoutedEvent("ConfigurationClosed",
        RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(SystemConfigurationView));
    }

    #region ROUTED_EVENTS_RELATED
    public static readonly RoutedEvent ConfigurationSavedEvent;
    public static readonly RoutedEvent ConfigurationClosedEvent;

    public event RoutedEventHandler ConfigurationSaved
    {
        add { AddHandler(ConfigurationSavedEvent, value); }
        remove { RemoveHandler(ConfigurationSavedEvent, value); }
    }

    public event RoutedEventHandler ConfigurationClosed
    {
        add { AddHandler(ConfigurationClosedEvent, value); }
        remove { RemoveHandler(ConfigurationClosedEvent, value); }
    }
    #endregion
}

ユーザー構成ビュー:

public partial class UserConfigurationView : UserControl
{
    static UserConfigurationView()
    {
        ConfigurationSavedEvent = EventManager.RegisterRoutedEvent("ConfigurationSaved",
        RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(UserConfigurationView));

        ConfigurationClosedEvent = EventManager.RegisterRoutedEvent("ConfigurationClosed",
        RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(UserConfigurationView));
    }

    #region ROUTED_EVENTS_RELATED
    public static readonly RoutedEvent ConfigurationSavedEvent;
    public static readonly RoutedEvent ConfigurationClosedEvent;

    public event RoutedEventHandler ConfigurationSaved
    {
        add { AddHandler(ConfigurationSavedEvent, value); }
        remove { RemoveHandler(ConfigurationSavedEvent, value); }
    }

    public event RoutedEventHandler ConfigurationClosed
    {
        add { AddHandler(ConfigurationClosedEvent, value); }
        remove { RemoveHandler(ConfigurationClosedEvent, value); }
    }
    #endregion
}

これらのクラスを使用していると、次のメッセージで TypeInitializationException が発生します。

OwnerType 'baskcode.Admin.Controls.AccountConfigurationView' の RoutedEvent 名 'ConfigurationSaved' は既に使用されています。

他のコントロールを読み込もうとすると、同じ例外がスローされます。問題を修正することはできません。この点で私を助けてください。

私は.Netバージョン4を使用しています

ありがとう。

4

2 に答える 2

4

EventManager.RegisterRoutedEvent引数を確認してください。別のファイルからコピーして貼り付けた場合、所有者の種類を変更するのを忘れがちです。例えば:

public class UserControlA
{
  public static readonly RoutedEvent ClickEvent =
    EventManager.RegisterRoutedEvent(
      "Click",
      RoutingStrategy.Bubble,
      typeof(RoutedEventHandler),
      typeof(UserControlA));
}

ただし、それをコピーして別のファイルに貼り付けます。以下の誤ったtypeof(UserControlA)に注意してください。これは、 typeof(UserControlB)である必要があります。

public class UserControlB
{
  public static readonly RoutedEvent ClickEvent =
    EventManager.RegisterRoutedEvent(
      "Click",
      RoutingStrategy.Bubble,
      typeof(RoutedEventHandler),
      typeof(UserControlA));
}
于 2013-02-17T17:48:21.103 に答える
1

コントロールを複数回初期化していると思います。同じルーティング イベント名を複数回登録することはできません。

これを試してください(1つRoutedEvent):

RoutedEvent[] x = EventManager.GetRoutedEventsForOwner(typeof(UserConfigurationView));

if(x == null) {
    ConfigurationSavedEvent = EventManager.RegisterRoutedEvent(
        "ConfigurationSaved",
        RoutingStrategy.Bubble,
        typeof(RoutedEventHandler),
        typeof(UserConfigurationView)
    );
}

そしてこれ(複数のインスタンスの場合):あなたの場合

using System.Linq;

...

RoutedEvent[] x = EventManager.GetRoutedEvents();

if(!x.Contains(ConfigurationSaved)) {
    ConfigurationSavedEvent = EventManager.RegisterRoutedEvent(
        "ConfigurationSaved",
        RoutingStrategy.Bubble,
        typeof(RoutedEventHandler), 
        typeof(UserConfigurationView)
    );
}
于 2011-05-21T16:38:21.997 に答える