c# - WPF打开文件外部不会在关闭时终止进程而不解析数据

时间:2011-03-12 18:06:34

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

我最近一直在开发一个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;
                            }

1 个答案:

答案 0 :(得分:0)

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

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