gpt4 book ai didi

c# - 打开文件的 WPF 命令行参数给出无限循环

转载 作者:行者123 更新时间:2023-11-30 21:16:25 26 4
gpt4 key购买 nike

这很奇怪!我正在开发一个读取 vCard 文件的应用程序,其中包含一个人的联系方式等信息。每个文件可能包含单独的“部分”,每个部分包含一个人的详细信息,由 BEGIN:VCARD [data here] END:VCARD 分隔。

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

当我的程序在资源管理器中双击 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);

}
}

如果有人能提供帮助,我们将不胜感激。

最佳答案

我认为 Artyom 走在正确的轨道上。

每次您创建另一个 MainWindow 并加载它时,您将获得当前应用程序参数并跳回到 readVcard,它将处理与您相同的 vCard已经处理并打开另一个 MainWindow 将继续该过程。

考虑将您在 MainWindow_Loaded() 中的所有代码移动到应用程序的 Startup 事件中。这样它只会在您的程序首次加载时被调用一次,而不是每次您创建一个新窗口时都会被调用。

为此,您需要像这样在 App.xaml 文件中注册事件:

<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 后面的代码中放入用于读取 vCard 的代码。像这样:

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);

}
}
}
}

关于c# - 打开文件的 WPF 命令行参数给出无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5205986/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com