WPF XAML 在不同 window 上更改 webbrowser url 的两个按钮
WPF XAML two buttons that change webbrowser url on a different window
我似乎无法在 Window2 上使用我的两个按钮来成功更改主 window 上的网络浏览器 url。我想做的就是单击第一个按钮转到一个地址,然后单击另一个按钮转到另一个地址。
我试过绑定但没有成功,我试过将 MainWindow webbrowser 设置为新的 Uri,但这也不起作用。我得到一个错误:'NullReferenceException was unhandled' "Object reference not set to an instance of an object."
不确定我做错了什么,如有任何帮助,我们将不胜感激。
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"));
}
}
}
((MainWindow)App.Current.MainWindow).webBrowser1.Navigate(new Uri("http://address1.com"));
是否有效,或者会抛出 InvalidCastException?
我似乎无法在 Window2 上使用我的两个按钮来成功更改主 window 上的网络浏览器 url。我想做的就是单击第一个按钮转到一个地址,然后单击另一个按钮转到另一个地址。 我试过绑定但没有成功,我试过将 MainWindow webbrowser 设置为新的 Uri,但这也不起作用。我得到一个错误:'NullReferenceException was unhandled' "Object reference not set to an instance of an object."
不确定我做错了什么,如有任何帮助,我们将不胜感激。
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"));
}
}
}
((MainWindow)App.Current.MainWindow).webBrowser1.Navigate(new Uri("http://address1.com"));
是否有效,或者会抛出 InvalidCastException?