【问题标题】:c# - WPF opening file externally not terminating process when closed and not parsing datac# - WPF在外部打开文件时关闭且不解析数据时不会终止进程
【发布时间】:2011-03-12 18:06:34
【问题描述】:

我最近一直在研究一个 vCard 解析器,它可以打开一个 vCard 文件并使用文件中的数据填充一系列文本框。当我的应用程序与 vCard 相关联时,vCard 会打开程序,并且文件名通过 statup 函数传递给名为 readVcard 的函数。然后由每个 BEGIN:VCARD 语句将其拆分为一个称为字符串的数组。如果长度大于 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;
                            }

【问题讨论】:

    标签: c# wpf command-line-arguments vcf-vcard


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-01-03
      • 1970-01-01
      • 2020-10-21
      • 2019-01-04
      • 2023-03-12
      • 2023-03-21
      • 1970-01-01
      相关资源
      最近更新 更多