- tweak: Dependency injection think
This commit is contained in:
63
Nebula.Launcher/ViewModels/MainViewModel.cs
Normal file
63
Nebula.Launcher/ViewModels/MainViewModel.cs
Normal file
@@ -0,0 +1,63 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using Avalonia.Controls;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.DependencyInjection;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using CommunityToolkit.Mvvm.Messaging;
|
||||
using Nebula.Launcher.Models;
|
||||
|
||||
namespace Nebula.Launcher.ViewModels;
|
||||
|
||||
|
||||
public partial class MainViewModel : ViewModelBase
|
||||
{
|
||||
public MainViewModel(AccountInfoViewModel accountInfoViewModel, IServiceProvider serviceProvider)
|
||||
{
|
||||
_currentPage = accountInfoViewModel;
|
||||
_serviceProvider = serviceProvider;
|
||||
Items = new ObservableCollection<ListItemTemplate>(_templates);
|
||||
|
||||
SelectedListItem = Items.First(vm => vm.ModelType == typeof(AccountInfoViewModel));
|
||||
}
|
||||
|
||||
private readonly List<ListItemTemplate> _templates =
|
||||
[
|
||||
new ListItemTemplate(typeof(AccountInfoViewModel), "HomeRegular", "Account"),
|
||||
];
|
||||
|
||||
[ObservableProperty]
|
||||
private bool _isPaneOpen;
|
||||
|
||||
[ObservableProperty]
|
||||
private ViewModelBase _currentPage;
|
||||
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
|
||||
[ObservableProperty]
|
||||
private ListItemTemplate? _selectedListItem;
|
||||
|
||||
partial void OnSelectedListItemChanged(ListItemTemplate? value)
|
||||
{
|
||||
if (value is null) return;
|
||||
|
||||
var vm = Design.IsDesignMode
|
||||
? Activator.CreateInstance(value.ModelType)
|
||||
: _serviceProvider.GetService(value.ModelType);
|
||||
|
||||
if (vm is not ViewModelBase vmb) return;
|
||||
|
||||
CurrentPage = vmb;
|
||||
}
|
||||
|
||||
public ObservableCollection<ListItemTemplate> Items { get; }
|
||||
|
||||
[RelayCommand]
|
||||
private void TriggerPane()
|
||||
{
|
||||
IsPaneOpen = !IsPaneOpen;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user