WPF - C#:此元素当前不与任何上下文关联

发布于 2024-08-18 04:43:10 字数 1329 浏览 9 评论 0原文

我有一个 WPF UserControl,我在 ElementHostControl 内的 WinForms 控件中使用它。然后我启动一个 WPF 窗口,同时 Windows 窗体仍处于打开状态。如果我随后关闭 WPF 窗口,并尝试将子元素添加到我的 WPF UserControl,它会在(子元素的)“InitializeComponent()”处崩溃,但有例外:

“System.Configuration.dll 中首次出现“System.Configuration.ConfigurationErrorsException”类型的异常。 附加信息:此元素当前未与任何上下文关联”

我找到了发生这种情况的原因,但我不知道如何解决问题。如果我省略此代码:

public static void EnsureApplicationResources()
    {
        if (Application.Current == null)
        {
            // create the Application object
            new Application();
            string assemblyName = System.IO.Path.GetFileNameWithoutExtension(
                Assembly.GetExecutingAssembly().ManifestModule.Name);

            // merge in your application resources
            Application.Current.Resources.MergedDictionaries.Add(
                Application.LoadComponent(new Uri("/KolonistenClient;component/KolonistenResourceDictionary.xaml", UriKind.RelativeOrAbsolute))
                as ResourceDictionary);
        }
    }

那么一切都很好。但我需要它,因为事实上,我的 ResourceDictionary(以及我定义的样式和模板)在整个 WPF 窗口和控件中均不可用 这是我在这里发现的

有没有办法结合两全其美?保留我的 ResourceDictionary,同时阻止应用程序从崩溃?

I have a WPF UserControl, which I use in a WinForms Control within an ElementHostControl. Then I start a WPF Window, while the Windows Form is still open. If I then close the WPF Window, and try to add a Child Element to my WPF UserControl, it crashes at "InitializeComponent()" (of the Child Element) with the exception:

"A first chance exception of type 'System.Configuration.ConfigurationErrorsException' occurred in System.Configuration.dll.
Additional information: This element is not currently associated with any context"

I found out why it happens, but I don't know how to solve the problem. If I leave out this code:

public static void EnsureApplicationResources()
    {
        if (Application.Current == null)
        {
            // create the Application object
            new Application();
            string assemblyName = System.IO.Path.GetFileNameWithoutExtension(
                Assembly.GetExecutingAssembly().ManifestModule.Name);

            // merge in your application resources
            Application.Current.Resources.MergedDictionaries.Add(
                Application.LoadComponent(new Uri("/KolonistenClient;component/KolonistenResourceDictionary.xaml", UriKind.RelativeOrAbsolute))
                as ResourceDictionary);
        }
    }

then everything is fine. I need that though, because of the fact that my ResourceDictionary (and thus also my defined styles and templates) is otherwise not available throughout the WPF Window and Controls. This I found out about here

Is there any way to combine the best of both worlds? Keeping my ResourceDictionary, while preventing the application from crashing?

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

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

发布评论

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

评论(1

舞袖。长 2024-08-25 04:43:10

我最终通过 xaml 在每个窗口中手动添加 ResourceDictionary 解决了这个问题。这样我的 WinForms 中的 UserControl 就不会受到影响。

不幸的是,仍然不明白为什么它崩溃了。

<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/KolonistenClient;component/KolonistenResourceDictionary.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Window.Resources>

I solved it eventually by adding the ResourceDictionary in each Window manually through xaml. This way the UserControl in my WinForms stays unaffected.

Still don't understand exactly why it crashed though, unfortunately.

<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/KolonistenClient;component/KolonistenResourceDictionary.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Window.Resources>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文