2009-07-17 31 views
0

私は私の顧客モジュールにイベントを発行しようとすると、それは動作しません(加入者がオブジェクトを受信しない、何も表示されません):モジュールのコンストラクタにイベントを公開するにはどうすればよいですか?

public class CustomersRegister : IModule 
    { 
     private static IRegionManager regionManager; 
     private static IRegion region; 
     private static CustomersMainView view; 
     private static CustomerAllView allview; 
     private static CustomerEdit editView; 

     public CustomersRegister(IRegionManager manager) 
     { 
      regionManager = manager; 
     } 

     public void Initialize() 
     { 
      StackPanel sp = LoadNavigation(); 
      sp.PublishEvent(PublishEventNames.AddCustomerMenu); 
     } 

     public static StackPanel LoadNavigation() 
     { 
      StackPanel sp = new StackPanel(); 
      sp.Margin = new Thickness { Left = 10.0, Top = 5.0, Right = 10.0 }; 
      sp.Children.Add(new CustomerMainMenu()); 
      return sp; 
     } 

しかし、私はそのように私は周りにこの作業を行う場合イベントを発行のviewmodel、中にコマンドを呼び出す領域にビューをロードした後、私はビューを非アクティブ、それは動作します:

public class CustomersRegister : IModule 
{ 
    private static IRegionManager regionManager; 
    private static IRegion region; 
    private static CustomersMainView view; 
    private static CustomerAllView allview; 
    private static CustomerEdit editView; 

    public CustomersRegister(IRegionManager manager) 
    { 
     regionManager = manager; 
    } 

    public void Initialize() 
    { 
     region = regionManager.Regions[RegionNames.DockManagerContainer]; 
     view = new CustomersMainView(); 
     view.DataContext = new ViewModels.CustomersMainViewModel(); 
     region.Add(view); 
     region.Activate(view); 
    } 

ビュー:

<UserControl x:Class="CustomersModul.Views.CustomersMainView" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:ncal="http://infragistics.com/ncal" 
    xmlns:Commands="clr-namespace:CRMInfrastructure.Commands;assembly=CRMInfrastructure" 
    ncal:XamDockManagerSettings.IsContentPaneInDocumentHost="True"    
    Commands:AvarioCommandBehavior.TheCommandToRun="{Binding LoadNavigation}" 
    Commands:AvarioCommandBehavior.RoutedEventName="Loaded" 
    Commands:AvarioCommandBehavior.CommandParameter="loading"> 
... 

のViewModel:

using System.Windows.Input; 
using CRMInfrastructure; 
using CRMInfrastructure.Commands; 
using CRMInfrastructure.Helpers; 

namespace CustomersModul.ViewModels 
{ 
    public class CustomersMainViewModel : ViewModelBase 
    { 
    public ICommand LoadNavigation { get; set; } 

    public CustomersMainViewModel() 
    { 
     LoadNavigation = new DelegateCommand<object>(load); 
    } 

    void load(object o) 
    { 
     CustomersNavigation.LoadNavigation().PublishEvent(PublishEventNames.AddCustomerMenu); 
     CustomersRegister.DeactivateView(); 
    } 
    } 
} 

は、どのように私は単に代わりに、この奇妙な回避策を行うためのモジュールのコンストラクタでイベントを公開することができますか?

答えて

0

イベントが伝播される前にStackPanel変数が有効範​​囲外になっているように見えます。これは、ViewModelをリージョンにロードし、パブリッシュメソッドへの静的呼び出しを使用すると、動作が異なる理由を説明します。

IModuleクラスのStackPanelのクラスレベルのインスタンス変数を作成してみてください。

関連する問題