我目前正在學習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