C#/WPF如何从ViewModel创建关闭应用程序按钮
因此,我对C#/WPF这个东西还不太熟悉,我想知道如何制作一个按钮,当点击它时就会关闭应用程序。 我尝试过谷歌搜索的许多建议,比如:C#/WPF如何从ViewModel创建关闭应用程序按钮,c#,wpf,mvvm,C#,Wpf,Mvvm,因此,我对C#/WPF这个东西还不太熟悉,我想知道如何制作一个按钮,当点击它时就会关闭应用程序。 我尝试过谷歌搜索的许多建议,比如: 所有这些似乎都没有帮到我。下面,我提供了我目前拥有的代码,这些代码是我根据ViewModel导航教程编写的。如果需要更多的代码,请告诉我 文件: main菜单视图模型: using Creator.Commands; using Creator.Stores; using System; using System.Collections.Gene
using Creator.Commands;
using Creator.Stores;
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Input;
namespace Creator.ViewModels
{
class MainMenuViewModel : ViewModelBase
{
public ICommand NavigateItemCreateMenuCommand { get; }
public MainMenuViewModel(NavigationStore navigationStore)
{
NavigateItemCreateMenuCommand = new NavigateCommand<ItemCreateMenuViewModel>(navigationStore, () => new ItemCreateMenuViewModel(navigationStore));
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Creator
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}
使用Creator.Commands;
使用Creator.Stores;
使用制度;
使用System.Collections.Generic;
使用系统文本;
使用System.Windows.Input;
namespace Creator.ViewModels
{
类MainMenuViewModel:ViewModelBase
{
公共ICommand NavigateItemCreateMenuCommand{get;}
公共主菜单视图模型(导航存储导航存储)
{
NavigateItemCreateMenuCommand=new NavigateCommand(navigationStore,()=>new ItemCreateMenuViewModel(navigationStore));
}
}
}
MainMenuView:Quit按钮必须关闭应用程序
<UserControl x:Class="Creator.Views.MainMenuView"
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:local="clr-namespace:Creator.Views"
mc:Ignorable="d"
d:DesignHeight="720" d:DesignWidth="1280">
<Grid Background="White">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition/>
<RowDefinition Height="50"/>
</Grid.RowDefinitions>
<DockPanel Background="LightBlue" Grid.Row="1" Grid.Column="1" >
<StackPanel>
<Label Content="Main Menu" HorizontalAlignment="Center" VerticalAlignment="Stretch" FontWeight="Bold" FontFamily="Century Gothic" FontSize="24"/>
<Button Content="Create Item" FontWeight="Bold" FontFamily="Century Gothic" FontSize="18" Margin="20,0,20,5" Padding="0,5,0,5" Command="{Binding NavigateItemCreateMenuCommand}"/>
<Button Content="Quit" FontWeight="Bold" FontFamily="Century Gothic" FontSize="18" Margin="20,0,20,5" Padding="0,5,0,5" VerticalAlignment="Bottom"/>
</StackPanel>
</DockPanel>
</Grid>
</UserControl>
MainWindow.xaml.cs:
using Creator.Commands;
using Creator.Stores;
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Input;
namespace Creator.ViewModels
{
class MainMenuViewModel : ViewModelBase
{
public ICommand NavigateItemCreateMenuCommand { get; }
public MainMenuViewModel(NavigationStore navigationStore)
{
NavigateItemCreateMenuCommand = new NavigateCommand<ItemCreateMenuViewModel>(navigationStore, () => new ItemCreateMenuViewModel(navigationStore));
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Creator
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
名称空间创建者
{
///
///MainWindow.xaml的交互逻辑
///
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
}
}
}
TL;医生:
我需要在MainMenuViewModel/MainMenuView上设置退出按钮以关闭应用程序。使用
QuitCommand
扩展您的ViewModel,并将其绑定到按钮
class MainMenuViewModel : ViewModelBase
{
public ICommand NavigateItemCreateMenuCommand { get; }
public ICommand QuitCommand { get; }
public MainMenuViewModel(NavigationStore navigationStore)
{
NavigateItemCreateMenuCommand = new NavigateCommand<ItemCreateMenuViewModel>(navigationStore, () => new ItemCreateMenuViewModel(navigationStore));
QuitCommand = new RelayCommand((par) => { Application.Current.Shutdown(); });
}
}
<Button Content="Quit" FontWeight="Bold" FontFamily="Century Gothic" FontSize="18" Margin="20,0,20,5" Padding="0,5,0,5" VerticalAlignment="Bottom" Command="{Binding QuitCommand}"/>
class MainMenuViewModel:ViewModelBase
{
公共ICommand NavigateItemCreateMenuCommand{get;}
公共ICommand命令{get;}
公共主菜单视图模型(导航存储导航存储)
{
NavigateItemCreateMenuCommand=new NavigateCommand(navigationStore,()=>new ItemCreateMenuViewModel(navigationStore));
QuitCommand=newrelaycommand((par)=>{Application.Current.Shutdown();});
}
}
还可以在中找到RelayCommand
的实现,使用QuitCommand
扩展视图模型,并将其绑定到按钮
class MainMenuViewModel : ViewModelBase
{
public ICommand NavigateItemCreateMenuCommand { get; }
public ICommand QuitCommand { get; }
public MainMenuViewModel(NavigationStore navigationStore)
{
NavigateItemCreateMenuCommand = new NavigateCommand<ItemCreateMenuViewModel>(navigationStore, () => new ItemCreateMenuViewModel(navigationStore));
QuitCommand = new RelayCommand((par) => { Application.Current.Shutdown(); });
}
}
<Button Content="Quit" FontWeight="Bold" FontFamily="Century Gothic" FontSize="18" Margin="20,0,20,5" Padding="0,5,0,5" VerticalAlignment="Bottom" Command="{Binding QuitCommand}"/>
class MainMenuViewModel:ViewModelBase
{
公共ICommand NavigateItemCreateMenuCommand{get;}
公共ICommand命令{get;}
公共主菜单视图模型(导航存储导航存储)
{
NavigateItemCreateMenuCommand=new NavigateCommand(navigationStore,()=>new ItemCreateMenuViewModel(navigationStore));
QuitCommand=newrelaycommand((par)=>{Application.Current.Shutdown();});
}
}
在中还可以找到RelayCommand的实现。您应该使用命令关闭ViewModel中正在运行的窗口。
通过将父控件窗口
交给命令参数,可以导入当前运行的main窗口
我使用MVVM为您制作了一个示例。
我希望它能帮助你。
您应该使用命令关闭ViewModel中正在运行的窗口。
通过将父控件窗口
交给命令参数,可以导入当前运行的main窗口
我使用MVVM为您制作了一个示例。
我希望它能帮助你。
首先,我将创建一个接口
public interface IClosable
{
Action Close { get;set; }
}
MainWindowViewModel : ICloseable
{
public Action Close { get;set; }
//your code
}
然后,您就知道viewmodel应该实现这个接口
public interface IClosable
{
Action Close { get;set; }
}
MainWindowViewModel : ICloseable
{
public Action Close { get;set; }
//your code
}
然后在窗户上
public partial class MainMenuView : Window
{
public MainMenuView()
{
InitializeComponent();
DataContext = new MainMenuViewModel();
if(DataContext is IClosable closable)
{
closable.Close += this.Close();
}
}
}
在关闭窗口之前,也要解除代理的挂钩。
现在,在viewmodel中调用Close操作将关闭窗口。首先,我将创建一个界面
public interface IClosable
{
Action Close { get;set; }
}
MainWindowViewModel : ICloseable
{
public Action Close { get;set; }
//your code
}
然后,您就知道viewmodel应该实现这个接口
public interface IClosable
{
Action Close { get;set; }
}
MainWindowViewModel : ICloseable
{
public Action Close { get;set; }
//your code
}
然后在窗户上
public partial class MainMenuView : Window
{
public MainMenuView()
{
InitializeComponent();
DataContext = new MainMenuViewModel();
if(DataContext is IClosable closable)
{
closable.Close += this.Close();
}
}
}
在关闭窗口之前,也要解除代理的挂钩。
现在,在viewmodel中调用Close操作将关闭窗口。不太清楚您想要什么。按钮关闭应用程序或只是一个窗口。您可以编写MainMenuView
,但它不是窗口
,而是用户控件
。如果是应用程序,则只需使用命令扩展VM,在其中调用application.Current.Shutdown()
@Rekshino是的,对不起,我指的是应用程序,认为窗口和应用程序关闭是相同的。将更新。使用此处提供的解决方案是可行的:这是否回答了您的问题?不太清楚你想要什么。按钮关闭应用程序或只是一个窗口。您可以编写MainMenuView
,但它不是窗口
,而是用户控件
。如果是应用程序,则只需使用命令扩展VM,在其中调用application.Current.Shutdown()
@Rekshino是的,对不起,我指的是应用程序,认为窗口和应用程序关闭是相同的。将更新。使用此处提供的解决方案是可行的:这是否回答了您的问题?这是关闭窗口,但不是关闭应用程序,如果同时打开了其他shell,就会出现问题。Window.Close()
我认为这是一个好方法。默认情况下,当您对要关闭的窗口对象调用Close()时,Closing和Closed等事件也会按顺序工作。这是关闭窗口,而不是关闭应用程序,如果同时打开了其他shell,则