Skip to main content

Microsoft Silverlight

Answered Question How to use multiple Silverlight applications in one .xap fileRSS Feed

(1)

robhouweling
robhouwe...

Contributor

Contributor

3174 points

548 Posts

Silverlight MVP

How to use multiple Silverlight applications in one .xap file

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



My blog

jackbond
jackbond

Contributor

Contributor

2820 points

725 Posts

Re: How to use multiple Silverlight applications in one .xap file

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:

private void Application_Startup(object sender, StartupEventArgs e)

{

this.RootVisual = new Lobby();

}

 

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
frefaln

Member

Member

153 points

74 Posts

Re: How to use multiple Silverlight applications in one .xap file

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.

----------------------------------------
Darren Mart
Developer of Steel Saga, which amounted to little more than some free advertising for Silverlight. Glad to help.

robhouweling
robhouwe...

Contributor

Contributor

3174 points

548 Posts

Silverlight MVP
Answered Question

Re: How to use multiple Silverlight applications in one .xap file

@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.

@frefaln: This should be the solution.:

In your App.xaml, use

this.RootVisual = new Page(e.InitParams);

instead of:

this
.RootVisual = new Page();


In your Page.xaml:
Create an extra constructor:

public Page(IDictionary<string, string> InitParams)

{

_initArguments = InitParams;

Init();

}


Now you can access the initParams by using this syntax:
_initParams["Label"]

Hope this helps.

Rob.

(If this has answered your question, please click on mark as answer on this post)

Cheers!
Rob Houweling



My blog

jackbond
jackbond

Contributor

Contributor

2820 points

725 Posts

Answered Question

Re: How to use multiple Silverlight applications in one .xap file

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.

robhouweling
robhouwe...

Contributor

Contributor

3174 points

548 Posts

Silverlight MVP

Re: How to use multiple Silverlight applications in one .xap file

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" />

(If this has answered your question, please click on mark as answer on this post)

Cheers!
Rob Houweling



My blog

jackbond
jackbond

Contributor

Contributor

2820 points

725 Posts

Re: How to use multiple Silverlight applications in one .xap file

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.

frefaln
frefaln

Member

Member

153 points

74 Posts

Re: How to use multiple Silverlight applications in one .xap file

You guys rock! I don't know why I didn't bother trying key=value, I guess I should follow my instincts Smile . 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. 

----------------------------------------
Darren Mart
Developer of Steel Saga, which amounted to little more than some free advertising for Silverlight. Glad to help.

Timmy G
Timmy G

Member

Member

66 points

52 Posts

Re: How to use multiple Silverlight applications in one .xap file

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 = New SilverlightControl1

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
CraigN

Member

Member

352 points

89 Posts

Re: How to use multiple Silverlight applications in one .xap file

 I've posted a description and a solution to this problem on my blog over here.

Microsoft Xbox MVP

apjrajesh
apjrajesh

Member

Member

6 points

3 Posts

Re: How to use multiple Silverlight applications in one .xap file

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.........

Thanks & Regards,
Rajesh Tanwar

mchlsync
mchlsync

Star

Star

14606 points

2,730 Posts

Silverlight MVP

Re: How to use multiple Silverlight applications in one .xap file

Please take a look this post.

http://www.flawlesscode.com/post/2008/03/Silverlight-2-Navigating-Between-Xaml-Pages.aspx 

(If this has answered your question, please click on "Mark as Answer" on this post. Thank you!)

Regards,
Michael Sync
Silverlight MVP

Blog : http://michaelsync.net


apjrajesh
apjrajesh

Member

Member

6 points

3 Posts

Re: How to use multiple Silverlight applications in one .xap file

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?

Thanks & Regards,
Rajesh Tanwar

mchlSync
mchlSync

Star

Star

14606 points

2,730 Posts

Silverlight MVP

Re: How to use multiple Silverlight applications in one .xap file

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.

 

(If this has answered your question, please click on "Mark as Answer" on this post. Thank you!)

Regards,
Michael Sync
Silverlight MVP

Blog : http://michaelsync.net


apjrajesh
apjrajesh

Member

Member

6 points

3 Posts

Re: Re: How to use multiple Silverlight applications in one .xap file

Thanks a lot Michael !!!Smile

Your answer has solved my problem and has also given better understanding of Silverlight.

Thanks again...........

Thanks & Regards,
Rajesh Tanwar

HarjinderSilverLight
Harjinde...

Member

Member

12 points

6 Posts

Re: How to use multiple Silverlight applications in one .xap file

 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
colijunior

Member

Member

2 points

1 Posts

Re: How to use multiple Silverlight applications in one .xap file

http://craign.net/2008/03/11/how-to-switch-silverlight-usercontrols/

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities