WPF 在当前窗口关闭循环中打开新窗口

发布于 2024-10-20 17:22:22 字数 1285 浏览 4 评论 0原文

我正在开发一个程序,该程序读取文本文件并将其部分内容存储在数组中的块中,然后(这是理论)解析每个块(索引)中的文本并使用解析的数据填充一系列文本框。因为每个块都会包含相同字段的数据,所以我的想法是当用户关闭当前窗口时打开一个新窗口并处理下一个索引中的数据。这将继续,直到达到数组的长度。

我的问题是我不知道打开一个新窗口并为未知数量的索引增加数组的索引。

这是我用于存储块的代码:

            using (StreamReader r = new StreamReader(fname))
            {

                string input = File.ReadAllText(fname);//read through file

                String[] vArray = input.Split(new string[] { "BEGIN:VCARD" }, StringSplitOptions.None); ...

然后我用于处理它的代码是:

                            int i;

                        parser(vArray[1]);

                        MainWindow a = new MainWindow();
                        a.parser(vArray[2]);
                        a.Show();

                        for (i = 1; i < vArray.Length - 2; i++)
                        {                                
                            a.Closing += delegate(object sender, System.ComponentModel.CancelEventArgs e)
                            {
                                MainWindow b = new MainWindow();
                                b.parser(vArray[i++]);
                                b.Show();
                            };
                        }

这似乎很明显,但我似乎无法理解它:)任何帮助都将受到欢迎和赞赏。

I am working on a program that reads a text file and stores parts of it in chunks in an array and then (this is the theory) parses the text in each block (index) and populates a series of textboxes with the parsed data. Because each block will contain data of the same fields, my idea is to open a new window when the user closes the current one and process the data in the next index. This will continue until the array's length has been reached.

My issue is that I don't know to open a new window and increment the array's index for an unknown number of indices.

This is my code for storing the chunks:

            using (StreamReader r = new StreamReader(fname))
            {

                string input = File.ReadAllText(fname);//read through file

                String[] vArray = input.Split(new string[] { "BEGIN:VCARD" }, StringSplitOptions.None); ...

and then my code for processing it is:

                            int i;

                        parser(vArray[1]);

                        MainWindow a = new MainWindow();
                        a.parser(vArray[2]);
                        a.Show();

                        for (i = 1; i < vArray.Length - 2; i++)
                        {                                
                            a.Closing += delegate(object sender, System.ComponentModel.CancelEventArgs e)
                            {
                                MainWindow b = new MainWindow();
                                b.parser(vArray[i++]);
                                b.Show();
                            };
                        }

This seems really obvious, but I just can't seem to get it :) Any help would be welcomed and appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

影子的影子 2024-10-27 17:22:22

一个问题似乎是您多次订阅第一个表单上的 Closing 事件,当该表单时,所有其他表单都会立即弹出。

我建议使用 ShowDialog< /a> 而不是显示
ShowDialog 阻止执行,直到打开的表单关闭。然后您可以删除任何关闭事件。

然后您也可以将所有内容简化为 foreach 循环。

foreach (var v in vArray)
{ 
    MainWindow mainWindow = new MainWindow();
    mainWindow.parser(v);
    mainWindow.ShowDialog();
}

One issue seems that you subscribe to the Closing event on the first form many times, when that forms all the other forms will pop up at once.

I would recommend using ShowDialog instead of Show.
ShowDialog blocks execution until the opened form is closed. Then you can remove any Closing events.

Then you can simplify everything to a foreach loop too.

foreach (var v in vArray)
{ 
    MainWindow mainWindow = new MainWindow();
    mainWindow.parser(v);
    mainWindow.ShowDialog();
}
深居我梦 2024-10-27 17:22:22

尝试 Window.ShowDialog 而不是展示。

不同之处在于,使用 showdialog,程序会等待窗体关闭后再继续。

Try Window.ShowDialog instead of show.

The difference is that with showdialog, the program waits for the form to be closed before continuing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文