我有一个WPF用户控件,这是我在ElementHostControl内一个WinForms控制使用。然后,我开始一个WPF窗口,而Windows窗体仍然是开放的。如果我再关闭WPF窗口,并尝试一个子元素添加到我的WPF用户控件,它具有异常崩溃的“的InitializeComponent()”(子元素):

  

“型的第一次机会异常‘System.Configuration.ConfigurationErrorsException’发生在System.Configuration.dll。   附加信息:该元素当前不与任何上下文中,“相关联的

我发现为什么会发生,但我不知道如何解决这个问题。如果我离开了这个代码:

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,同时防止从崩溃应用程序?

有帮助吗?

解决方案

我通过XAML手动在每个窗口中添加的ResourceDictionary解决它最终。这样,在我的WinForms用户控件保持不变。

还是不明白到底的为什么的它虽然坠毁,很遗憾。

<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/KolonistenClient;component/KolonistenResourceDictionary.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Window.Resources>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top