Skip to main content
Home Forums Silverlight Programming Programming with .NET - General How to use multiple Silverlight applications in one .xap file
16 replies. Latest Post by colijunior on September 24, 2009.
(1)
robhouwe...
Contributor
3158 points
540 Posts
03-06-2008 3:55 PM |
Hi,
I'm having the following issue. When having multiple Silverlight applications in a single project, how can I load any of these applications instead of the default Page.xaml?
- When using the "html" <object> method: it expects a parameter with a xap file, xaml files are not allowed.- asp:Silverlight control also needs a xap file. asp:xaml doesn't work.
Out of options for now. Anyone?
Kind regards,
Rob Houweling
(If this has answered your question, please click on mark as answer on this post)Cheers!Rob Houweling
jackbond
2806 points
722 Posts
03-06-2008 5:41 PM |
I'm dealing with trying to get my 1.1 app converted and am working with this right now. I think(and I could be completely off here) this is what you need to do:
A 2.0 needs to have at least 1 application class defined within it. If you bring up the properties for your Silverlight App you will see a drop down list titled "Startup object." I think this list is populated with the classes in your app that derive from Application. If you go to that class(I think the silverlight project template autogenerates 1 for you) you can set the root visual. I'm just figuring this out now, so this is what my code looks like:
{
}
This is a more complete version taken from the docs:
private void App_Startup(object sender, StartupEventArgs e) { // Check to see if user provided the /StartPage initialization parameter. // If they did, and the value is "DefaultStartPage", set the root visual // with an instance of the DefaultStartPage class. If they did, and the // value is "NonDefaultStartPage", set the root visual with an instance // of the NonDefaultStartPage class. If they did, but didn't provide a // value, throw an exception. Otherwise, set root visual with an instance // of the DefaultStartPage. string startPageParameter = "/StartPage"; if( !e.InitParams.ContainsKey(startPageParameter) ) { this.RootVisual = new DefaultStartPage(); } else { switch (e.InitParams[startPageParameter] ) { case "DefaultStartPage": this.RootVisual = new DefaultStartPage(); break; case "NonDefaultStartPage": this.RootVisual = new NonDefaultStartPage(); break; default: throw new Exception("/StartPage must be 'DefaultStartPage' or 'NonDefaultStartPage'."); } } }
Hope this helps and I have not sent you on the wrong path.
frefaln
Member
153 points
74 Posts
03-07-2008 2:19 AM |
I'm stuck on the same issue and I'd love to know why this isn't documented somewhere (or at least a place where the average Joe can find it).
App.xaml does indeed accept StartupEventArgs, but its InitParameters property is Dictionary whereas the InitParameters property on an asp:Silverlight control is a String. I'd love to know how I'm supposed to get those InitParameters across -- once that happens I can do exactly what JackBond said and serve up a different RootVisual accordingly.
If this isn't possible, then I suppose each unique control has to go in its own .xap? That seems like a huge waste if that's the case.
03-07-2008 4:10 AM |
@jackbond: Thanks for the example. I figured this one out myself, but I'm not really happy with the solution, so I decided to make separate XAP files for my solution.
this
instead of:this.RootVisual = new Page();
In your Page.xaml:Create an extra constructor:
_initArguments = InitParams;
Init();
Now you can access the initParams by using this syntax:_initParams["Label"]Hope this helps.Rob.
03-07-2008 4:46 AM |
I think the intended idea is to be able to have multiple "root" controls in the same xap, and then load the appropriate one based on a setting in the params. I tried messing around with initParams setting property, but couldn't figure out the correct syntax to get e.InitParams properly populated. Anyone have a sample? You really shouldn't need separate xap files.
03-07-2008 5:17 AM |
In and <asp:Silverlight> control:InitParameters="Label=Applicatiebeheer,Url=http://www.amercom.nl,Animate=true"
In an <object>:<param name="initParams" value="Label=Applicatiebeheer,Url=http://www.amercom.nl,Animate=true" />
03-07-2008 7:24 AM |
Thanks, that's the official sign it's time for bed. Don't know why I didn't try = as a delimiter, seems ridiculously obvious now. Although, if you google initParams and Silverlight, your post will be the only hit that helps. They sure do index this site fast. 4:19AM in Vegas, enough coding for today.
03-07-2008 10:40 AM |
You guys rock! I don't know why I didn't bother trying key=value, I guess I should follow my instincts . Got it working now and it's a beautiful thing.
I still think the Silverlight Gods should consider adding a standardized parameter which specifies the entry point, i.e. which UserControl in the .xap we want to render for an instance of <asp:Silverlight > control. Unless developers aggressively hunt the info like we did, it probably won't strike them as intuitive to use key=value pairs in an InitParameters property.
Timmy G
64 points
51 Posts
03-07-2008 8:49 PM |
How about loading a new usercontrol programmatically after the initial control was loaded? For example, in a button click event on my control I've tried:
Application.Current.RootVisual =
But that doesn't seem to do anything. I expected the "current" control to change. Me.Content = New SilverlightControl1 works but Me is the original control so it technically is hanging around and acting as a parent which is not what I'm after. I want to replace the original usercontrol while remaining in the same app.
Thanks! It's fun playing with the beta. Can't wait to get proficient enough to start cranking out apps!
CraigN
352 points
89 Posts
03-11-2008 4:29 PM |
I've posted a description and a solution to this problem on my blog over here.
apjrajesh
6 points
3 Posts
04-25-2008 8:50 AM |
Hi all,
I am beginner with Silverlight and have gone through various blogs, articles and e-books to know the world of silverlight.
I have following query :
"We may desire to have multiple silverlight pages in a single application. For example, consider I've two silverlight pages "Page.xaml" and "Shapes.xaml". I've a button on "page.xaml", on click of this button I want to display "Shapes.xaml" page. How to do this?
Also if I want to call a silverlight page through the click event of a button(placed on simple web page like "defaut.aspx"), how to do that?
Please help.........
mchlsync
Star
14566 points
2,730 Posts
04-25-2008 9:51 AM |
Please take a look this post.
http://www.flawlesscode.com/post/2008/03/Silverlight-2-Navigating-Between-Xaml-Pages.aspx
04-28-2008 1:37 AM |
Hi Michael,
Thanks for giving much needed link. Blog posted at this link is awesome. My problem of navigating between ".xaml" pages is solved.
But I'm still having no idea about "navigating between '.aspx' page and '.xaml' page. For example if I've made a silverlight project and got 'mysilverlight.aspx' page by default for getting the silverlight content. Obviously I can call 'mysilverlight.aspx' page to see the silverlight content. Now if I add new page 'default.aspx' to my application and can easily naviagate between 'mysilverlight.aspx' page and 'default.aspx' using Response.Redirect() method. Now consider that by default 'page.xaml' is shown in silverlight content through 'mysilverlight.aspx', but I want to show 'shape.xaml' at the click of some button on 'default.aspx' then how to do that?".
In short how to change root visual element of grid(of silverlight application) from '.aspx' pages?
mchlSync
04-28-2008 2:04 AM |
apjrajesh:But I'm still having no idea about "navigating between '.aspx' page and '.xaml' page.
XAML is not like a webpage. It's more like control. Are you familiar with ASP.NET or winform or WPF? If you are familiar with those technologies, you may understand what I meant by "control".. The control can't display on the browser alone. It has to be hosted on a web page.
apjrajesh: I want to show 'shape.xaml' at the click of some button on 'default.aspx' then how to do that?".
Like I said, xaml is not like a webpage. So, You have to host shape.xaml on webpage (let's say "popup.aspx") then, you can show this popup.aspx page that has shape.xaml contetn at the click of some buttons on "default.aspx"
apjrajesh:In short how to change root visual element of grid(of silverlight application) from '.aspx' pages?
Changing the rootvisual element from aspx page is a different thing. You can pass the parameter from aspx to Silverlight project using initParams. then, change the rootpage in App.xaml.cs. If you are not sure how to use initParams, you can search it in this forum.
04-30-2008 5:53 AM |
Thanks a lot Michael !!!
Your answer has solved my problem and has also given better understanding of Silverlight.
Thanks again...........
Harjinde...
12 points
6 Posts
09-18-2008 8:23 AM |
Hi Rob
I read your post it looks very good. Please help me if you post the complete code for this application. I'll really apperciate for this effort by your side. Because I am on learning stage and have eager to learn.
Thanks in Advance
colijunior
2 points
1 Posts
09-24-2009 5:08 PM |
http://craign.net/2008/03/11/how-to-switch-silverlight-usercontrols/