WPF MVVM导航取消处理?



在我的WPF项目中,MVVM模式。我可以根据情况在主窗口显示不同的视图。在切换视图时,我找不到如何发出用户确认请求,如果用户不批准,视图不会改变。你能告诉我你对此的建议吗,如果有的话,样本文章或项目?非常感谢。输入图片描述

<Window ....>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="200"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<StackPanel VerticalAlignment="Bottom" HorizontalAlignment="Center" Grid.Row="1" 
Grid.Column="0" Grid.ColumnSpan="2" Orientation="Horizontal">
<Button Width="120" Margin="5" Command="{Binding UpdateViewCommand}" 
CommandParameter="Home"/>
<Button Width="120" Margin="5" Command="{Binding UpdateViewCommand}" 
CommandParameter="Portfolio"/>
</StackPanel>
<Frame Content="{Binding SelectedViewModel}"/>
</Grid>
</Window>




<Application ....">
<Application.Resources>
<ResourceDictionary>                    
<DataTemplate DataType="{x:Type viewModels:HomeViewModel}">
<views:Home/>
</DataTemplate>
<DataTemplate DataType="{x:Type viewModels:PortfolioViewModel}">
<views:Portfolio/>
</DataTemplate>
</ResourceDictionary>
</Application.Resources>
</Application>
<Page x:Class="pkyRaporCore.Views.Home"
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:viewModels="clr-namespace:pkyRaporCore.ViewModels"
mc:Ignorable="d" 
d:DesignHeight="450" d:DesignWidth="800"
Title="Home">
<Page.DataContext>
<viewModels:HomeViewModel/>
</Page.DataContext>
<Grid>
<ComboBox ItemsSource="{Binding Koleksiyon}" DisplayMemberPath="{Binding}" Width="200"/>
</Grid>
</Page>

<Page ....>
<Grid>
<StackPanel>
<TextBlock Text="Portfolio" Margin="5 5"/>
<ListBox Margin="5 5">
<ListBoxItem Content="Portfolio 1/6"/>
<ListBoxItem Content="Portfolio 2/6"/>
<ListBoxItem Content="Portfolio 3/6"/>
<ListBoxItem Content="Portfolio 4/6"/>
<ListBoxItem Content="Portfolio 5/6"/>
<ListBoxItem Content="Portfolio 6/6"/>
</ListBox>
</StackPanel>
</Grid>
</Page>

public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
Window window = new MainWindow();
window.DataContext = new MainViewModel();
window.Show();
base.OnStartup(e);
}
}
public class MainViewModel : ViewModelBase, I
{
private readonly IRegionManager regionManager;
public DelegateCommand<string> NavigateCommand { get; set; }
private ViewModelBase selectedViewModel = new HomeViewModel();
public ViewModelBase SelectedViewModel
{
get { return selectedViewModel; }
set { selectedViewModel = value; OnPropertyChanged(); }
}
public ICommand UpdateViewCommand { get; set; }
public MainViewModel()
{
UpdateViewCommand = new UpdateViewCommand(this);
}
}
public class ViewModelBase: ObservableBase
{
}
public class ObservableBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
public class UpdateViewCommand : ICommand
{
private MainViewModel viewModel;
public UpdateViewCommand(MainViewModel viewModel)
{
this.viewModel = viewModel;
}
public event EventHandler CanExecuteChanged;
public bool CanExecute(object parameter)
{
return true;
}
public void Execute(object parameter)
{
if (parameter.ToString() == "Home")
{
viewModel.SelectedViewModel = new HomeViewModel();
}
else if (parameter.ToString() == "Portfolio")
{
viewModel.SelectedViewModel = new PortfolioViewModel();
}
}
}

如果用户不接受更改视图,你可以在MessageBox中询问用户,什么也不做

public class UpdateViewCommand : ICommand
{
private MainViewModel viewModel;
public UpdateViewCommand(MainViewModel viewModel)
{
this.viewModel = viewModel;
}
public event EventHandler CanExecuteChanged;
public bool CanExecute(object parameter)
{
return true;
}
public void Execute(object parameter)
{
MessageBoxResult res = MessageBox.Show("Do you confirm changing view?", "", MessageBoxButton.YesNo);
if (res != MessageBoxResult.Yes)
return;
if (parameter.ToString() == "Home")
{
viewModel.SelectedViewModel = new HomeViewModel();
}
else if (parameter.ToString() == "Portfolio")
{
viewModel.SelectedViewModel = new PortfolioViewModel();
}
}
}

最新更新