开发者

XAML Parser cannot find resource within dynamically loaded XAP when creating form instance

开发者 https://www.devze.com 2022-12-29 17:51 出处:网络
I\'ve followed Tim Heuer\'s video for dynamically loading other XAP\'s (into a \'master\' Silverlight application), as well as some other links to tweak the loading of resources and am stuck on the pa

I've followed Tim Heuer's video for dynamically loading other XAP's (into a 'master' Silverlight application), as well as some other links to tweak the loading of resources and am stuck on the particular issue of loading style resources from within the dynamically loaded XAP (i.e. the contents of Assets\Styles.xaml). When I run the master/hosting applcation, it successfully streams the dynamic XAP and I can read the deployment info etc. and load the assembly parts. However, when I actuall try to create an instance of a form from the Dynamic XAP, it fails with

Cannot find a Resource with the Name/Key LayoutRootGridStyle

which is in it's Assets\Styles.xaml file (it works if I run it directly so I know it's OK). For some reason these don't show up as application resources - not sure if I've totally got the wrong end of the stick, or am just missing something? Code snippet below (apologies it's a bit messy - just trying to get it working first) ...

'' # Here's the code that reads the dynamic XAP from the web server ...
'' #...
wCli = New WebClient
AddHandler wCli.OpenReadCompleted, AddressOf OpenXAPCompleted
wCli.OpenReadAsync(New Uri("MyTest.xap", UriKind.Relative))
'' #...

'' #Here's the sub that's called when openread is completed 
'' #...
Private Sub OpenXAPCompleted(ByVal sender As Object, ByVal e As      System.Net.OpenReadCompletedEventArgs)
Dim sManifest As String = New StreamReader(Application.GetResourceStream(New     StreamResourceInfo(e.Result, Nothing), New Uri("AppManifest.xaml", UriKind.Relative)).Stream).ReadToEnd

Dim deploymentRoot As XElement = XDocument.Parse(sManifest).Root
Dim deploymentParts As List(Of XElement) = _
        (From assemblyParts In deploymentRoot.Elements().Elements() Select assemblyParts).ToList()

Dim oAssembly As Assembly = Nothing
For Each xElement As XElement In deploymentParts
    Dim asmPart As AssemblyPart = New AssemblyPart()
    Dim source As String = xElement.Attribute("Source").Value
    Dim sInfo As StreamResourceInfo = Application.GetResourceStream(New StreamResourceInfo(e.Result, "application/binary"), New Uri(source, UriKind.Relative))
    If source = "MyTest.dll" Then
        oAssembly = asmPart.Load(sInfo.Stream)
    Else
        asmPart.Load(sInfo.Stream)
    End If
Next

Dim t As Type() = oAssembly.GetTypes()

Dim AppClass = (From parts In t Where parts.FullName.EndsWith(".App") Select parts).SingleOrDefault()
Dim mykeys As Array

If Not AppClass Is Nothing Then
    Dim a As Application = DirectCast(oAssembly.CreateInstance(AppClass.FullName), Application)

    For Each strKey As String In a.Resources.Keys
        If Not Application.Current.Resources.Contains(strKey) Then
            Application.Current.Resources.Add(strKey, a.Resources(strKey))
        End If
    Next
End If

Dim objectType As Type = oAssembly.GetType("MyTest.MainPage")
Dim ouiel = Activator.CreateInstance(objectType)
Dim myData As UIElement = DirectCast(ouiel, UIElement)
Me.splMain.Children.Add(myData)
Me.splMain.UpdateLayout()
End Sub
'' #...

'' # And here's the line that fails with "Cannot find a Resource with the Name/Key      LayoutRootGridStyle"
'' #  ...
System.Windows.Application.LoadComponent(Me, New     System.Uri("/MyTest;component/MainPage.xaml", System.UriKind.Relative))
'' #...

Just to re-cap, there are 3 scenarios to consider ... 1) The dynamically loaded XAP's style resources are left in a merged resource dictionary (in a seperate xaml file), referenced from the app.xaml of the dynamically loaded silverlight app (XAP) - When running the master application the resources from the dynamic XAP do not appear to be present under the current application (after loading the XAP assembly parts). Error occurs.

2) The dynamically loaded XAP's style resources are moved from the merged resource dictionary (from a seperate xaml file), into the app.xaml of the dynamic application, in place of the merged resource dictionary reference. - When running the master application the resources from the dynamic XAP DO appear to be present under the current application (after loading the XAP assembly开发者_开发问答 parts). However, the error still occurs.

3) The dynamically loaded XAP's style resources are copied into the app.xaml of the calling/master application (not desirable). - Error no longer occurs.


Answer provided by bykinag on silverlight forums ...

I added the following line after loading the assembly.

App.Current.Resources.MergedDictionaries.Add(New ResourceDictionary() With {.Source = New Uri("/MyTest;component/Assets/Styles.xaml", UriKind.RelativeOrAbsolute)})

I now have an issue where the dynamic application can't seem to see other pages within it (Page not found) but I'll probably raise that separately if I can't resolve it.

0

精彩评论

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