[Catel]如何将带有构造函数参数的ViewModel传递给TabServiceExtensions方法?

时间:2017-02-03 00:48:35

标签: c# wpf mvvm catel

所以我上周开始使用Catel,但是我在使用标签界面时遇到了麻烦。我一直在使用以下资源使用带有MVVM的选项卡式界面https://catelproject.atlassian.net/wiki/display/CTL/Using+a+tabbed+interface+with+MVVM#UsingatabbedinterfacewithMVVM-CreatingClosableTabItem):

我有一个MainWindow(catel:Window),它包含一个带 xmlns的TabControl:controls =" clr-namespace:AutoProgram.UI.Controls"

<Border Background="#50FFFFFF" BorderBrush="{StaticResource WindowFrameBrush}" BorderThickness="5" Margin="-6" Padding="0">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="30" />
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>
        <Border Grid.Row="0" Grid.Column="0" Background="{StaticResource WindowFrameBrush}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" CornerRadius="0,0,0,0" Margin="0" Padding="0">
            <Grid>
                <TextBlock Foreground="White" FontWeight="Bold" VerticalAlignment="Center" Margin="10,2,10,2" Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}, Path=Title}"/>
                <Button Content="X" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="1" FontSize="7" Width="15" Height="15" Padding="0" Command="ApplicationCommands.Close"/>
            </Grid>
        </Border>
        <catel:StackGrid Grid.Row="1" Grid.Column="0">
            <catel:StackGrid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
            </catel:StackGrid.RowDefinitions>
            <catel:StackGrid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
            </catel:StackGrid.ColumnDefinitions>

            <ItemsControl x:Name="ItemsControlAutomators" Grid.Row="0" Grid.Column="0" ItemsSource="{Binding Automators}">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <UniformGrid Columns="3" />
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Button x:Name="Button" Content="{Binding Automator.Name, Mode=OneWay}" Command="{Binding ElementName=ItemsControlAutomators, Path=DataContext.RunAutomator}" CommandParameter="{Binding}"></Button>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </catel:StackGrid>

        <catel:TabControl x:Name="TabControlAutomators" Grid.Row="2" Grid.Column="0" Margin="-2" LoadTabItems="LazyLoading">
            <TabControl.ItemTemplate>
                <DataTemplate>
                    <controls:ClosableTabItem Title="{Binding ViewModel.Title}" CanClose="{Binding CanClose}" />
                </DataTemplate>
            </TabControl.ItemTemplate>

            <TabControl.ContentTemplate>
                <DataTemplate>
                    <ContentControl Content="{Binding ViewModel, Converter={catel:ViewModelToViewConverter}}" />
                </DataTemplate>
            </TabControl.ContentTemplate>
        </catel:TabControl>
    </Grid>
</Border>

相关的MainWindow.xaml.cs

public partial class MainWindow
{
    public MainWindow()
    {
        InitializeComponent();

        var serviceLocator = this.GetServiceLocator();
        var tabService = serviceLocator.ResolveType<ITabService>() as TabService;

        tabService?.SetTabControl(TabControlAutomators);
    }
}

相关的MainWindowViewModel.cs和失败位置

public class MainWindowViewModel : ViewModelBase
{
    private readonly IAutomatorService _automatorService;
    private readonly ITabService _tabService;

    public MainWindowViewModel(IAutomatorService automatorService, ITabService tabService)
    {
        Argument.IsNotNull(() => automatorService);
        Argument.IsNotNull(() => tabService);

        _automatorService = automatorService;
        _tabService = tabService;

        RunAutomator = new Command<AutomatorModel>(OnRunAutomator, OnRunAutomatorCanExecute);
    }

    public override string Title => "AutoProgram";

    public ObservableCollection<AutomatorModel> Automators
    {
        get { return GetValue<ObservableCollection<AutomatorModel>>(AutomatorsProperty); }
        set { SetValue(AutomatorsProperty, value); }
    }

    public static readonly PropertyData AutomatorsProperty = RegisterProperty("Automators", typeof(ObservableCollection<AutomatorModel>), () => new ObservableCollection<AutomatorModel>());

    public Command<AutomatorModel> RunAutomator { get; private set; }

    public async void OnRunAutomator(AutomatorModel automatorModel)
    {
        Debug.WriteLine($"NAME: {automatorModel.Automator.Name}");
        _tabService.AddAndActivate<AutomatorViewModel>(new AutomatorViewModel(automatorModel), true);   // Throws a null exception in TabItem.cs
        //_tabService.AddAndActivate<AutomatorViewModel>(new AutomatorViewModel(), true);   // But this works (sort of, see bottom error).
    }

}

