如何使用SimpleIoc将ViewModel绑定到View

时间:2015-03-16 11:59:39

标签: c# wpf mvvm mvvm-light

我正在使用MVVMLight框架,我尝试在实例化View时使用依赖注入。当从服务定位器SimpleIoc中提取View时,我希望SimpleIoc创建此View的新实例并将其作为参数传递给其ViewModel。我应该如何在SimpleIoC中使用ViewModel注册View来获得这种方法?目前我按照那样做了注册:

SimpleIoc.Default.Register<MainViewModel>();

但我试着这样做:

SimpleIoc.Default.Register<IMainView, MainViewModel>();

假设MainViewModel是MainView视图模型的新实例(实现了IMainView接口)。

如何在SimpleIoC中注册View和ViewModel,如何使用其引用(使用其ViewModel)调用View应用依赖注入?

更新

我目前的代码:

ViewModelLocator.cs

public class ViewModelLocator
{

    static ViewModelLocator()
    {
        ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);

        if (ViewModelBase.IsInDesignModeStatic)
        {
            SimpleIoc.Default.Register<IDataService, Design.DesignDataService>();
        }
        else
        {
            SimpleIoc.Default.Register<IDataService, DataService>();
        }

        SimpleIoc.Default.Register<MainViewModel>();
        SimpleIoc.Default.Register<NewsViewModel>();
        SimpleIoc.Default.Register<TeamsViewModel>();
    }

    /// <summary>
    /// Gets the Main property.
    /// </summary>
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
        "CA1822:MarkMembersAsStatic",
        Justification = "This non-static member is needed for data binding purposes.")]
    public MainViewModel Main
    {
        get
        {
            return ServiceLocator.Current.GetInstance<MainViewModel>();
        }
    }

    public NewsViewModel NewsViewModel
    {
        get
        {
            return ServiceLocator.Current.GetInstance<NewsViewModel>();
        }
    }

    public TeamsViewModel TeamsViewModel
    {
        get
        {
            return ServiceLocator.Current.GetInstance<TeamsViewModel>();
        }
    }

    /// <summary>
    /// Cleans up all the resources.
    /// </summary>
    public static void Cleanup()
    {
    }
}

MainWindow.xaml

<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Skins/MainSkin.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Window.Resources>

<Grid x:Name="LayoutRoot">
    <Grid.RowDefinitions>
        <RowDefinition Height="30"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <StackPanel Orientation="Horizontal">
        <Button Height="30" Width="40" Command="{Binding OpenNews}">News</Button>
        <Button Height="30" Width="40" Margin="5,0,0,0" Command="{Binding OpenTeams}">Teams</Button>
    </StackPanel>
    <ContentPresenter  Grid.Row="1" Content="{Binding CurrentViewModel}"/>
</Grid>

MainViewModel.cs

    private ViewModelBase _currentViewModel;
    /// <summary>
    /// Contains the current view model.
    /// </summary>
    public ViewModelBase CurrentViewModel
    {
        get { return _currentViewModel; }
        set
        {
            if (_currentViewModel == value)
                return;
            _currentViewModel = value;
            RaisePropertyChanged(CurrentViewModelPropertyName);
        }
    }

    #region Commands

    public RelayCommand OpenNews { get; set; }
    public RelayCommand OpenTeams { get; set; }

    #endregion

    private void RegisterCommands()
    {
        OpenNews = new RelayCommand(ShowOpenNews);
        OpenTeams = new RelayCommand(ShowOpenTeams);

    }

    private void ShowOpenTeams()
    {
        CurrentViewModel = ServiceLocator.Current.GetInstance<TeamsViewModel>(); //MainViewModel.TeamsViewModel;
    }

    private void ShowOpenNews()
    {
        CurrentViewModel = ServiceLocator.Current.GetInstance<NewsViewModel>();
    }

的App.xaml

<Application.Resources>
    <DataTemplate DataType="{x:Type vm:NewsViewModel}">
        <view:NewsView/>
    </DataTemplate>
    <DataTemplate DataType="{x:Type vm:TeamsViewModel}">
        <view:TeamView/>
    </DataTemplate>        
    <!--Global View Model Locator-->
    <vm:ViewModelLocator x:Key="Locator"
                         d:IsDataSource="True" />
</Application.Resources>

目前我通过DataTemplates更改当前视图并更改属性CurrentViewModel。 (现在ViewModel firstand我想先查看)

我想做依赖注入并使用SimpleIOC,但我不知道如何发挥作用。

0 个答案:

没有答案