tags:

views:

35

answers:

2

I am trying to build a library of silverlight controls where the client can choose which control they want to use. The Silverlight Application project will have several user controls. The structure is something like:

Project
-App.xaml
-MainPage.xaml
--Controls (Folder)
------ControlA.xaml
------ContorlB.xaml

How can i reference ControlA or ControlB from my HTML pages? It seems like its only possible to reference xap assemblies so do i need to create a Silverlight application for each usercontrol? Seems like overkill?

If i wanted to use ControlA from the library so i want to be able to do something like:

<object>
    <param name="source" value="ClientBin/Silverlight.xap"/>
    <param name="class" value="ControlA"/>
</object>

I know the above is not valid SL markup but i think you can understand what I'm trying to do?

+1  A: 

Your Silverlight controls are not exposed to the HTML. The Silverlight app itself is merely an <object> in HTML markup (simplified story, of course); the controls are only available inside the Silverlight application.

There are ways of communicating between your Silverlight app and the web markup, e.g. you could find a way to have the Silverlight app change which control it's rendering.

Jimmy
i know, but how can i specify which xaml i want to render using the <object > tag. Seems like i can only point to the xap file and not the controls within.
GMan
Probably the simplest way is to use the InitParams (http://msdn.microsoft.com/en-us/library/cc838255%28VS.95%29.aspx) parameter on the <object> tag. Alternatively, for more interactive use, you could use the HTML Bridge (http://msdn.microsoft.com/en-us/library/cc645076%28VS.95%29.aspx) to interact between your markup page and your Silverlight app via Javascript. Either way, you'll have to have your Silverlight app smart enough to handle the input and react appropriately.
Jimmy
A: 

You could have a controller XAML file which reads the InitParams.

Pass the value in HTML to Silverlight:

<object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">
      <param name="source" value="ClientBin/SilverlightApplication1.xap"/>
      <param name="onError" value="onSilverlightError" />
      <param name="initparams" value="control=ControlA" />
      ...

And in your Application_Startup event read out your value:

private void Application_Startup(object sender, StartupEventArgs e)
{
    var initParams = e.InitParams;

    if (initParams.Keys.Contains("control"))
    {
        if (initParams["control"] == "ControlA")
        { 
            // Render control A
            // this.RootVisual = new ControlA();
        } else  if (initParams["control"] == "ControlB")
        { 
            // Render control B
            // this.RootVisual = new ControlB();
        }
    }

    // Default page.    
    this.RootVisual = new MainPage();
}
Peter Kiers
Use of initparams is good but a series of `else if` would be unwieldy for many controls. See this answer for a more dynamic solution: http://stackoverflow.com/questions/2338016/silverlight-how-to-create-a-page-dynamically/2338131#2338131
AnthonyWJones