Microsoft Developer Network > Forums Home > Windows Live Developer Forums Forums > Bing Maps: Map Control Development > Converting asp:Silverlight tag to object tag for Silverlight 3
Ask a questionAsk a question
 

QuestionConverting asp:Silverlight tag to object tag for Silverlight 3

  • Wednesday, September 30, 2009 6:53 AMyoonka Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    Greetings, 

    I found this quite painful but was able to piece together a solution. If you want to pass a clientToken from a silverlight 3 object instead of a silverlight 2 asp control here is the default.aspx and default.aspx.cs code: 

    default.aspx

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="dashboard.Web.Default1"%>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head id="Head1" runat="server">
        <title>Title</title>
        <style type="text/css">
        html, body {
    	    height: 100%;
    	    overflow: auto;
        }
        body {
    	    padding: 0;
    	    margin: 0;
        }
        #silverlightControlHost {
    	    height: 100%;
    	    width: 100%;
    	    text-align:center;
        }
        </style>
        <script type="text/javascript" src="Silverlight.js"></script>
        <script type="text/javascript">
            function onSilverlightError(sender, args) {
                var appSource = "";
                if (sender != null && sender != 0) {
                  appSource = sender.getHost().Source;
                }
                
                var errorType = args.ErrorType;
                var iErrorCode = args.ErrorCode;
    
                if (errorType == "ImageError" || errorType == "MediaError") {
                  return;
                }
    
                var errMsg = "Unhandled Error in Silverlight Application " +  appSource + "\n" ;
    
                errMsg += "Code: "+ iErrorCode + "    \n";
                errMsg += "Category: " + errorType + "       \n";
                errMsg += "Message: " + args.ErrorMessage + "     \n";
    
                if (errorType == "ParserError") {
                    errMsg += "File: " + args.xamlFile + "     \n";
                    errMsg += "Line: " + args.lineNumber + "     \n";
                    errMsg += "Position: " + args.charPosition + "     \n";
                }
                else if (errorType == "RuntimeError") {           
                    if (args.lineNumber != 0) {
                        errMsg += "Line: " + args.lineNumber + "     \n";
                        errMsg += "Position: " +  args.charPosition + "     \n";
                    }
                    errMsg += "MethodName: " + args.methodName + "     \n";
                }
    
                throw new Error(errMsg);
            }
        </script>
    </head>
    <body style="height:100%; width:100%; background-color:#6a6a6a; margin:0;">
        <form id="form1" runat="server" style="height:100%; width:100%; margin:0,0,0,0;">
        <div id="silverlightControlHost" style="height:100%; width:100%;">
            <object id="Xaml1" data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">
    		  <param name="InitParams" value="<%=InitParams %>" />
    		  <param name="source" value="ClientBin/YourSilverlight.xap"/>
    		  <param name="onError" value="onSilverlightError" />
    		  <param name="background" value="white" />
    		  <param name="minRuntimeVersion" value="3.0.40624.0" />
    		  <param name="autoUpgrade" value="true" />
    		  <a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40624.0" style="text-decoration:none">
     			  <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style:none"/>
    		  </a>
    	    </object><iframe id="_sl_historyFrame" style="visibility:hidden;height:0px;width:0px;border:0px"></iframe>
    	    </div>
        </form>
    </body>
    </html>
    
    default.aspx.cs

    using System;
    using System.Net;
    using System.Web.UI;
    using dashboard.Web.TokenWebReference;
    
    namespace dashboard.Web
    {
        public partial class Default1 : Page
        {
            protected string InitParams { get; private set; }
            protected void Page_Load(object sender, EventArgs e)
            {
              if (Page.IsPostBack) return;
                
                // Initialize the service variable which makes the request 
                var commonService = new CommonService();
                commonService.Url = "https://staging.common.virtualearth.net/find-30/common.asmx";
                commonService.Credentials = new NetworkCredential("138610", "!T4n4gr4m!");
    
                // Create the TokenSpecification object to pass to GetClientToken.
                var tokenSpec = new TokenSpecification();
    
                // Use the ‘Page’ object to retrieve the end-client’s IPAddress.
                tokenSpec.ClientIPAddress = Page.Request.UserHostAddress;
    
                // Set the expiration time of the token.
                // The maximum allowable token duration is 480 minutes (8 hours).
                // The minimum allowable duration is 15 minutes.
                tokenSpec.TokenValidityDurationMinutes = 480;
    
                // Make the GetClientToken request to retrieve a token from the Virtual Earth Token Service. 
                // Pass this token to the Silverlight application.
    
                InitParams = string.Format("clientToken={0}", commonService.GetClientToken(tokenSpec));
               // InitParams = string.Format("Param1={0},Param2={1}", "param1", "param2");
               // myToken.Text = String.Format(commonService.GetClientToken(tokenSpec));
                //Xaml1.InitParameters = "clientToken=" + commonService.GetClientToken(tokenSpec);
                
            }
        }
    }
    

All Replies

  • Sunday, October 11, 2009 4:48 PMJulesW Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I tried this to get hold of the token - but still having problems getting a succesful token into my silverlight Page

    In my App.Xaml.Cs  under Application_Startup method  I now have

    this.RootVisual = new Page(e.InitParams["clientToken"]);

    And then within Page.xaml.cs  I have the folliwing in my Page constructor

    public Page(string Token)
    TheClientToken = Token
    .....

    RouteService.Credentials MyCredentials = new RouteService.Credentials();
    MyCredentials.Token = TheClientToken;

    Under debug a long Token String is being passed through, but the resulsts of my CalculateRouteAsync call always returns "Credentials are either Invalid or Unspecified" errors in my Silverlight page. 
    - I have checked that I am calling the corrrect staging version of the Route Service
    - I have checked my Credentials when calling Route Servcie from within aspx server side controls, which resturns results OK.

    So it iss soimthing to do with corrupting my Token , in the transfer from Default.aspx to the Silverlight Control.  Even though thr Token 'looks OK' within Debug.

    Any suggestions ?
    Cheers

    Julian

    Newbie Indigo and SOA fanatic
  • Tuesday, November 03, 2009 2:37 AMRoyClem Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Julian,

    If you resolved your problem, would you share the solution?

    Thanks.