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

Tagged:
Freelance Jobs

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.


TomH
2010-05-11 10:54:31 Scores:4
Incidentally if I move the style resources directly into the app.xaml of the dynamically loaded XAP (and remove the merged dictionaries reference to the file they were in) then they all suddenly appear to be present in the calling apps resources list. However, even though I can now see them I still get the "Cannot find a Resource with the Name/Key LayoutRootGridStyle" error from the xaml parser (and yes the dictionary key LayoutRootGridStyle is definately present in the current applications resources).
TomH
2010-05-11 15:09:30 Scores:0
Interestingly it seems to work if I copy the contents of the merged resource dictionary from the dynamic XAP file directly into the resources dictionary of the calling/master App.xaml. But obviously I don't want to do that because I want to be able to pick up the needed resources from the dynamic XAML - rather than 'hard coding' them in the master application.
TomH
2010-05-11 16:25:27 Scores:0

1 answer

Answer 1
Scores:3

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.

Tom H
2010-05-12 09:57:32
Share |
View original post at stackoverflow.com

Related topics

Where can I find information about creating 3D controls in WPF?

I am about to start to make the interface for a game in WPF / Silverlight. I'd like it to have a nice look all in 3 dimensions. Can you point me to some good resources, tutorials, books... on how to develop real 3D controls, like Buttons and such? ...

How to Reset Silverlight View in ViewModel Without Creating New Instance

In my application, I instantiate a new instance of my view in its associated viewmodel's constructor. I also have some events to subscribe to with an event aggregator. public class FooViewModel { private FooView TheView { get; set; } private IEventAggregator Aggregator { get; set; } ...

Silverlight cannot find resource in the same dictionary

I have the following style defined in the ResourceDictionary of a Silverlight 4.0 application <Style x:Key="GridSplitterStyle" TargetType="sdk:GridSplitter"> <Setter Property="HorizontalAlignment" Value="Stretch" /> <Setter Property="VerticalAlignment" Value="Stretch" /> <Setter Property="Background" Value="Transparent" /> <Setter Property="Template" Value="{StaticResource GridSplitterTemplate}" /> </Style> <ControlTemplate x:Key="GridSplitterTemplate" TargetType="sdk:GridSplitter"> ...

In Silverlight, when loading XAPs dynamically, can only only the first loaded define an App.xaml?

I have four Silverlight 4 apps, for which I'm trying to build a "shell" to host them in, dynamically loading the Xaps on demand, and giving the user the impression they are a single application. Each application currently has its own App.xaml, which defines a System.Windows.Application. My question is - ...

What is the convention for dynamically creating an object instance?

I'm creating a page that will allow you to put multiple widgets on it, and some widgets can be duplicated on the page. So I need to understand the proper convention for creating an object instance on the fly. // my bulb object var bulb = { state: ...

receiving undeclared prefix when dynamically creating a xaml object

Hell all, I am currently trying to create a few object dynamically in code. This has been successful up to the point to where I want to identify the element for example Here I create the object ( an ellipse with a drop shadow) public MainPage() { ...

Resource files not loaded properly in the silverlight client

I have a SL 4.0 application hosted on IIS with a possibility to change GUI language. The problem is that hosted on one machine , language functionality works and on the other machine not. Inside this piece of code: Thread.CurrentThread.CurrentCulture = new CultureInfo(message.NewCulture); ...

Can't debug dynamically loaded silverlight applications (xap file)

I have a solution which contains multiple applications. In the 'root' application I read a config file, download the xap files and load them using some class System.ComponentModel.Composition.Hosting.DeploymentCatalog . The problem is that I can't debug these dynamically loaded applications (the web project, I can debug the Silverlight client). Visual ...

I'd like to crate a form dynamically instead of XAML

I would like to create a form dynamically instead of using the XAML to do it. The form reside into a tab control. Bellow is XAML code: this is a XAML sample. <TabControl> <TabItem Header="SVRS Data" Name="tab_SVRS_Data2"> <Grid> <Grid.ColumnDefinitions> ...

Dynamically creating an image and retrieving dimensions with jQuery

So I am dynamically creating an image and I wish to get the image dimensions. Is it possible to do this before the image is attached to the DOM? At the moment I am doing this: var arrow = $('<img src="' + parameters.arrowRight + '" />'); arrow.load(function() { ...