2010-12-06 83 views
3

我是MEF的新手,並試圖找出如何構建我的棱鏡4.0應用程序連接視圖查看模型。我的用例是我有一個用戶控件嵌套在另一個用戶控件中。我想將嵌套的用戶控件連接到它的視圖模型。我試圖遵循Prism 4.0的例子,但不確定我是否使用MEF最佳實踐。連接嵌套視圖查看模型在棱鏡4.0和MEF

以下是我的應用程序中的一些片段來演示此問題。 HomeView有一個名爲HelloView的嵌套用戶控件。我需要將HelloView連接到名爲HelloViewModel的視圖模型。處於當前狀態的代碼不起作用。我認爲HelloView不是由MEF構建的,因此HelloViewModel沒有連接。

***** HomeModule ***** 
[ModuleExport(typeof(HomeModule))] 
public class HomeModule : IModule 
{ 
    IRegionManager _regionManager; 

    [ImportingConstructor] 
    public HomeModule(IRegionManager regionManager) 
    { 
     _regionManager = regionManager; 
    } 

    public void Initialize() 
    { 
     // Create the view 
     IHomeView homeView = ServiceLocator.Current.GetInstance<IHomeView>(); 

     // Add it to the region 
     IRegion region = _regionManager.Regions["MainRegion"]; 
     region.Add(homeView, "HomeView"); 
     region.Activate(homeView); 
    } 
} 


****** IHomeView ***** 
public interface IHomeView 
{ 
} 


***** HomeView.xaml ***** 
<UserControl ...> 

    <Grid x:Name="LayoutRoot"> 
     <view:HelloView x:Name="helloView"/> 
    </Grid> 

</UserControl> 


***** HomeView.xaml.cs ***** 
[Export(typeof(IHomeView))] 
[PartCreationPolicy(CreationPolicy.NonShared)] 
public partial class HomeView : UserControl, IHomeView 
{ 
    public HomeView() 
    { 
     InitializeComponent(); 
    } 
} 


***** IHelloView ***** 
public interface IHelloView 
{ 
} 


***** HelloView.xaml ***** 
<UserControl ...> 
    <StackPanel x:Name="LayoutRoot" Margin="10"> 
     <StackPanel Orientation="Horizontal" VerticalAlignment="Top"> 
      <TextBlock Text="Name" VerticalAlignment="Center" /> 
      <TextBox Width="100" VerticalAlignment="Center" Margin="10 0 0 0" 
        Text="{Binding Path=Name, Mode=TwoWay}" /> 
      <Button Content="Submit" VerticalAlignment="Center" Margin="10 0 0 0" 
        Command="{Binding SubmitCommand}"/> 
     </StackPanel> 
     <TextBlock Text="{Binding Message}" Margin="0 10 0 0" Foreground="Red" /> 
    </StackPanel> 
</UserControl> 

***** HelloView.xaml.cs ***** 
[Export(typeof(IHelloView))] 
[PartCreationPolicy(CreationPolicy.NonShared)] 
public partial class HelloView : UserControl, IHelloView 
{ 
    public HelloView() 
    { 
     InitializeComponent(); 
    } 

    [Import] 
    public IHelloViewModel ViewModel 
    { 
     set { this.DataContext = value; } 
    } 
} 


***** IHelloViewModel ***** 
public interface IHelloViewModel 
{ 
} 


***** HelloViewModel ***** 
[Export(typeof(IHelloViewModel))] 
[PartCreationPolicy(CreationPolicy.NonShared)] 
public class HelloViewModel : NotificationObject, IHelloViewModel 
{ 
    public HelloViewModel() 
    { 
     this.SubmitCommand = new DelegateCommand<object>(this.OnSubmit); 
    } 

    private void OnSubmit(object obj) 
    { 
     Message = "Hello " + Name; 
    } 

    private string _name; 
    public string Name 
    { 
     get { return _name; } 
     set 
     { 
      if (value != _name) 
      { 
       _name = value; 
       this.RaisePropertyChanged("Name"); 
      } 
     } 
    } 

    private string _message; 
    public string Message 
    { 
     get { return _message; } 
     set 
     { 
      if (value != _message) 
      { 
       _message = value; 
       this.RaisePropertyChanged("Message"); 
      } 
     } 
    } 

    public ICommand SubmitCommand { get; private set; } 
} 

回答

1

你的解決方案是好的,我只有2注: 第一:如果您的目錄包含超過1型IHelloViewModel(最有可能是因爲你有幾個觀點和的ViewModels相應的),那麼你得到的組成錯誤,因爲導入返回多個結果。

[Import]public IHelloViewModel ViewModel 

應該像

[Import(typeof(HelloViewModel))] IHelloViewModel ViewModel 

,或者你只是讓你的財產,如:

[Import] 
public HelloViewModel ViewModel 

二: 待辦事項不使用ServiceLocator用於創建您的HomeView的。 ServiceLocator旨在創建單例實例,並且EventAggregator是完美的候選。瀏覽次數應不共享(和你正確地將其標記爲[PartCreationPolicy(CreationPolicy.NonShared)] - 否則就是要將視圖添加到您得到錯誤的另一個區域。) )

使用

[Import] 
    public HomeView HomeView 

希望這有助於。

+0

感謝grimcoder!好的提示。我認爲我原來的帖子沒有清楚說明代碼實際上不起作用 - HelloView沒有連接到HelloViewModel。我猜測,HelloView的構建不通過MEF。什麼是做這個聯結的最佳的方式? – Naresh 2010-12-07 08:43:27