2015-02-09 40 views
0

我似乎無法讓我的兩個按鈕在Window2上成功更改我的主窗口上的webbrowser url。我想要做的就是點擊按鈕1並轉到一個地址,然後單擊另一個按鈕並轉到其他地址。 我已經嘗試綁定沒有任何運氣,我已經嘗試將MainWindow webbrowser設置爲新的Uri,但是這也不起作用。我收到一個錯誤:'NullReferenceException未處理'「未將對象引用設置爲對象的實例。」WPF XAML兩個按鈕,在不同的窗口上更改webbrowser url

不知道我在做什麼錯,任何幫助將不勝感激。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
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.Shapes; 
using System.Timers; 
using System.Threading; 



namespace _2CAMS 
{ 
/// <summary> 
/// Interaction logic for Window2.xaml 
/// </summary> 
/// 

public partial class Window2 : Window 
{ 
    MainWindow mw; 

    public Window2() 
    { 
     InitializeComponent(); 

    } 

    private void Window_Loaded(object sender, RoutedEventArgs e) 
    { 
     this.ShowActivated = true; 
     this.MouseLeftButtonDown += delegate { this.DragMove(); }; 
    } 

    private void Button1_Checked(object sender, RoutedEventArgs e) 
    { 

    } 

    private void button2_Click(object sender, RoutedEventArgs e) 
    { 
     this.Close(); 
     Application.Current.Shutdown(); 
    } 

    private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
    { 
     //this.DragMove(); 
    } 

    private void Image1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
    { 
     System.Diagnostics.Process.Start("http://www.website.com"); 

    } 

    private void button3_Click(object sender, RoutedEventArgs e) 
    { 
     Application.Current.MainWindow.WindowState = System.Windows.WindowState.Minimized; 

     //App.Current.MainWindow.Hide(); 
     Window3 win3 = new Window3(); 
     win3.Show(); 
     this.WindowState = System.Windows.WindowState.Minimized; 
    } 

    private void button4_Click(object sender, RoutedEventArgs e) 
    { 
     mw.webBrowser1.Navigate(new Uri("http://www.address1.com")); 

    } 

    private void button5_Click(object sender, RoutedEventArgs e) 
    { 

     mw.webBrowser1.Navigate(new Uri("http://www.address2.com")); 
    } 

} 
} 
+0

*那*代碼給你一個NRE?哪個變量是空的? – BradleyDotNET 2015-02-09 21:09:40

+0

不確定,在查看「查看詳情...」時我沒有看到任何東西,或者我只是不知道我在找什麼。 – cheapkid1 2015-02-09 21:15:51

+0

[快照](http://s42.photobucket.com/user/cheapkid1/media/c_zpsamhxjbwy.png.html) – cheapkid1 2015-02-09 21:26:38

回答

1
((MainWindow)App.Current.MainWindow).webBrowser1.Navigate(new Uri("http://address1.com")); 

它的工作原理,也引發InvalidCastException?

+0

這工作,但是當順序我點擊按鈕4它加載適當的頁面,然後當我點擊按鈕5它加載適當的頁面,但有錯誤。然而,奇怪的是,當我顛倒序列並先從按鈕5開始,然後再按下按鈕4,然後按需要交替多次,它不會產生錯誤。我很困惑。 – cheapkid1 2015-02-11 14:05:17

+0

這是我得到的[error](http://s42.photobucket.com/user/cheapkid1/media/c_zpsj0gpf2cl.jpg.html)。這是我的MainWindow.cs代碼。 – cheapkid1 2015-02-11 14:11:55

+0

經過一番研究之後,我仍然不確定爲什麼會出現這種錯誤,但是我只能在MainWindow上放置兩個單獨的web瀏覽器,並使按鈕4和5交替出現,即瀏覽器可見/隱藏。這似乎無誤地工作。感謝所有幫助的人。 – cheapkid1 2015-02-11 20:13:15