TabServiceExtensions.cs

public static class TabServiceExtensions
{
    public static TabItem Add<TViewModel>(this ITabService tabService, object dataContext = null, bool canClose = false)
        where TViewModel : IViewModel
    {
        Argument.IsNotNull(() => tabService);

        var tabItem = CreateTabItem<TViewModel>(tabService, dataContext);
        tabItem.CanClose = canClose;

        tabService.Add(tabItem);

        return tabItem;
    }

    public static TabItem AddAndActivate<TViewModel>(this ITabService tabService, object dataContext = null, bool canClose = false)
        where TViewModel : IViewModel
    {
        Argument.IsNotNull(() => tabService);

        var tabItem = Add<TViewModel>(tabService, dataContext, canClose);
        tabService.Activate(tabItem);

        return tabItem;
    }

    public static TabItem CreateTabItem<TViewModel>(this ITabService tabService, object dataContext)
        where TViewModel : IViewModel
    {
        Argument.IsNotNull(() => tabService);

        var dependencyResolver = tabService.GetDependencyResolver();
        var viewModelFactory = dependencyResolver.Resolve<IViewModelFactory>();
        var vm = viewModelFactory.CreateViewModel<TViewModel>(typeof(TViewModel), dataContext);

        return new TabItem(vm);
    }

    public static void AddAndActivate(this ITabService tabService, TabItem tabItem)
    {
        Argument.IsNotNull(() => tabService);
        Argument.IsNotNull(() => tabItem);

        tabService.Add(tabItem);
        tabService.Activate(tabItem);
    }
}

TabItem.cs

public class TabItem
{
    public TabItem(IViewModel viewModel)
    {
        Argument.IsNotNull(() => viewModel);

        ViewModel = viewModel;
        CanClose = true;

        if (!viewModel.IsClosed)
        {
            viewModel.ClosedAsync += OnViewModelClosed;
        }
    }

    public IViewModel ViewModel { get; private set; }

    public bool CanClose { get; set; }

    public object Tag { get; set; }

    public event EventHandler<EventArgs> Closed;

    private async Task OnViewModelClosed(object sender, ViewModelClosedEventArgs e)
    {
        var vm = ViewModel;
        if (vm != null)
        {
            vm.ClosedAsync -= OnViewModelClosed;
        }

        Closed.SafeInvoke(this);
    }
}

我希望TabItems是AutomatorViewModels。后者初始化为:

    public AutomatorViewModel(AutomatorModel automatorModel)
    {
        Title = "Test";
    }

但是上面的代码在TabItem.cs中抛出了一个null异常。如果我省略构造函数参数,即将其更改为 public AutomatorViewModel() 使用&#34;测试&#34;创建标签。标题。虽然在这种情况下手动关闭这些选项卡时会出现以下错误: System.Windows.Data错误:4:找不到用于引用绑定的源&#39; RelativeSource FindAncestor,AncestorType =&#39; System.Windows.Controls.TabControl&#39;,AncestorLevel =&#39; 1&#39; &#39 ;. BindingExpression:路径= TabStripPlacement;的DataItem = NULL;目标元素是&#39; TabItem&#39; (名称=&#39;&#39);目标财产是“NoTarget&#39; (键入&#39;对象&#39;)

App.xaml.cs

    protected override void OnStartup(StartupEventArgs e)
    {
        LogManager.AddDebugListener();

        Log.Info("Starting application");

        StyleHelper.CreateStyleForwardersForDefaultStyles();

        var serviceLocator = ServiceLocator.Default;
        serviceLocator.RegisterType<IAutomatorService, AutomatorService>();
        serviceLocator.RegisterType<ITabService, TabService>();

        // SOME THINGS I'VE TRIED/CURRENTLY TRYING.
        //var dependencyResolver = this.GetDependencyResolver();
        //var viewModelLocator = dependencyResolver.Resolve<IViewModelLocator>();
        //viewModelLocator.Register<AutomatorView, AutomatorViewModel>();
        //viewModelLocator.Register(typeof(AutomatorView), typeof(AutomatorViewModel));
        //viewModelLocator.Register<MainWindow, MainWindowViewModel>();

        Log.Info("Calling base.OnStartup");
        base.OnStartup(e);
    }

Catel调试信息:

