WPF Properties.Settings 在启动和关闭时引发异常

发布于 2024-09-14 10:29:51 字数 2166 浏览 2 评论 0原文

当我的 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 技术交流群。

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

发布评论

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

评论(1

三岁铭 2024-09-21 10:29:51

根据我的经验,这种例外并不罕见。不寻常的是,在您的情况下,该异常(显然)未得到处理。通常,(再次根据我的经验)该异常会被其他机制默默地捕获和处理。

无论如何,你不会在 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.

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