WP7应用程序冻结还是挂起? [英] WP7 application freezes or hangs?

查看:109
本文介绍了WP7应用程序冻结还是挂起?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我正在尝试读取项目文件夹本身随附的文本文件,该文件位于单独的文件夹中.我正在尝试阅读此文本文件,然后将每一行添加到列表中,并将每一行作为列表中的单独项添加,然后将其绑定到列表框,并且每个列表框项(之前的每一行)都将是一个超链接在列表框中.令人非常沮丧的是,一旦代码在运行时开始执行,该应用程序就会冻结一次.可能是什么问题?

Hi guys, I am trying to read a text file that I have included with the project folder itself in a separate folder. I am trying to read this text file and then add each line to a list, each line as a separate item in the list, then I am looking to bind it to a listbox and each listbox item (each line previously) would be a hyperlink in the listbox. It''s been very frustrating since the app just freezes every single time as soon as the code starts executing at runtime. What could be the problem?

public partial class Page2 : PhoneApplicationPage
   {
       public Page2()
       {
           InitializeComponent();

           // Will contain the names of malls added through a text file

           List<string> Mall_List = new List<string>();

           using(StreamReader reader = new StreamReader("/Mall_List/Mall_List.txt"))
           {
                   while(reader.Peek() >= 0)
                   {
                       Mall_List.Add(reader.ReadLine());
                   }

                   reader.Close();
           }

               Malllist.ItemsSource = Mall_List;
        }

   }




我现在还不知道如何将列表框项显示为超链接,因此非常感谢您的帮助!




I have no idea at this point how to display the listbox items as hyperlinks so help is much appreciated, thanks!

推荐答案

首先,请不要在构造函数中执行此操作.如果初始化,则将此部分推迟到应用程序的主事件处理周期已经开始工作时(例如,当主窗口已显示时).这样,可以使用在主UI循环中捕获所有异常的机制.

如果此文件读取和处理过程也可以花费大量时间,请以更准确的方式进行:在单独的线程中进行.

这可能不是您观察到的悬挂的真正原因.您需要确保在调试器下执行代码.



而且,在任何情况下,无论是相对还是绝对路径,都没有像"/Mall_List/Mall_List.txt"这样的硬编码路径有用的情况.现在,您的代码取决于工作目录,并且该目录可以在任何位置.它由用户定义,如何启动应用程序.

可以找到相对于可执行目录或特殊文件夹"(System.Environment.GetFolderPath http://msdn.microsoft.com/en-us/library/system.environment.getfolderpath.aspx [
First of all, never do it in the constructor. Defer this part if initialization until the moment when the main event handling cycle of the application is already working, for example, when the main window is already shown. This way, the mechanism of catching of all of the exception in the main UI loop can be used.

If this file reading and processing procedure can also get some considerable time, do it in even more accurate way: do it in the separate thread.

This might be not the real reason of the hanging you observe. You need to execute your code under debugger to be sure.



Also, there are no situations where a hard-coded path like your "/Mall_List/Mall_List.txt" could be useful, no matter if this is a relative or absolute path. Now your code depends on working directory, and this directory can be anywhere at all; it''s defined by the user, how an application is started.

The file can be found relative to the executable directory, or "special folder" (System.Environment.GetFolderPath, http://msdn.microsoft.com/en-us/library/system.environment.getfolderpath.aspx[]), or calculated based on some configuration data — never hard-coded.



I looks like you mix up working directory and executable directory. The working directory has nothing to do with the directory where your executable files are — it is defined be the user and can be anywhere. This is a parameter in a .LNK file; and if started directly, is still defined by the user. Most usually, this is the current working directory of some other program, like a file manager (Explorer or any other) at the moment when the user starts the application. So, it can be anywhere; and your program can change it. When you use a relative path, the actual path is found relative to the current working directory, which can be anything. This approach is often used in console applications, where a more experienced user directly controls the working directory and know where to provide and expect files.

The directory where your executable files are, in contrast, never changes unless you move those executable files and start the application again. Here is how to find it:
string location = System.Reflection.Assembly.GetEntryAssembly().Location;
string executableDirectory = System.IO.Path.GetDirectoryName(location);



请注意,这是一种可靠的方法;在其他一些特殊情况下,还有其他一些方法可能会使您产生混乱的结果,但是在所有情况下,这种方法都是相同的.我在这里更详细地解释它:
如何找到我的程序目录 [ 如何找到我的程序目录 [].

我认为现在您应该对目录问题有全面的了解.

—SA



Please pay attention, that this is a reliable method; there are other methods which can give you confusing results in some more special cases, but this will work the same way in all cases. I explain it in some more detail here:
How to find my programs directory [ (executable directory) ].

And I advised in the working directory and "special folder" here:
How to find my programs directory [ (current directory and "special folder") ].

I think now you should have comprehensive information on the directory issues.

—SA


这篇关于WP7应用程序冻结还是挂起?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