Is there a way to embed Silverlight XAML in my page using ASP.NET code?

So, I want to create some Silverlight plots from some data in my ASP.NET webpage. The way to do this is using Visifire (my current thing) or the upcoming Silverlight Drawing , it seems, to create some XAML or at least some Silverlight objects ( like in this Visifire example ) and then somehow attach them to page.

Strange, the only way to do this seems to be through JavaScript! That is, I would have to do something like this:

User β†’ click button β†’ JavaScript event handler β†’ AJAX call to get XAML using the server method β†’ ​​enter the XAML string in the client HTML (for example, is this generally or like this for a specific Visifire method ).

This is very controversial! Most likely, this is an opportunity to do something similar inside MyPage.aspx.cs :

 protected void MyButton_Click(object sender, EventArgs e) { this.MyFictionalSilverlightControl.Xaml = this.GenerateXamlFromData(); } 

That would be much better! I am happy to keep MyFictionalSilverlightControl inside the UpdatePanel , although it is inefficient; I just wanted to know how to make such a fictional Silverlight control.

Any ideas? Or suggestions on how to do it better?

0
ajax silverlight xaml
source share
4 answers

I would suggest a simpler AJAXy approach: ask JS to request XAML, and then runtime paste XAML into the control (see HTML bridge) after XAML has been returned from the server. Thus, your control could make a nice animation / transition to a new screen.

However, you can do what you are aiming for:

  • Cancel server management of Silverlight in the update panel.
  • Write an aspx page to create XAML (modify DocType to make the task easier)
  • Specify the Silverlight server element on the .ASPX page:

    ... asp: Silverlight ID = "Xaml1" Runat = "server" Source = "~ / ClientBin / myXAMLPage.aspx" MinimumVersion = "2.0.30523" Width = "100%" Height = "100%">

  • Run UpdatePanel to do this when the user clicks a button.

Now, whenever the user clicks the button, the update panel (containing the Silverlight control) will be displayed, and this SL control will query the XAML file and display the one found.

Note that this approach creates a control using a XAML file. This means that you cannot use managed code in Silverlight. If you need managed code, instead of specifying the "myXAMLPage.aspx" page in the source, specify your .xap file, and then use initparam to specify "myXAMLPage.aspx". Inside the Silverlight application, load the xaml file (WebClient or something else) and then use XamlReader.Load to load it and display it.

NTN, Erik

+2
source share

I suggest you download ASP.NET Silverlight Server Management. More information about this control can be found here. In general, you put the compiled XAM file in the BIN directory, and then bind XAML to asp: Silverlight control. He takes care of everything else. In addition, you can pass some parameters to Silverlight using the asp: Silverlight collection of input parameters. This is a cumbersome approach, but it works.

0
source share

I could be wrong: this question was seen as a WPF developer, not a SilverLight developer, but in WPF you can create and embed controls in XAML in code.

 Label myLabel = new Label(); // Create a new control myLabel.Text = "Hello Wirral"; // Change some of its properties MyContainingPanel.Children.Add(myLabel); // Add it to an existing container 
0
source share

A very simple solution for this is to simply make the table / data view in silverlight. Then use the web service to provide data to the silverlight application.

I am sure that MS will approve it completely.

Otherwise (and you asked him;):

The problem with the ideal solution that you pointed out is that the ASP.Net abstraction hides the reality of double publishing data, and also creates a client interface on the server, when a Silverlight client-side application can do it, it feels great:

Step 1 - Sending html to the client with embedded data

Step 2 - Initializing the Client-Side Application (Silverlight Application)

Step 3 - Click the GenerateXamlFromData Call Button on the Server

Step 4 - Get the same data as in step 1, and then create Xaml from it

Step 5 - Transfer It Back To The Browser

Step 6 - calling a method in a Silverlight application to install Xaml

Steps 3-5 are completely redundant. On the client, you already have data and something that you can create diagrams on the fly, the silverlight application.

If the table had an absolute requirement that the table be in html, then I would decide:

Make your data non-html. Use XML / XSLTransform, JSON / javascript / JQuery or something like your favorite client-side transformation to display it. Web Services, Anything. The main thing here is that you open your web page, an http message back to the data and create your html table on the fly. Then transfer the same data to the silverlight application and create a chart.

Silverlight can dynamically build a chart with a simple object creation. If you really like to poke yourself in the eyes with a pencil, you can still dynamically build Xaml lines and use them to create chart objects, and an easy way to earn the StackOverflow Xaml-Nazi icon

Another solution to your problem, if you really wanted to, you could just pass the Sivlerlight App html table dom and extract data from it. Generally speaking, if you haven’t done something really stupid XML-compliant html tables, just load them into an XDocument.

As for the simple way that you describe, afayk this is not possible. Maybe someday it will be, but to be honest, it will never be a good solution, it will just be a solution for people who want to hack fast and dirty applications.

0
source share

All Articles