加载窗口时获取页面的“父”窗口(WPF)

时间:2019-07-18 07:19:18

标签: c# wpf

在我的程序中,我必须打开一个包含两个页面的窗口,并且需要向这些页面传递一些字符串信息。我在“父”窗口上使用属性来执行此操作,该属性是在Window构造函数上初始化的。

当我尝试在其构造函数上获取这些页面之一的父窗口时,出现了我的问题。我相信我必须这样做,因为我一开始就需要字符串信息来填充组合框。

我尝试通过这种方式获取窗口:

parentWindow = (AsistenteWindow)Window.GetWindow(this);

但是,当Page初始化时,它给了我 NullReferenceException 。我以为问题是在进行Page初始化时Window没有完全加载,但是我不确定。

我的窗口代码是:

public partial class AsistenteWindow : Window
    {

        #region Atributos
        public string Resultado { get; set; } = "";
        public string ModeloDispSeleccionado { get; set; } = "";
        public string NumeroCluster { get; set; }
        public string TextoCluster { get; set; }
        #endregion

        public AsistenteWindow(string modelo)
        {
            InitializeComponent();

            ModeloDispSeleccionado = modelo;
        }
    }

字符串属性是我必须从页面访问的属性。

我的第一个Page代码是:

public partial class AsistenteClusterPage : Page
    {

        private Dictionary<string, string> dicClusters = new Dictionary<string, string>();
        private AsistenteWindow parentWindow;


        public AsistenteClusterPage()
        {
            InitializeComponent();

            parentWindow = (AsistenteWindow)Window.GetWindow(this);

            try
            {
                // crea un diccionario con todos los clusters y el texto para mostrar, asegurando que no se repitan
                foreach (DataRow mccRow in SQLiteHelper.TramasDataSet.Tables["ModeloClusterCanal"].Rows)
                {
                    if (mccRow["ModeloId"].ToString() == parentWindow.ModeloDispSeleccionado)
                    {
                        foreach (DataRow clRow in SQLiteHelper.TramasDataSet.Tables["Cluster"].Rows)
                        {
                            if (clRow["ClusterId"].ToString() == mccRow["ClusterId"].ToString()
                                && !dicClusters.ContainsKey(clRow["ClusterId"].ToString()))
                            {
                                dicClusters.Add(clRow["ClusterId"].ToString(), clRow["ClusterName"] + " (" + clRow["ClusterId"].ToString() + ")");
                            }
                        }
                    }
                }

                // rellena el ComboBox con los clusters
                foreach (KeyValuePair<string, string> keyValue in dicClusters)
                {
                    cbCluster.Items.Add(new ComboBoxItem(keyValue.Value, keyValue.Key));
                }
            }

            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + "/n" + ex.StackTrace);

            }
        }
    }

(我不会发布另一页的代码,因为它太长了,但是我还必须以类似的方式从那里访问字符串信息,所以我想也会有问题)

我还需要能够从Pages中关闭父窗口。

是否有更好的方法?我是否应该不使用父窗口中的属性,然后应该以其他方式传递字符串信息?

1 个答案:

答案 0 :(得分:0)

我最终使用Application.Current.Properties字典来存储字符串信息,就像this post中所说的那样。它没有错误。

然后,如许多有关获取父窗口的问题所述,我使用Window.GetWindow(this).Close();来关闭窗口。这不会产生任何错误,因为我已经在加载窗口时得到了窗口(按下按钮时它将关闭)。

我想这是我现在可以获得的最佳方法。仍然不知道使用Application.Current.Properties是否是最佳选择吗?

编辑:我现在不使用Window.GetWindow(this).Close();,而是使用按钮的IsCancel属性,如@XAMIMAX所述。