1
votes

I'm having a lot of troubles in making Silverlight recognize the namespases of Silverlight toolkit maybe because I am writing XAML code in ASP.Net User Control (*.ascx) file.

I've Googled and searched here (Stacked? maybe) but nothing worked for me.

I am sure this can be done somehow, I've seen it working before.

After we write the XAML contents in the (.ascx) file we send these contents to an (.xap) silverlight executable file which loads the XAML contents via XamlReader in run-time and executes that.

this is the head part of the ASCX file:

<%@ Control Language="C#" AutoEventWireup="false" CodeBehind="TargetChartByVisits2.ascx.cs" Inherits="Mynamespace.TargetChartByVisits2" %>
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit"
xmlns:datavis="clr-namespace:System.Windows.Controls.DataVisualization;assembly=System.Windows.Controls.DataVisualization.Toolkit"
xmlns:charting="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"
xmlns:chartingprimitives="clr-namespace:System.Windows.Controls.DataVisualization.Charting.Primitives;assembly=System.Windows.Controls.DataVisualization.Toolkit"
xmlns:data="clr-namespace:MyNamespace.MyCharts;assembly=MyNamespace.MyCharts"
Width="350" Height="400">

What I am trying to do is to draw a chart in Silverlight Toolkit, I have this XAML code to assist data-binding:

<controls:ObjectCollection x:Key="SeriesData">
    <asp:Repeater ID="Repeater1" DataSource="<%# vwMain %>" runat="server">
        <ItemTemplate>
            <data:ChartDataElement MeasurementDate="<%# HttpUtility.HtmlEncode(Sql.ToString(Eval("MeasurementDate"))) %>" VisitReading="<%# HttpUtility.HtmlEncode(Sql.ToString(Eval("VisitAmount"))) %>" VisitTarget="<%# HttpUtility.HtmlEncode(Sql.ToString(Eval("Visit_TargetAmount"))) %>" />
        </ItemTemplate>
    </asp:Repeater>
</controls:ObjectCollection>

I get an error that clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit" is an unknown namespace, even though, I have the toolkit installed and the samples are working well and I've added the reference to this assembly (Controls) from

C:\Program Files (x86)\Microsoft SDKs\Silverlight\v4.0\Toolkit\Apr10\Bin

I'm using VS2010, thanks.

3

3 Answers

3
votes

I suspect you'll be getting this error because your XAP file does not include the toolkit's DataVisualization dll. In the project that you compile to generate the XAP you will need to add references to all dlls that the subsequently downloaded Xaml may reference.

Even if Xaml and Code found in the Xap project do not use these dlls in order for a XamlReader that is used to parse downloaded Xaml to work all the dlls references in that downloaded Xaml needs to be present in the Xap project.

There are some alternatives but they are seriously more complex so if you can anticipate the expected referenced dlls that is much better.

It appears the you are under the impression that a silverlight application should be able access whatever silverlight SDKs have been installed on the client machine. This is not the case, only the compiling MSBuild or Visual Studio is interested is and uses what has been installed on the machine. Running silverlight applications can only make use of what has been included in the Xap or what it can download, it can't use dlls installed on the client.

1
votes

It doesn't matter if you are using Silverlight in an aspx, ascx or html page, the rules are the same. Silverlight is a browser plugin and so the content must be embedded with a HTML object tag:

<object width="300" height="300"
    data="data:application/x-silverlight-2," 
    type="application/x-silverlight-2" >
    <param name="source" value="SilverlightApplication1.xap"/>
</object>

This is the type of code you need to place in your ascx page, not the XAML.

See How to: Add Silverlight to a Web Page by Using HTML for more information.

0
votes

Silverlight can be instantiated in two ways:

1) Silverlight 1 style app, using XAML and JavaScript only. This has no managed code, and no UserControl. In this case, you could embed the XAML roughly like this (I'd have to go look up this archaic syntax since it's rarely used nowadays).

2) Silverlight 2+ app, using managed code. In this case, you must use an <object> tag. So you wouldn't embed the XAML in an ASCX file directly like this - rather, you would have the XAML in a Silverlight Application or Silverlight Class Library, build a .xap file, and point to the .xap file with the <object> tag from ASCX.

Can you clarify which one you're trying to do? It seems like you want #2 (managed code app) but if you do want to do a Silverlight 1 style app, please respond with that, and I can update my answer with exactly how to do that.