2016-06-28 99 views
0

我目前正在學習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(); 
     } 
    } 
} 
+0

在['Page'](https://msdn.microsoft.com/en-us/library/system.windows.controls.page(v = vs.110).aspx)中沒有方法'Close' – Default

+0

@默認如果沒有方法,我該怎麼辦? – syn3rgy

+0

@ syn3rgy這是你自己,我猜。 [但你仍然不能調用方法或訪問不存在的類成員。](https://msdn.microsoft.com/en-us/library/bb383961.aspx) – Default

回答

0

這是一個有點猜的,因爲我還沒有看夠你的代碼。如果電話this.Close()被放置在以外定義您要關閉的窗口的類,那麼在範圍內沒有這種方法

+0

我剛編輯我的文章以包含整個代碼..我是一個初學者,所以你可以徹底解釋它。 – syn3rgy

+0

@ syn3rgy假設你不熟悉面向對象編程是否正確?怎麼樣的C#類? – Default

+0

@默認是的,我想嘗試一個C#,並不知道從哪裏開始。 – syn3rgy