在Silverlight上动态加载图像

时间:2010-03-27 02:13:19

标签: silverlight silverlight-3.0 c#-3.0 asp.net-3.5

我有一个Silverlight应用程序,必须动态加载图像,具体取决于图像名称。我现在采取的方法是通过查询字符串将图像名称传递给页面,并将其作为参数传递给Silverlight对象标记

这是传递的查询字符串

Response.Redirect("Build.aspx?img=" + this.PictureUploader.PostedFile.FileName;

我尝试将它传递给Silverlight:

<object id="SilverlightObject" data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">
      <param name="source" value="Silverlight/iMapsSL.xap"/>
      <param name="onError" value="onSilverlightError" />
      <param name="background" value="white" />
      <param name="minRuntimeVersion" value="3.0.40624.0" />
      <param name="autoUpgrade" value="true" />
      <param name="image" value="<%# Request.QueryString["img"] %>" />
      <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>
最后一个param标签中的

,其中name = image value = Requerst.QueryString

我像这样在

中捕获Silverlight应用程序中的图像
private void Application_Startup(object sender, StartupEventArgs e)
    {
        string pictureName = "";

        if (e.InitParams != null && e.InitParams.Count > 0)
        {
            pictureName = e.InitParams["image"];

            this.RootVisual = new MainPage(pictureName);
        }

        else
        {
            this.RootVisual = new MainPage();
        }
    }

当MainPage启动时,我像这样设置了Image控件的图像源

this.Image.Source = new BitmapImage(new Uri(pictureName, UriKind.RelativeOrAbsolute));

但Silverlight没有图像加载,有人帮忙吗?

1 个答案:

答案 0 :(得分:1)

您不能通过发明像“image”这样的参数名称来设置任意值。通过指定“initparams”参数创建InitParams。 “initparams”值是逗号分隔的名称=值对集合。因此,您的代码应如下所示: -

<object id="SilverlightObject" data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">
      <param name="source" value="Silverlight/iMapsSL.xap"/>
      <param name="onError" value="onSilverlightError" />
      <param name="background" value="white" />
      <param name="minRuntimeVersion" value="3.0.40624.0" />
      <param name="autoUpgrade" value="true" />
      <param name="initparams" value="image=<%# Request.QueryString["img"] %>" />
      <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>