11:22:39:117 =&gt; [DEBUG] [Catel.MVVM.ViewModelBase] [8]创建类型&#39; AutomatorViewModel&#39;的视图模型。具有唯一标识符2

11:22:39:117 =&gt; [DEBUG] [Catel.MVVM.ViewModelCommandManager] [8]为视图模型创建ViewModelCommandManager&#39; AutoProgram.UI.ViewModels.AutomatorViewModel&#39;具有唯一标识符&#39; 2&#39;

11:22:39:118 =&gt; [DEBUG] [Catel.MVVM.ViewModelCommandManager] [8]为视图模型创建了一个ViewModelCommandManager&#39; AutoProgram.UI.ViewModels.AutomatorViewModel&#39;具有唯一标识符&#39; 2&#39;

11:22:39:119 =&gt; [DEBUG] [Catel.MVVM.ManagedViewModel] [8]添加了视图模型实例,当前包含&#39; 1&#39;类型&#39; AutoProgram.UI.ViewModels.AutomatorViewModel&#39;

的实例

11:22:39:123 =&gt; [DEBUG] [Catel.IoC.TypeFactory] ​​[8]创建类型&#39; AutoProgram.UI.ViewModels.AutomatorViewModel&#39;使用特定参数。在缓存中找不到构造函数,因此搜索正确的

11:22:39:124 =&gt; [DEBUG] [Catel.IoC.TypeFactory] ​​[8]检查构造函数&#39; public ctor(AutomatorModel automatorModel)&#39;可以使用

11:22:39:126 =&gt; [DEBUG] [Catel.IoC.TypeFactory] ​​[8]构造函数无效,因为值&#39; AutoProgram.UI.ViewModels.AutomatorViewModel&#39;不能用于参数&#39; AutoProgram.UI.ViewModels.AutomatorViewModel&#39;

11:22:39:126 =&gt; [DEBUG] [Catel.IoC.TypeFactory] ​​[8]构造函数有效且可以使用

11:22:39:127 =&gt; [DEBUG] [Catel.IoC.TypeFactory] ​​[8]无法使用构造函数,无法构造类型&#39; AutoProgram.UI.ViewModels.AutomatorViewModel&#39;使用指定的参数

11:22:39:128 =&gt; [DEBUG] [Catel.IoC.TypeFactory] ​​[8]创建类型&#39; AutoProgram.UI.ViewModels.AutomatorViewModel&#39;使用特定参数。在缓存中找不到构造函数,因此搜索正确的

11:22:39:128 =&gt; [DEBUG] [Catel.IoC.TypeFactory] ​​[8]检查构造函数&#39; public ctor(AutomatorModel automatorModel)&#39;可以使用

11:22:39:129 =&gt; [DEBUG] [Catel.IoC.TypeFactory] ​​[8]构造函数无效,因为参数&#39; automatorModel&#39;无法从依赖项解析器解析

11:22:39:129 =&gt; [DEBUG] [Catel.IoC.TypeFactory] ​​[8]构造函数有效且可以使用

11:22:39:130 =&gt; [DEBUG] [Catel.IoC.TypeFactory] ​​[8]无法使用构造函数,无法构造类型&#39; AutoProgram.UI.ViewModels.AutomatorViewModel&#39;使用指定的参数

11:22:39:130 =&gt; [DEBUG] [Catel.MVVM.ViewModelFactory] ​​[8]无法构建视图模型&#39; AutoProgram.UI.ViewModels.AutomatorViewModel&#39;使用数据上下文注入&#39; AutomatorViewModel&#39; AutoProgram.UI.vshost.exe错误:0:11:22:39:131 =&gt; [错误] [Catel.Argument] [8]参数&#39; viewModel&#39;不能为空 抛出异常:&#39; System.ArgumentNullException&#39;在Catel.Core.dll

编辑#1:

  • 添加了调试信息(取消注释 LogManager.AddDebugListener();
  • 添加了App.xaml.cs

编辑#2:

通过将视图模型的初始化更改为无构造函数,并显式设置模型属性,找到了一种解决方法,如下所示:

_tabService.AddAndActivate(new AutomatorViewModel {AutomatorModel = automatorModel},false);

1 个答案:

答案 0 :(得分:0)

您应该将 DataContext 对象,而不是 ViewModel 传递到AddAndActivate。所以这应该工作(并将为你构建和注入vm):

_tabService.AddAndActivate<AutomatorViewModel>(automatorModel, true);