XAML Parser cannot find resource in dynamically loaded XAP when instantiating form

I watched a Tim Heuer movie to dynamically load another XAP (into the Silverlight Silverlight application), as well as some other links to configure resource loading, and I had a specific problem with loading style resources from within dynamically loaded XAP (i.e. the contents of Assets \ Styles.xaml). When I start the master hosting, it successfully transfers dynamic XAP, and I can read the deployment information, etc. And load the components of the assembly. However, when I try to instantiate a form from Dynamic XAP, it fails with

Cannot find resource with name / key LayoutRootGridStyle

which is in it Assets \ Styles.xaml file (it works if I run it directly so that I know it in order). For some reason, they do not appear as application resources - are not sure if I completely got the wrong end of the stick or just missed something? Below is a snippet of code (I apologize that it is a little messy - just trying to get it to work in the first place) ...

'' # Here 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 the sub that 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 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 overlap, there are 3 scenarios to consider ... 1) Dynamically loaded XAP style resources remain in the combined resource dictionary (in a separate xaml file), which is referenced by the app.xaml application of the dynamically loaded Silverlight (XAP) application. When the main application starts, resources from dynamic XAP are apparently not present in the current application (after loading the XAP assembly components). An error has occurred.

2) Dynamically loaded XAP style resources are transferred from the integrated resource dictionary (from a separate xaml file) to the app.xaml of the dynamic application instead of a link to the integrated resource directory. - When starting the main application, resources from the dynamic XAP DO are apparently present in the current application (after loading parts of the XAP assembly). However, the error is still happening.

3) Dynamically loaded XAP-style resources are copied to the call / master application app.xaml (undesirable). - The error no longer occurs.

+4
source share
1 answer

Answer provided by bykinag on silverlight forums ...

After loading the assembly, I added the following line.

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

I now have a problem when a dynamic application cannot see other pages inside it (page not found), but I will probably cut it separately if I can’t solve it.

+3
source

Source: https://habr.com/ru/post/1309407/


All Articles