2013-10-07 89 views
0

我有另一個WPF窗口我創建..說window2.xaml。我有一個按鈕..然後點擊我想要它加載該窗口..我試過谷歌搜索,但似乎沒有工作。它只是加載一個空白頁面。我知道這很簡單,但我真的無法找到如何通過我的搜索。從一個按鈕加載一個新的WPF窗口

這是我曾嘗試:

GameClock temp = new GameClock(); 
temp.ShowDialog(); //just shows blank window 
temp.Show(); //just shows a blank window too 

編輯:我想通了這個問題。我拿出初始化組件,因爲出現錯誤。我認爲這只是主窗口所需要的。當我把它放回去時,它就會起作用。感謝大家。

+1

顯示您GameClock XAML –

+0

這基本上它。我添加的唯一東西是設置所有者:temp.Owner = this;你的櫥窗是否顯示在設計師手中? – Paul

+1

GameClock構造函數是否完成其「InitializeComponent」? –

回答

1

試試這個...ü可以用像普通的梅索德

private void button_ItemClick(object sender, ItemClickEventArgs e) 
{ 
    try 
    { 
     OpenWin("window2", new Uri(@"window2.xaml", UriKind.Relative), "Window2Label"); 
    } 
    catch (Exception ex) 
    { 
     Message.Show(ex); 
    } 
} 

public static DocumentPanel OpenWin(string namePainelItem, Uri xamlPath, string caption = "", RoutedEventHandler unloadEvent = null, bool closeOpenWin = false) 
{ 
    try 
    {   
     if (closeOpenWin) 
     { 
      CloseWin(namePainelItem, false); 
     } 

     DocumentPanel panel1 = GetWin(namePainelItem); 
     if (panel1 == null) 
     { 
      panel1 = new DocumentPanel(); 
      panel1.Caption = caption; 
      panel1.Name = namePainelItem; 
      panel1.Content = xamlPath; 

      if (unloadEvent != null) 
      { 
       panel1.Unloaded += unloadEvent;       
      }    

      hdl.dockLayoutManager.DockController.Insert(hdl.documentGroup1, panel1, 1); 
      hdl.dockLayoutManager.DockController.ActiveItem = panel1; 
     } 
     else 
     { 
      if (panel1.Visibility != Visibility.Visible) 
       panel1.Visibility = Visibility.Visible; 

      if(panel1.IsClosed) 
       panel1.Closed = false;      

      hdl.dockLayoutManager.DockController.ActiveItem = panel1; 
     } 
     return panel1; 
    } 
    catch (Exception ex) 
    { 
     Message.Show(ex);     
    } 
    return new DocumentPanel(); 
} 


public static void CloseWin(string namePainelItem) 
{ 
    try 
    { 
     BaseLayoutItem item = hdl.dockLayoutManager.GetItem(namePainelItem); 

     if (item != null) 
     { 
      hdl.documentGroup1.Items.Remove(item); 
      hdl.dockLayoutManager.DockController.RemovePanel((DocumentPanel)item); 
      item = null; 
     } 
    } 
    catch (Exception ex) 
    { 
     Message.Show(ex); 
    } 
} 
1

在創建GameClock實例之前,您可能需要閱讀XAML文件。事情是這樣的:

GameClock clock; 
FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read); 
clock = (GameClock)XamlReader.Load(fs); 

JAB

相關問題