通用相同的按钮,不同的视图,不同的模式。WPF 导航存储

Generic same button, different view, different modal. WPF NAVIGATION STORING

提问人:NeuSandeZ 提问时间:11/12/2023 更新时间:11/12/2023 访问量:40

问:

我在 WPF 中有一个通用主题,用于基本的 crud 操作,我的大多数视图都继承自该主题。但是,当我按下“添加”按钮时,模式会显示,但在使用 ModalStorageView 的所有视图中。

public class NavigationModalViewStore : NavigationStoreBase
{
    public bool IsOpenModal => CurrentViewModel != null;

    public void Close()
    {
        CurrentViewModel = null;
    }
    
    private ViewModelBase _currentViewModel;

    public ViewModelBase CurrentViewModel
    {
        get => _currentViewModel;
        set
        {
            _currentViewModel = value;
            OnCurrentViewModelChanged();
        }
    }

    public event Action? CurrentViewModelChanged;

    private void OnCurrentViewModelChanged()
    {
        CurrentViewModelChanged?.Invoke();
    }
}

这是命令:

using System;
using Hotel.MVVM.ViewModels;
using Hotel.Stores;
using Microsoft.EntityFrameworkCore.Metadata.Internal;

namespace Hotel.Commands;

public class NavigateModalCommand : BaseCommand

{
    private readonly NavigationModalViewStore _navigationModalViewStore;
    private readonly Func<ViewModelBase> _createViewModel;

    public NavigateModalCommand(NavigationModalViewStore navigationModalViewStore, Func<ViewModelBase> createViewModel)
    {
        _navigationModalViewStore = navigationModalViewStore;
        _createViewModel = createViewModel;
    }

    public override void Execute(object? parameter)
    {
        _navigationModalViewStore.CurrentViewModel = _createViewModel();
    }
}

这是 ViewModel

public class ReservationsListingViewModel : ViewModelBase
{
    private readonly NavigationModalViewStore _navigationModalViewStore;
    public ViewModelBase CurrentModalViewModel => _navigationModalViewStore.CurrentViewModel;
    public bool IsModalOpen => _navigationModalViewStore.IsOpenModal;
    public ICommand AddViewModalCommand { get; }
    public ReservationsListingViewModel(NavigationModalViewStore navigationModalViewStore)
    {
        _navigationModalViewStore = navigationModalViewStore;
        AddViewModalCommand = new NavigateModalCommand(_navigationModalViewStore,() => new CrudAddModalViewModel());
        _navigationModalViewStore.CurrentViewModelChanged += OnCurrentViewModalChanged;
    }

    private void OnCurrentViewModalChanged()
    {
        OnPropertyChanged(nameof(CurrentModalViewModel));
        OnPropertyChanged(nameof(IsModalOpen));
    }
}
public class TestViewModel : ViewModelBase
{
    private readonly NavigationModalViewStore _navigationModalViewStore;
    public ViewModelBase CurrentModalViewModel => _navigationModalViewStore.CurrentViewModel;
    public bool IsModalOpen => _navigationModalViewStore.IsOpenModal;
    public ICommand AddViewModalCommand { get; }
    public TestViewModel(NavigationModalViewStore navigationModalViewStore)
    {
        _navigationModalViewStore = navigationModalViewStore;
        AddViewModalCommand = new NavigateModalCommand(_navigationModalViewStore,() => new TextXDViewModel());
        _navigationModalViewStore.CurrentViewModelChanged += OnCurrentViewModalChanged;
    }

    private void OnCurrentViewModalChanged()
    {
        OnPropertyChanged(nameof(CurrentModalViewModel));
        OnPropertyChanged(nameof(IsModalOpen));
    }
}

我在 WPF 应用程序中使用 DI 主机,并且 NavigationModalStore 已注册为单例。我尝试过使用 Transiet 注册它,但它仅在我进行第一次迭代并在所有视图中显示该模式时才有效。我认为这与与所有主要视图共享模态的存储有关。

有人可以帮我找出方便存储模态的方法,而无需为显示模态的每个主要视图创建存储?

我尝试将 ModalStorageView 注册为 Transient,因此每当我访问它时,我都会获得新实例。我想它可能与其他服务冲突。

以下是注册情况:

public partial class App : System.Windows.Application
{
    private readonly IHost _host;

    public App()
    {
        var connectionString = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json")
            .Build()
            .GetConnectionString("HotelDbContext");
        
        _host = Host.CreateDefaultBuilder().ConfigureServices(services =>
            {
                services.AddInfrastructure(connectionString);
                
                // services.AddSingleton<INavigationService, NavigationService>();
                services.AddSingleton<NavigationViewStore>();

                services.AddSingleton<NavigationModalViewStore>(); // 
                
                services.AddSingleton<MainWindowViewModel>();
                services.AddSingleton(s => new MainWindow
                {
                    DataContext = s.GetRequiredService<MainWindowViewModel>()
                });
            })
            .Build();
    }

    protected override void OnStartup(StartupEventArgs e)
    {
        _host.Start();

        var navigationModalStore = _host.Services.GetRequiredService<NavigationModalViewStore>();
        var navigationStore = _host.Services.GetRequiredService<NavigationViewStore>();
        
        navigationStore.CurrentViewModel = new ReservationsListingViewModel(navigationModalStore);

        MainWindow = _host.Services.GetRequiredService<MainWindow>();
        MainWindow.Show();

        base.OnStartup(e);
    }
}
C# WPF 泛型导航 模式窗口

评论


答: 暂无答案