开发者

WPF ResourceDictionary Loaded at Runtime Not Loading Referenced Assemblies

I've created a WPF application that contains some control templates which we can call 'default templates' and these templates are compiled into the application. This application also loads an external XAML file at runtime with additional control templates which can call 'custom templates'.

Everything works fine until I add a Callout control from the Expression Blend SDK to the 'custom' templates XAML that get loaded at runtime and then try to use that template. I get the following exception:

Cannot create unknown type '{http://schemas.microsoft.com/expression/2010/drawing}Callout'.

I noticed that if I put that Callout control in my 'default templates' file (the one that is compiled) and first use that default开发者_开发问答 template, the then load and use the 'custom templates' it will work.

It seems to me that the referenced expression sdk assembly is not being loaded when I add my 'custom templates' to my MergedDictionaries. Any ideas here?

This is the xmlns declaration at the top of the XAML file: xmlns:ed="http://schemas.microsoft.com/expression/2010/drawing

And this is the sub where I am adding the resource dictionary at runtime:

Private Sub LoadResourceFileButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
    Try
        Dim path As String = "\ResourceFiles\CustomMapSymbols.xaml"
        Dim resDict As New ResourceDictionary
        resDict.Source = New Uri(path, UriKind.Relative)

        Application.Current.Resources.BeginInit()
        Application.Current.Resources.MergedDictionaries.Add(resDict)
        Application.Current.Resources.EndInit()

    Catch ex As Exception
        MessageBox.Show(ex.ToString)
    End Try
End Sub


I have similar symptoms in my situation where I get the following error:

Cannot create unknown type '{htp://schemas.microsoft.com/expression/2010/drawing}Callout'.

However it works fine in a user control that is compiled in the application. This made me think that the ResourceDictionary implementation maybe doesn't handle the 'pretty' namespaces (htp://...) the same was as in the app.

So I tried referencing the explicit clr namespace directly in the declaration and it now works. I obviously had to change references in the rest of the xaml to ef:Callout. Easily done with auto replace. I didn't want to replace ed: with it in case I break Expression Blend that might be relying on it.

Old namespace declaration in the external resource dictionary:

xmlns:ed="htp://schemas.microsoft.com/expression/2010/drawing"

Sorry: I had to replace the http to htp to to post this answer.

New namespace declaration in the external resource dictionary:

xmlns:ef="clr-namespace:Microsoft.Expression.Controls;assembly=Microsoft.Expression.Drawing"

I found where the exact namespace was of the Callout class by using the Object Browser.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