2010-12-06 95 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