![](/img/trans.png)
[英]How to open another view on button click without creating a Window in Titanium?
[英]How do I open a xaml window from another xaml window using button click?
private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
{
Win1 OP= new Win1();
OP.show();
}
OP.show()拋出一個錯誤。
它是一個用戶控制表單。
你說Win1
是“它是一個用戶控制表單”。 (重點是我的)。
如果Win1
實際上是UserControl
類型,則問題是UserControl
類型沒有定義Show()
方法。 因此它不能作為窗口“打開”。
要解決此問題,您需要打開一個窗口並將UC作為該窗口的內容:
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
,它將起作用,因為框架識別出它不是窗口並為其創建窗口。
所有這些答案都很好,但是他們沒有利用WPF的導航功能。 如果你打算隱藏/關閉當前窗口,那么這是舊的WinForms
窗口管理的一個很好的替代品。 而不是創建多個窗口,創建頁面,只需使用MainWindow中的單個Frame控件顯示它們:
<Grid>
<Frame Source="/Menu.xaml" NavigationUIVisibility="Hidden"/>
</Grid>
然后,當您想要顯示另一個“窗口”(實際上是一個頁面)時,您可以使用NavigationService
來更改幀源。 從菜單頁面代碼后面:
private void Button_Click(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/Contact.xaml", UriKind.Relative));
}
現在將顯示“聯系”頁面。 使用此方法可確保窗口屬性標准化。 一個窗口統治他們所有!
打開一個窗口並關閉第一個窗口:
private void Button_Click(object sender, RoutedEventArgs e)
{
window2 win2= new window2();
win2.Show();
this.Close();
}
您無法打開UserControl作為窗口或對話框。 最好通過后面的代碼或XAML將usercontrol添加到某個Window中,然后打開該窗口。 不要忘記設置該窗口的DataContext。 請記住,父控件/窗口的datacontext將由子控件/用戶控件繼承。
您無法直接顯示usercontrol,您應該在另一個窗口中呈現您的usercontrol。
喜歡。
private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
{
UserControl1 uc1 = new UserControl1();
var win = new MainWindow();
win.Content = win1;
win.Show();
}
如果你想像彈出窗口那樣做:
private void btn1_click(object sender, RoutedEventArgs e)
{
newWin win2 = new newWin();
win2.Show():
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.