2015-02-09 30 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