2009-07-17 61 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; 
     } 

但是,如果我做這個工作,圍繞讓我加載視圖到它調用的視圖模型,它出版的事件的命令的區域,然後我取消激活的觀點,那麼它的工作原理:

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

視圖模型:

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創建類級別的實例變量。