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