我正在實作一個 WPF 應用程式,并在單擊按鈕時切換視圖模型。我必須通過 youtube 教程實作導航商店。當我單擊一個按鈕時,navigateCommand 將執行,創建一個新的 viewModel 并通知視圖進行更改。但是我不明白 OnCurrentViewModelChanged() 方法在做什么以及為什么需要它,動作 CurrentViewModelChanged 正在回傳 void,并且是空的?還是我錯過了什么?CurrentViewModelChanged 在做什么?有人可以解釋一下嗎?
public class NavigationStore
{
public event Action CurrentViewModelChanged;
private NotifyPropertyChanged currentViewModel;
public NotifyPropertyChanged CurrentViewModel
{
get => currentViewModel;
set
{
currentViewModel = value;
OnCurrentViewModelChanged();
}
}
private void OnCurrentViewModelChanged()
{
CurrentViewModelChanged?.Invoke();
}
}
public class NavigateCommand<TViewModel> : CommandBase where TViewModel : NotifyPropertyChanged
{
private readonly NavigationStore _navigationStore;
private readonly Func<TViewModel> _createViewModel;
public NavigateCommand(NavigationStore navigationStore, Func<TViewModel> createViewModel)
{
_navigationStore = navigationStore;
_createViewModel = createViewModel;
}
public override void Execute()
{
_navigationStore.CurrentViewModel = _createViewModel();
}
}
public class MainViewModel : NotifyPropertyChanged
{
private readonly NavigationStore _navigationStore;
public NotifyPropertyChanged CurrentViewModel => _navigationStore.CurrentViewModel;
public MainViewModel(NavigationStore navigationStore)
{
_navigationStore = navigationStore;
_navigationStore.CurrentViewModelChanged = OnCurrentViewModelChanged;
}
private void OnCurrentViewModelChanged()
{
OnPropertyChanged(nameof(CurrentViewModel));
}
}
uj5u.com熱心網友回復:
所以首先,我也跟著他的教程(很可能是 SingletonSean 的),我不同意 @BenicCode 對此的看法(雖然我不像他那樣是 WPF 的專業人士),我真的很喜歡他的解釋和解決方案到問題。此外,他在整個指南中不斷更改專案,實施更好的解決方案并解釋為什么使用這個比那個更好。
OnCurrentViewModelChanged() 方法引發一個事件,以便呼叫任何訂閱它的方法。但是,您實際上并不需要它,您可以像這樣實作 NavigationStore:
導航商店.cs
public class NavigationStore : INavigationStore
{
private ViewModelBase? _currentViewModel;
public ViewModelBase? CurrentViewModel
{
get => _currentViewModel;
set
{
_currentViewModel?.Dispose();
_currentViewModel = value;
NavigationStateChanged?.Invoke();
}
}
public event Action? NavigationStateChanged;
}
現在,在您的 MainViewModel 中,您可以簡單地將 NavigationStateChanged 操作訂閱到 OnCurrentViewModelChanged(),而不是在導航存盤中再添加一個方法。
主視圖模型.cs
public class MainViewModel : ViewModelBase
{
private readonly INavigationStore _navigationStore;
public ViewModelBase? CurrentViewModel => _navigationStore.CurrentViewModel;
public MainViewModel(INavigationStore navigationStore)
{
_navigationStore = navigationStore;
_navigationStore.NavigationStateChanged = OnNavigator_NavigationStateChanged;
}
private void OnNavigator_NavigationStateChanged()
{
OnPropertyChanged(nameof(CurrentViewModel));
}
}
它基本相同,但更簡單一些(如果我錯了,請糾正我)。通過將 NavigationStateChanged 訂閱到 OnNavigator_NavigationStateChanged,每當引發 NavigationStateChanged 時,OnNavigator_NavigationStateChanged 也會觸發,它會通知您的 UI 發生更改(因為您將 ContentControl 的 Content 屬性系結到 CurrentViewModel 屬性)。
主視窗.xaml
<Grid>
<ContentControl Content="{Binding CurrentViewModel}" />
</Grid>
在本教程的這一點上,他只想演示非常基本的導航。隨著您的進一步發展,事情變得更加清晰和復雜。我真的建議完成他的教程,可能會有更好的指南,但作為起點,我找不到更好的頻道。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/458851.html
