在 wpf 中关闭页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42142553/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
closing a page in wpf
提问by Samer
I am new to c#, I just created a new page and want to close it in the xaml side I have:
我是 c# 新手,我刚刚创建了一个新页面,并想在我拥有的 xaml 端关闭它:
<Grid>
<Button Content="Back" Click="Button_Click_Exit" HorizontalAlignment="Left" Margin="220,263,0,0" VerticalAlignment="Top" Width="75"/>
</Grid>
and on the .xaml.cs side:
在 .xaml.cs 方面:
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 UI
{
/// <summary>
/// Interaction logic for calibrationPage.xaml
/// </summary>
public partial class calibrationPage : Page
{
public calibrationPage()
{
InitializeComponent();
}
private void Button_Click_Exit(object sender, RoutedEventArgs e)
{
Close();
}
}
}
I think this supposed to be really simple, somehow I get this error, when I try to build it:
我认为这应该非常简单,当我尝试构建它时,不知何故我收到了这个错误:
error CS1061: 'UI.calibrationPage' does not contain a definition for 'Close' and no extension method 'Close' accepting a first argument of type 'UI.calibrationPage' could be found
EDIT: I understand that close() does not exit, then rephrasing my question, how can I simply close the page using a button click?
编辑:我知道 close() 不会退出,然后重新表述我的问题,如何简单地使用按钮单击关闭页面?
EDIT2: For the benefit of others: I ended up using
PageNavigator.NavigateTofunction to navigate back and forth between pages, I do not think that there is a concept of closing one page in wpf. Thanks for everyone's participation.
EDIT2:为了他人的利益:我最终使用
PageNavigator.NavigateTo函数在页面之间来回导航,我认为没有在wpf. 感谢大家的参与。
回答by Mokey
You're not telling an object to close.
你不是告诉一个对象关闭。
If you're just wanting a button to act on the event, I'd recommend "click" event or "OnClick". If you are in VS you can just double click the button in the designer, it will auto populate everything for you.
如果您只是想要一个按钮来处理事件,我建议您使用“click”事件或“OnClick”。如果您在 VS 中,您只需双击设计器中的按钮,它就会自动为您填充所有内容。
If you want a form to close you can use similar methods from your WPF window.
如果您想要关闭表单,您可以在 WPF 窗口中使用类似的方法。
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
On the other hand, if you wanted to create your own method (Like in this case)you can do so like:
另一方面,如果您想创建自己的方法(如本例),您可以这样做:
private void closeMethod()
{
// your code here
}
then you call it with:
然后你调用它:
closeMethod();
depending on your method you can tell it to do what you want on the page. Such as close the page or window.
根据您的方法,您可以告诉它在页面上执行您想要的操作。如关闭页面或窗口。
You can also pass references in your methods
您还可以在方法中传递引用
refer to https://msdn.microsoft.com/en-us/library/ms173114.aspxfor methods.
有关方法,请参阅https://msdn.microsoft.com/en-us/library/ms173114.aspx。
Not sure if it fits here, but you may want to use Application.Exit();in some cases, if so refer to https://social.msdn.microsoft.com/Forums/windows/en-US/f23247b8-e0ca-46eb-afb0-dbf22d4c84ac/thisclose-vs-applicationexit?forum=winformsto find the difference.
不确定它是否适合这里,但您可能想Application.Exit();在某些情况下使用,如果是这样,请参阅https://social.msdn.microsoft.com/Forums/windows/en-US/f23247b8-e0ca-46eb-afb0-dbf22d4c84ac /thisclose-vs-applicationexit?forum=winforms找出不同之处。

