WPF Properties.Settings 在启动和关闭时引发异常
当我的 Wpf 应用程序启动时,我在 Settings.Designer.cs 中收到 BindingFailureException
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
public global::System.Collections.Specialized.StringCollection Projects {
get {
return ((global::System.Collections.Specialized.StringCollection)(this["Projects"]));
}
set {
this["Projects"] = value;
}
}
并显示以下消息:
The assembly with display name 'System.XmlSerializers' failed to load in the 'LoadFrom' binding context of the AppDomain with ID 1. The cause of the failure was: System.IO.FileNotFoundException: Could not load file or assembly 'System.XmlSerializers, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. The system cannot find the file specified.
在后面的我的 MainWindow 代码中,我正在 window_loaded
上执行以下操作:
try
{
if (Properties.Settings.Default.Projects==null)
Properties.Settings.Default.Projects=new StringCollection( );
var removalList = new List<string>( );
foreach (string project in Properties.Settings.Default.Projects)
{
if (System.IO.File.Exists(project))
OpenProject(project);
else
{
removalList.Add(project);
}
}
foreach (string missingProject in removalList) //so that we are not removing an item while iterating
{
Properties.Settings.Default.Projects.Remove(missingProject);
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString( ));
}
也在 window_fitting
中>
try
{
//TODO: save prompt
Properties.Settings.Default.Save( );
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString( ));
}
这也会引发相同的异常。为什么我在访问 Properties.Settings
时遇到异常?
When my Wpf app starts up I get a BindingFailureException in Settings.Designer.cs
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
public global::System.Collections.Specialized.StringCollection Projects {
get {
return ((global::System.Collections.Specialized.StringCollection)(this["Projects"]));
}
set {
this["Projects"] = value;
}
}
With the following message:
The assembly with display name 'System.XmlSerializers' failed to load in the 'LoadFrom' binding context of the AppDomain with ID 1. The cause of the failure was: System.IO.FileNotFoundException: Could not load file or assembly 'System.XmlSerializers, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. The system cannot find the file specified.
In My MainWindow code behind I am doing the following on window_loaded
:
try
{
if (Properties.Settings.Default.Projects==null)
Properties.Settings.Default.Projects=new StringCollection( );
var removalList = new List<string>( );
foreach (string project in Properties.Settings.Default.Projects)
{
if (System.IO.File.Exists(project))
OpenProject(project);
else
{
removalList.Add(project);
}
}
foreach (string missingProject in removalList) //so that we are not removing an item while iterating
{
Properties.Settings.Default.Projects.Remove(missingProject);
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString( ));
}
Also in window_closing
try
{
//TODO: save prompt
Properties.Settings.Default.Save( );
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString( ));
}
Which also throws the same exception. Why am I get an exception accessing the Properties.Settings
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据我的经验,这种例外并不罕见。不寻常的是,在您的情况下,该异常(显然)未得到处理。通常,(再次根据我的经验)该异常会被其他机制默默地捕获和处理。
无论如何,你不会在 VS 调试器中使用“抛出异常时中断”来运行它,是吗?该设置位于“调试”->“调试”下。顺便说一句,例外。
That exception is not so unusual in my experience. What is unusual is that the exception is (apparently) unhandled in your situation. Usually, (again in my experience) that exception is silently caught and handled by some other mechanism.
You are not, by any chance, running this in the VS debugger with 'break when an exception is thrown' are you? That setting is under Debug -> Exceptions..., by the way.