首页 > 解决方案 > 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();
        }
    }
 }

标签: wpfmvvm

解决方案


您可以在 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();
        }
    }
}

推荐阅读