2011-03-05 23 views
2

这是一个奇怪的!我正在研究一个读取vCard文件的应用程序,其中包含一个人的联系信息等。每个文件可能包含单独的“部分”,每个部分包含一个人的详细信息,它们由BEGIN:VCARD [data here] END:VCARD分隔。c# - 打开文件的WPF命令行参数给出无限循环

为了让我的用户能够查看所有不同的细节,我已经允许我的程序在应用程序的文本框中填充细节,然后打开一个新窗口并使用该窗口执行此操作,但对于每个窗口文件中的不同部分。

问题出现在我的程序打开时,双击Explorer中的vCard文件。它通过vCard持续循环。我不知道该怎么做,但下面是我的问题的代码:

public void readVcard(string fname)//Reads vCard and then loops through sections 
    { 
     try 
     { 
      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; 

       for (i = 1; i < vArray.Length; i++) 
       { 
        MainWindow a = new MainWindow(); 
        a.parser(vArray[i]); //Parser is the function that populates the app 
        a.Show(); 
       } 

       return; 
      } 
     }... 

这个函数就是从这里称为:

void MainWindow_Loaded(object sender, RoutedEventArgs e)//Processes a file when opened externally 
    { 
     if (Application.Current.Properties["ArbitraryArgName"] != null) 
     { 
      string fname = Application.Current.Properties["ArbitraryArgName"].ToString(); 

      readVcard(fname); 

     } 
    } 

如果有人可以帮助,这将不胜感激。

+0

什么都没有作为一个无限循环突出。你能指定它看起来卡在哪一行吗?你怎么知道这是一个无限循环与类似僵局的事情? – vcsjones 2011-03-05 18:50:59

+0

如果我删除'for'循环并指定'parser(vArray [1]);'例如,它工作正常。如果我将循环保留在那里,它就会继续循环。 – user646265 2011-03-05 19:00:29

+0

您是否尝试过手动将调试器附加到循环应用程序以查看应用程序正在执行的操作? – madd0 2011-03-05 19:17:27

回答

0

我明白了!我现在在App.xaml.cs中有以下代码:

public partial class App : Application 
{ 
    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); 

     } 
    } 

} 

它工作正常!感谢大家。顺便说一句,以下博客包含我最初使用的命令行信息,如果有人需要它:http://blogs.msdn.com/b/avip/archive/2008/10/27/wpf-supporting-command-line-arguments-and-file-extensions.aspx

1

当您创建并显示新的MainWindow(a.Show())时,MainWindow_Loaded事件再次触发,并再次调用readVcard方法。所以有一个无限循环。

或者可能并非真的无限,因为我相信,一段时间后可能会发生StackOverflowException。

您只需查看启动逻辑,因此readVcard将不会在MainWindow_Loaded事件中启动,而是在Main方法(在program.cs文件中)中启动。或者你可以添加一些标志,当readVcard方法第一次被调用时它将被设置。

3

我认为Artyom走上了正轨。

每次创建另一个主窗口并加载它,你会得到当前应用argurment和跳跃回至readVcard,这将处理您已经在处理相同的vCard和开放的又一主窗口这将继续这个过程。

考虑所有你MainWindow_Loaded内的代码的移动()启动事件为您的应用。这样,它只会在程序第一次加载时调用一次,而不是每次创建新窗口。用于读取电子名片

<Application x:Class="MyProgram.App" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Startup="Application_Startup"> 
</Application> 

然后在后面的App.xaml的代码你把你的代码:

要做到这一点,你需要为事件在App.xaml文件进行注册,像这样。像这样:

namespace MyProgram 
{ 
    /// <summary> 
    /// Interaction logic for App.xaml 
    /// </summary> 
    public partial class App : Application 
    { 
     private void Application_Startup(object sender, StartupEventArgs e) 
     { 
      if (Application.Current.Properties["ArbitraryArgName"] != null) 
      { 
       string fname = Application.Current.Properties["ArbitraryArgName"].ToString(); 

       readVcard(fname); 

      } 
     } 
    } 
} 
+0

我已经添加了这个,但它不加载文件。 – user646265 2011-03-05 21:23:16

+0

啊,我调整了它,它现在起作用了!谢谢! – user646265 2011-03-06 10:40:37

相关问题