如何使用按钮单击从另一个 xaml 窗口打开一个 xaml 窗口?
private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
{
Win1 OP= new Win1();
OP.show();
}
OP.show() 抛出错误.
OP.show() is throwing an error.
这是一个用户控件表单.
It is a usercontrol form.
你说 Win1
是它是一个 usercontrol 表单."(重点是我的).
You say that Win1
is "It is a usercontrol form." (emphasis is mine).
如果 Win1
实际上是 UserControl
类型,问题是 UserControl
类型没有定义 Show()代码>方法.所以它不能作为一个窗口打开".
If Win1
is actually of type UserControl
, the issues is that the type UserControl
doesn't define Show()
method. So it cannot be "opened" as a window.
要解决这个问题,您需要打开一个窗口并将 UC 作为该窗口的内容:
To solve this you need to open a window and have the UC as the content for that window:
private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
{
Win1 OP= new Win1();
var host = new Window();
host.Content = OP;
host.Show();
}
附带说明,您可以在 App.xaml 中使用 UserControl
作为 StartupUri
并且它会起作用,因为框架识别出它不是窗口并为
As a side note, you can use UserControl
as StartupUri
in App.xaml and it will work since the framework recognizes that it's not a window and creates a window for it.