问题描述
在我的winphone应用程序中,我有一个奇怪的情况,我在与文件无关的代码中得到一个 System.IO.FileNotFoundException
。
In my winphone app I have a strange situation where I get a System.IO.FileNotFoundException
in code that does nothing related to files.
我有一个类来管理延迟函数调用:
I have a class that manages delayed function calls:
// Delayed function manager
namespace Test
{
public static class At
{
private readonly static TimerCallback timer =
new TimerCallback(At.ExecuteDelayedAction);
public static void Do(Action action, TimeSpan delay,
int interval = Timeout.Infinite)
{
var secs = Convert.ToInt32(delay.TotalMilliseconds);
new Timer(timer, action, secs, interval);
}
public static void Do(Action action, int delay,
int interval = Timeout.Infinite)
{
Do(action, TimeSpan.FromMilliseconds(delay), interval);
}
public static void Do(Action action, DateTime dueTime,
int interval = Timeout.Infinite)
{
if (dueTime < DateTime.Now) return;
else Do(action, dueTime - DateTime.Now, interval);
}
private static void ExecuteDelayedAction(object o)
{
(o as Action).Invoke();
}
}
}
还有一个管理ProgressIndicator的类状态:
And a class that manages ProgressIndicator state:
namespace Test
{
public class Indicator
{
public DependencyObject ThePage;
public ProgressIndicator Progressor;
public Indicator(DependencyObject page)
{
ThePage = page;
Progressor = new ProgressIndicator();
SystemTray.SetProgressIndicator(ThePage, Progressor);
}
// If set(true) then set(false) in one second to remove ProgressIndicator
public void set(bool isOn)
{
Progressor.IsIndeterminate = Progressor.IsVisible = isOn; // Exception happens on this line
if (isOn) At.Do(delegate { this.set(false); }, 1000);
}
}
}
当我尝试运行 set(true)
代码中的方法我得到以下异常:
When I try to run set(true)
method in code I get the following exception:
An exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.ni.dll and wasn't handled before a managed/native boundary
A first chance exception of type 'System.UnauthorizedAccessException' occurred in System.Windows.ni.dll
为什么会发生这种情况,如何解决?
Why is this happening and how can this be fixed?
推荐答案
看起来你的实际问题是权限, FileNotFound
异常可能是因为没有能够读取文件位置目录。
Looks like your actual problem is permissions, the FileNotFound
exception is probably happening as a result of not being able to read the file location directory.
我没有Windows Phone开发的任何经验,但是,我假设在任何位置,$ code> System.Windows.ni.dll 需要比您运行应用程序的权限更高的权限。
I don't have any experience with Windows Phone development, however, I assume wherever System.Windows.ni.dll
is resided requires higher permissions than what you are running your app under.
更新
根据您的调用堆栈错误消息 - 无效的跨线程访问,问题是您尝试从不是UI线程的另一个线程访问/更新GUI组件。尝试将您的代码更改为:
Based on your call stack error message - "Invalid cross-thread access", the problem is your attempting to access/update a GUI component from another thread which isn't the UI thread. Try changing your code to:
Deployment.Current.Dispatcher.BeginInvoke(()=>
{
Progressor.IsIndeterminate = Progressor.IsVisible = isOn;
if (isOn)
At.Do(delegate { this.set(false); }, 1000);
}
});
这篇关于System.IO.FileNotFoundException在延迟函数中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!