Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 仅当其处于选中状态时加载选项卡ViewModel_C#_Wpf_Mvvm_Dependency Injection - Fatal编程技术网

C# 仅当其处于选中状态时加载选项卡ViewModel

C# 仅当其处于选中状态时加载选项卡ViewModel,c#,wpf,mvvm,dependency-injection,C#,Wpf,Mvvm,Dependency Injection,我有一个带有依赖注入的WPF MVVM应用程序 我有一个带有许多视图模型和视图的选项卡控件 我正在搜索仅在选择选项卡时加载视图模型。防止在启动应用程序时加载所有选项卡的所有逻辑 我想问题是当我做IoC时,所有的视图模型都被加载了 App.xaml.cs public partial class App : Application { public IConfiguration Configuration { get; set; } protected override void

我有一个带有依赖注入的WPF MVVM应用程序

我有一个带有许多视图模型和视图的选项卡控件

我正在搜索仅在选择选项卡时加载视图模型。防止在启动应用程序时加载所有选项卡的所有逻辑

我想问题是当我做IoC时,所有的视图模型都被加载了

App.xaml.cs

public partial class App : Application
{
    public IConfiguration Configuration { get; set; }

    protected override void OnStartup(StartupEventArgs e)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(Path.Combine(Directory.GetCurrentDirectory(), "Config"))
            .AddJsonFile("AppSettings.json", false, true);

        Configuration = builder.Build();

        var serviceProvider = CreateServiceProvider();

        var window = serviceProvider.GetRequiredService<MainWindow>();
        window.DataContext = serviceProvider.GetRequiredService<MaintenanceViewModel>();
        window.Show();
    }

    private IServiceProvider CreateServiceProvider()
    {
        IServiceCollection services = new ServiceCollection();

        services.RegisterLibrary();
        services.RegisterPersistence();

        services.AddSingleton<ISolidRivetsViewModelFactory, SolidRivetsViewModelFactory>();

        services.AddSingleton<CreateViewModel<CouponViewModel>>(services =>
        {
            return () => new CouponViewModel(services.GetRequiredService<IWotService>());
        });

        services.AddScoped<CouponViewModel>();
        services.AddScoped<MaintenanceViewModel>();
        services.AddScoped<MainWindow>();

        services.AddAutoMapper(x => { x.AddProfile<SolidRivetProfile>(); });

        return services.BuildServiceProvider();
    }
}
公共部分类应用程序:应用程序
{
公共IConfiguration配置{get;set;}
启动时受保护的覆盖无效(StartupEventArgs e)
{
var builder=new ConfigurationBuilder()
.SetBasePath(Path.Combine(Directory.GetCurrentDirectory(),“Config”))
.AddJsonFile(“AppSettings.json”,false,true);
Configuration=builder.Build();
var serviceProvider=CreateServiceProvider();
var window=serviceProvider.GetRequiredService();
window.DataContext=serviceProvider.GetRequiredService();
window.Show();
}
私有IServiceProvider CreateServiceProvider()
{
IServiceCollection服务=新的ServiceCollection();
services.RegisterLibrary();
services.RegisterPersistence();
services.AddSingleton();
services.AddSingleton(服务=>
{
return()=>新的CouponViewModel(services.GetRequiredService());
});
services.addScope();
services.addScope();
services.addScope();
services.AddAutoMapper(x=>{x.AddProfile();});
return services.BuildServiceProvider();
}
}
tabcontrolxaml

<UserControl x:Class="WoTHMI.Maintenance"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  xmlns:sections="clr-namespace:WoTHMI.workspaces.maintenance.sections"
  xmlns:viewmodel="clr-namespace:WoTHMI.ViewModels"
  mc:Ignorable="d" 
  d:DesignHeight="450" d:DesignWidth="1200">
<!--<TabControl TabStripPlacement="Left"
  Background="White">-->
<TabControl TabStripPlacement="Left"
            ItemsSource="{Binding TabChildren}"
            SelectedItem="{Binding SelectedTabViewModel}">
    <TabControl.Resources>
        <DataTemplate DataType="{x:Type viewmodel:CouponViewModel}">
            <sections:TestPlate />
        </DataTemplate>
    </TabControl.Resources>

    <TabControl.ItemContainerStyle>
        <Style TargetType="TabItem">
            <Setter Property="Header" Value="{Binding Header}" />
            <Setter Property="Height" Value="50" />
            <Setter Property="Width" Value="200" />
        </Style>
    </TabControl.ItemContainerStyle>
</TabControl>

选项卡控件视图模型

public class MaintenanceViewModel : ViewModelBase
{
    private readonly CouponViewModel _couponViewModel;

    public ObservableCollection<ITabViewModel> TabChildren { get; set; }

    public ITabViewModel SelectedTabViewModel { get; set; }

    public MaintenanceViewModel(CouponViewModel couponViewModel)
    {
        _couponViewModel = couponViewModel;
        _couponViewModel.Header = "TestPlate";

        TabChildren = new ObservableCollection<ITabViewModel>
        {
            _couponViewModel
        };
    }
}
公共类维护ViewModel:ViewModelBase { 私有只读CouponViewModel _CouponViewModel; 公共可观察集合选项卡children{get;set;} 公共ITabViewModel SelectedTabViewModel{get;set;} 公共维护视图模型(耦合视图模型耦合视图模型) { _couponViewModel=couponViewModel; _couponViewModel.Header=“TestPlate”; TabChildren=新的可观察集合 { _耦合视图模型 }; } }
您的问题表明您可能在ViewModels的注入构造函数中做了太多工作。有关更多信息,请阅读。