c# - WPF 从外部打开文件,关闭时不会终止进程,并且不会解析数据

发布于 2024-10-21 18:43:49 字数 3165 浏览 0 评论 0原文

我最近一直在开发一个 vCard 解析器,它可以打开 vCard 文件并使用文件中的数据填充一系列文本框。当我的应用程序与 vCard 关联时,vCard 打开程序,并且文件名通过 statup 函数传递给名为 readVcard 的函数。然后,每个 BEGIN:VCARD 语句将其分割成一个名为 string 的数组。如果长度大于 1,窗口将显示一个对话框,其中包含数组中索引中的数据,当该对话框关闭时,将打开一个新对话框,直到读取内容为止。当最后一个窗口关闭时,如果文件是从外部打开的,则程序不会终止,但如果是在内部打开,则程序可以正常工作!

我在解析 readVcard 的数据时也遇到问题。我已经测试过它,数据在 readVcard 函数中使用,但没有传递到解析器函数,它实际上在解析器函数中解析它。同样,当文件在内部打开但在外部打开时,这可以正常工作。

这是我的启动事件:

    protected override void OnStartup(StartupEventArgs e)
    {
        if (e.Args != null && e.Args.Count() > 0)
        {
            this.Properties["ArbitraryArgName"] = e.Args[0];
        }
        base.OnStartup(e);

        if (Application.Current.Properties["ArbitraryArgName"] != null)
        {
            string fname = Application.Current.Properties["ArbitraryArgName"].ToString();
            MainWindow mw = new MainWindow();
            mw.readVcard(fname);
            //Application curApp = Application.Current;
            //curApp.Shutdown();
        }

    }

读取 vCard 函数如下:

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

                progressBar1.Value = 10;

                input = input ?? "---This file did not contain any text---jlb95";

                if (input != "---This file did not contain any text---jlb95" || input != "")
                {

                    if (!input.Contains("BEGIN:VCARD") || !input.Contains("END:VCARD"))
                    {
                        MessageBox.Show("This file: " + fname + " is not formatted correctly." + "\r\n Error: 001", "File not formatted correctly", MessageBoxButton.OK, MessageBoxImage.Error);
                        progressBar1.Value = 0;
                    }

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

                        if (vArray.Length > 1)
                        {
                            MessageBoxResult dialog = MessageBox.Show("This vCard File contains multiple contacts. The program can loop through them and will open a new Window for each one" +
                                " when the current window is closed or it can open the first contact. Do you want to open all the contacts?", "File contains multiple contacts", MessageBoxButton.YesNo, MessageBoxImage.Question
                                , MessageBoxResult.Yes);

                            if (dialog == MessageBoxResult.Yes)
                            {
                                progressBar1.Value = 20;

                                foreach (var v in vArray)
                                {
                                    MessageBox.Show(v);
                                    MainWindow mainWindow = new MainWindow();
                                    mainWindow.parser(v, fname);
                                    mainWindow.ShowDialog();

                                }

                                progressBar1.Value = 0;

                                return;
                            }

I've recently been working on a vCard parser that opens a vCard file and populates a series of textboxes with data from the files. When my application is associated with a vCard, the vCard opens the program and the filename is passed via a statup function to a function called readVcard. This is then split by each BEGIN:VCARD statement into an array called string. If the length is greater than 1, the Window shows a dialog with the data from the index in an array and when that dialog is closed a new one opens until the contents have been read. When the last Window is closed, the program doesn't terminate if the file is opened externally but if it opens internally it works fine!

I also have an issue with it parsing the data from readVcard. I have tested it and the data is used in the readVcard function, but not passed on to the parser function, where it actually parses it. Again this works fine when the file is opened internally, but not externally.

This my startup event:

    protected override void OnStartup(StartupEventArgs e)
    {
        if (e.Args != null && e.Args.Count() > 0)
        {
            this.Properties["ArbitraryArgName"] = e.Args[0];
        }
        base.OnStartup(e);

        if (Application.Current.Properties["ArbitraryArgName"] != null)
        {
            string fname = Application.Current.Properties["ArbitraryArgName"].ToString();
            MainWindow mw = new MainWindow();
            mw.readVcard(fname);
            //Application curApp = Application.Current;
            //curApp.Shutdown();
        }

    }

The read vCard function is below:

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

                progressBar1.Value = 10;

                input = input ?? "---This file did not contain any text---jlb95";

                if (input != "---This file did not contain any text---jlb95" || input != "")
                {

                    if (!input.Contains("BEGIN:VCARD") || !input.Contains("END:VCARD"))
                    {
                        MessageBox.Show("This file: " + fname + " is not formatted correctly." + "\r\n Error: 001", "File not formatted correctly", MessageBoxButton.OK, MessageBoxImage.Error);
                        progressBar1.Value = 0;
                    }

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

                        if (vArray.Length > 1)
                        {
                            MessageBoxResult dialog = MessageBox.Show("This vCard File contains multiple contacts. The program can loop through them and will open a new Window for each one" +
                                " when the current window is closed or it can open the first contact. Do you want to open all the contacts?", "File contains multiple contacts", MessageBoxButton.YesNo, MessageBoxImage.Question
                                , MessageBoxResult.Yes);

                            if (dialog == MessageBoxResult.Yes)
                            {
                                progressBar1.Value = 20;

                                foreach (var v in vArray)
                                {
                                    MessageBox.Show(v);
                                    MainWindow mainWindow = new MainWindow();
                                    mainWindow.parser(v, fname);
                                    mainWindow.ShowDialog();

                                }

                                progressBar1.Value = 0;

                                return;
                            }

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

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

发布评论

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

评论(1

别担心:已修复。该方法实际上并没有调用 mw 来显示,因此数据没有被处理,并且在当前窗口关闭时它被挂起。

        if (Application.Current.Properties["ArbitraryArgName"] != null)
        {
            string fname = Application.Current.Properties["ArbitraryArgName"].ToString();
            MainWindow mw = new MainWindow();
            mw.Show();
            mw.readVcard(fname);
        }

Don't worry: fixed it. The method didn't actually call mw to show, so the data wasn't being processed and it was left hanging when the current window was closed.

        if (Application.Current.Properties["ArbitraryArgName"] != null)
        {
            string fname = Application.Current.Properties["ArbitraryArgName"].ToString();
            MainWindow mw = new MainWindow();
            mw.Show();
            mw.readVcard(fname);
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文