0

我创建了另一个 WPF 窗口.. 说 window2.xaml。我有一个按钮..点击我希望它加载那个窗口..我试过谷歌搜索,但似乎没有任何效果。它只是加载一个空白页面。我知道这很简单,但我真的无法通过搜索找到如何做到这一点。

这是我尝试过的:

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

编辑:我发现了问题。我取出了初始化组件,因为有一个错误。我认为这只是主窗口需要的东西。当我把它放回去时,它起作用了。感谢大家。

4

2 回答 2

1

在创建 GameClock 实例之前,您可能需要读取 XAML 文件。像这样的东西:

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

于 2013-10-07T19:08:47.797 回答
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);
    }
}
于 2013-10-07T19:09:21.907 回答