我目前正在学习C#WPF。 现在我只是想了解导航如何工作。关闭WPF主窗口页面
我创建了一个包含2个按钮的测试应用程序。 一个导航到下一个页面,另一个打开一个新窗口。
页面之间的导航不是问题。 我能够使用按钮从page1导航到page2。下面的代码写在我的Page1.xaml.cs
private void button_Click(object sender, RoutedEventArgs e)
{
Page2 p2 = new Page2();
this.NavigationService.Navigate(p2);
}
问题是,当我试图打开一个新窗口,并关闭一个按钮前一,它不工作。 (我也写过这个我Page1.xaml.cs)
private void button_Copy_Click(object sender, RoutedEventArgs e)
{
Window1 win1 = new Window1();
win1.Show();
this.Close();
}
它给我的错误码CS1061,告诉我,它不包含定义“关闭” .....
这里是我的Page1.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 WpfApplication1
{
/// <summary>
/// Interaction logic for Page1.xaml
/// </summary>
public partial class Page1 : Page
{
public Page1()
{
InitializeComponent();
}
private void button_Click(object sender, RoutedEventArgs e)
{
Page2 p2 = new Page2();
this.NavigationService.Navigate(p2);
}
private void button_Copy_Click(object sender, RoutedEventArgs e)
{
Window1 win1 = new Window1();
win1.Show();
this.Close();
}
}
}
在['Page'](https://msdn.microsoft.com/en-us/library/system.windows.controls.page(v = vs.110).aspx)中没有方法'Close' – Default
@默认如果没有方法,我该怎么办? – syn3rgy
@ syn3rgy这是你自己,我猜。 [但你仍然不能调用方法或访问不存在的类成员。](https://msdn.microsoft.com/en-us/library/bb383961.aspx) – Default