private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
{
Win1 OP= new Win1();
OP.show();
}
OP.show() is throwing an error.
show()正在抛出一个错误。
It is a usercontrol form.
它是一个usercontrol表单。
6 个解决方案
#1
5
You say that Win1
is "It is a usercontrol form." (emphasis is mine).
您说Win1是“它是一个usercontrol表单”。(重点是我)。
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.
如果Win1实际上是UserControl类型,那么问题是UserControl类型没有定义Show()方法。所以它不能作为窗口“打开”。
To solve this you need to open a window and have the UC as the content for that window:
要解决这个问题,您需要打开一个窗口,并将UC作为该窗口的内容:
private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
{
Win1 OP= new Win1();
var host = new Window();
host.Content = OP;
host.Show();
}
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.
作为补充说明,您可以在App.xaml中使用UserControl作为StartupUri,它将工作,因为框架认识到它不是一个窗口并为它创建一个窗口。
#2
2
Opens a window and close the first one :
打开一个窗口,关闭第一个窗口:
private void Button_Click(object sender, RoutedEventArgs e)
{
window2 win2= new window2();
win2.Show();
this.Close();
}
#3
1
You can't open UserControl as Window or dialog box. Better add usercontrol into some Window through code behind or XAML and then open that window. Don't forget to set DataContext of that window. Remember datacontext of parent control/window will get inherited by child controls/usercontrols.
不能将UserControl打开为窗口或对话框。最好通过代码或XAML将usercontrol添加到某个窗口中,然后打开该窗口。不要忘记设置那个窗口的DataContext。记住,父控件/窗口的datacontext将由子控件/usercontrols继承。
#4
1
All of these answers are fine, however they do not take advantage of WPF's Navigation features. If you're going to be hiding/closing the current window anyway, then this is a great alternative to the old WinForms
window management. Instead of creating multiple windows, create pages and simply display them using a single Frame control in the MainWindow:
所有这些答案都很好,但是它们没有利用WPF的导航特性。如果您将隐藏/关闭当前窗口,那么这是对旧的WinForms窗口管理的一个很好的选择。不要创建多个窗口,创建页面,只需在主窗口中使用一个框架控件来显示:
<Grid>
<Frame Source="/Menu.xaml" NavigationUIVisibility="Hidden"/>
</Grid>
Then when you want to display another "window" (actually a page), you can use NavigationService
to change the frame source. From the Menu page code behind:
然后,当您想要显示另一个“窗口”(实际上是一个页面)时,您可以使用NavigationService来更改框架源。从后面的菜单页代码:
private void Button_Click(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/Contact.xaml", UriKind.Relative));
}
This will now display the Contact page. Using this method ensures that the window properties are standardized. One window to rule them all!
这将显示联系人页面。使用此方法可确保窗口属性是标准化的。一个统治他们的窗口!
#5
0
You can't show usercontrol directly, you should render your usercontrol in another window.
不能直接显示usercontrol,应该在另一个窗口中显示usercontrol。
like.
喜欢的。
private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
{
UserControl1 uc1 = new UserControl1();
var win = new MainWindow();
win.Content = win1;
win.Show();
}
#6
0
If you'd like to do it like a popup:
如果你想像弹窗那样做:
private void btn1_click(object sender, RoutedEventArgs e)
{
newWin win2 = new newWin();
win2.Show():
}
#1
5
You say that Win1
is "It is a usercontrol form." (emphasis is mine).
您说Win1是“它是一个usercontrol表单”。(重点是我)。
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.
如果Win1实际上是UserControl类型,那么问题是UserControl类型没有定义Show()方法。所以它不能作为窗口“打开”。
To solve this you need to open a window and have the UC as the content for that window:
要解决这个问题,您需要打开一个窗口,并将UC作为该窗口的内容:
private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
{
Win1 OP= new Win1();
var host = new Window();
host.Content = OP;
host.Show();
}
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.
作为补充说明,您可以在App.xaml中使用UserControl作为StartupUri,它将工作,因为框架认识到它不是一个窗口并为它创建一个窗口。
#2
2
Opens a window and close the first one :
打开一个窗口,关闭第一个窗口:
private void Button_Click(object sender, RoutedEventArgs e)
{
window2 win2= new window2();
win2.Show();
this.Close();
}
#3
1
You can't open UserControl as Window or dialog box. Better add usercontrol into some Window through code behind or XAML and then open that window. Don't forget to set DataContext of that window. Remember datacontext of parent control/window will get inherited by child controls/usercontrols.
不能将UserControl打开为窗口或对话框。最好通过代码或XAML将usercontrol添加到某个窗口中,然后打开该窗口。不要忘记设置那个窗口的DataContext。记住,父控件/窗口的datacontext将由子控件/usercontrols继承。
#4
1
All of these answers are fine, however they do not take advantage of WPF's Navigation features. If you're going to be hiding/closing the current window anyway, then this is a great alternative to the old WinForms
window management. Instead of creating multiple windows, create pages and simply display them using a single Frame control in the MainWindow:
所有这些答案都很好,但是它们没有利用WPF的导航特性。如果您将隐藏/关闭当前窗口,那么这是对旧的WinForms窗口管理的一个很好的选择。不要创建多个窗口,创建页面,只需在主窗口中使用一个框架控件来显示:
<Grid>
<Frame Source="/Menu.xaml" NavigationUIVisibility="Hidden"/>
</Grid>
Then when you want to display another "window" (actually a page), you can use NavigationService
to change the frame source. From the Menu page code behind:
然后,当您想要显示另一个“窗口”(实际上是一个页面)时,您可以使用NavigationService来更改框架源。从后面的菜单页代码:
private void Button_Click(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/Contact.xaml", UriKind.Relative));
}
This will now display the Contact page. Using this method ensures that the window properties are standardized. One window to rule them all!
这将显示联系人页面。使用此方法可确保窗口属性是标准化的。一个统治他们的窗口!
#5
0
You can't show usercontrol directly, you should render your usercontrol in another window.
不能直接显示usercontrol,应该在另一个窗口中显示usercontrol。
like.
喜欢的。
private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
{
UserControl1 uc1 = new UserControl1();
var win = new MainWindow();
win.Content = win1;
win.Show();
}
#6
0
If you'd like to do it like a popup:
如果你想像弹窗那样做:
private void btn1_click(object sender, RoutedEventArgs e)
{
newWin win2 = new newWin();
win2.Show():
}