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

发布于 2024-10-20 17:52:58 字数 1435 浏览 7 评论 0原文

这是一件奇怪的事!我正在开发一个读取 vCard 文件的应用程序,其中包含一个人的联系方式等信息。每个文件可能包含单独的“部分”,每个部分包含一个人的详细信息,这些详细信息由 BEGIN:VCARD [此处的数据] 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);

        }
    }

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

This is a weird one! I am working on an application that reads vCard files, which contain contact etc. information for a person. Each file may contain separate 'sections' that each contain the details for one person, which are separated by BEGIN:VCARD [data here] END:VCARD.

To enable my users to view all of the different details, I've allowed my program to populate the textboxes in my app with the details and then open a new Window and do this with that one, but for each of the different sections in the file.

The problem comes about when my program opens when a vCard file has been double clicked in Explorer. It keeps looping through the vCard. I don't know what to do, but below is my problematic code:

    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;
            }
        }...

This function is called from here:

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

        }
    }

If anyone could help, it would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寄人书 2024-10-27 17:52:58

我认为 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);

            }
        }
    }
}

I think that Artyom is on the right track.

Every time you create another MainWindow and load it you will be getting the current applications argurment and jumping back in to readVcard, which will process the same vCard that you are already processing and open yet another MainWindow which will continue the process.

Consider moving all of the code you have inside of MainWindow_Loaded() to the Startup event for your application. That way it will only get called once when your program first loads, instead of every time you create a new window.

To do this you need to register for the event in your App.xaml file like so:

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

And then in the code behind App.xaml you put your code for reading the vCard. Like this:

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

            }
        }
    }
}
冷月断魂刀 2024-10-27 17:52:58

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

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

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

When you create and show new MainWindow (a.Show()), the MainWindow_Loaded event fires again and it again calls a readVcard method. So there is an infinite loop.

Or may be not really infinite, because, I belive, some time later a StackOverflowException may happen.

You just need to review startup logic, so readVcard will launch not in the MainWindow_Loaded event, but, for example, in the Main method (in program.cs file). Or you may add some flag, which will be set when readVcard method first called.

冷夜 2024-10-27 17:52:58

我得到它!我现在在 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

I get it! I've now got the following code in 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);

        }
    }

}

It works fine! Thanks everyone. BTW the following blog contains the command-line info I originally used if anyone needs it: http://blogs.msdn.com/b/avip/archive/2008/10/27/wpf-supporting-command-line-arguments-and-file-extensions.aspx.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文