共享一个封装Silverlight的WebPart

不管是报表图表还是电子地图,将Silverlight应用程序嵌入到MOSS中,可以大大提高MOSS站点的用户体验。而合理的应用WebPart对嵌入的Silverlight进行封装则能够减少开发复杂程度。

最近参与的项目中有不少这样的案例,于是便写了一个简单的Silverlight封装WebPart,分享与此,希望能够帮助又需要的朋友。

(第一次写WebPart,请多指教)

 

考虑到的问题有:

  • xap文件缓存期限
  • 承载容器的样式
  • xap文件路径配置
  • xap文件接收的参数

没有考虑到的问题有:

  • 安全性(绝大多数MOSS应用都是intranet的,相对安全) 

代码如下:

/****************************** Module Header ******************************\
* Module Name:  SilverLightWrapper.cs
* Project:      CarolLib.MOSS.WebParts
* Author:       Lance Zhang
* Copyright (c) CarolLib Co., Ltd.
*
* Provide a way to wrap Silverlight xap with webpart,
* and output the rendered div to web page.

* Key Features:
* ==============================================================

* @ - Private different timespan unit to control the browser cache.
* @ - Can pass custom parameter(s, by split) to silverlight application.
* @ - Customize containner div's style.

* ==============================================================

* History:
* * 8/19/2009 13:53 PM Lance Zhang Created
* * 8/24/2009 21:29 PM Lance Zhang Add ContainerStyle
* * 9/09/2009 21:29 PM Lance Zhang Add CustomParams
\**************************************************************************
*/
using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Web.UI;
using Microsoft.SharePoint.WebPartPages;
using System.Web.UI.WebControls.WebParts;
using System.ComponentModel;

namespace CEAir.InfoPortal.WebParts
{
    [Guid(
"803617a2-9dc9-4eb8-b05c-3e064bc12123")]
    
public class SilverLightWrapper : System.Web.UI.WebControls.WebParts.WebPart
    {
        
#region ctor.

        
public SilverLightWrapper()
        {

        }

        
#endregion

        
#region Properties

        
private string containerStyle = "PADDING-BOTTOM: 10px; PADDING-LEFT: 10px; WIDTH: 730px; PADDING-RIGHT: 20px; BACKGROUND: url(/_LAYOUTS/SL3/img/it_task_bg.jpg) #fff no-repeat left top; FLOAT: left; HEIGHT: 600px; PADDING-TOP: 10px";
        [Personalizable]
        [WebBrowsable]
        [WebDisplayName(
"容器div样式")]
        [Category(
"个性设置")]
        
public string ContainerStyle
        {
            
get { return containerStyle; }
            
set { containerStyle = value; }
        }


        
private string customParams = string.Empty;
        [Personalizable]
        [WebBrowsable]
        [WebDisplayName(
"CustomParams")]
        [Category(
"自定义参数")]
        
public string CustomParams
        {
            
get { return customParams; }
            
set { customParams = value; }
        }


        
private string path = "/_LAYOUTS/SL3/StatusReport.xap";
        [Personalizable]
        [WebBrowsable]
        [WebDisplayName(
"xap文件路径")]
        [WebDescription(
"例如:/_LAYOUTS/SL3/StatusReport.xap")]
        [Category(
"个性设置")]
        
public string Path
        {
            
get { return path; }
            
set { path = value; }
        }


        
public string CurrentUser 
        {
            
get
            {
                
return this.Context.User.Identity.Name;
            }
        }


        
private string cacheUnit = "";
        [Personalizable]
        [WebBrowsable]
        [WebDisplayName(
"缓存时间单位")]
        [WebDescription(
"例如:分、时、天、月、年")]
        [Category(
"缓存设置")]
        
public string CacheUnit
        {
            
get { return cacheUnit; }
            
set { cacheUnit = value; }
        }

        
#endregion

        
#region Override Members

        
protected override void CreateChildControls()
        {
            
string cacheKey = GetCacheKey();

            StringBuilder sl 
= new StringBuilder();
            sl.AppendLine(
string.Format("<div id=\"silverlightControlHost\" style=\"{0}\">", ContainerStyle));
            sl.AppendLine(
"<object data=\"data:application/x-silverlight-2,\" type=\"application/x-silverlight-2\" width=\"100%\" height=\"100%\">");
            sl.AppendLine(
string.Format("<param name=\"source\" value=\"" + Path + "?version={0}\"/>", cacheKey));
            sl.AppendLine(
"<param name=\"onError\" value=\"onSilverlightError\" />");
            sl.AppendLine(
"<param name=\"background\" value=\"white\" />");
            sl.AppendLine(
"<param name=\"minRuntimeVersion\" value=\"3.0.40818.0\" />");
            sl.AppendLine(
"<param name=\"autoUpgrade\" value=\"true\" />");
            sl.AppendLine(
string.Format("<param name=\"initParams\" value=\"user={0},{1}\" />"this.CurrentUser, this.CustomParams));
            sl.AppendLine(
"<a href=\"http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40818.0\" style=\"text-decoration:none\">");
            sl.AppendLine(
"<img src=\"http://go.microsoft.com/fwlink/?LinkId=161376\" alt=\"获取 Microsoft Silverlight\" style=\"border-style:none\"/>");
            sl.AppendLine(
"</a>");
            sl.AppendLine(
"</object>");
            sl.AppendLine(
"<iframe id=\"_sl_historyFrame\" style=\"visibility:hidden;height:0px;width:0px;border:0px\">");
            sl.AppendLine(
"</iframe>");
            sl.AppendLine(
"</div>");

            LiteralControl silverlightHost 
= new LiteralControl(sl.ToString());
            
this.Controls.Add(silverlightHost);
        }

        
private string GetCacheKey()
        {
            
string cacheKey = string.Empty;

            
switch (CacheUnit)
            {
                
case "":
                    cacheKey 
= DateTime.Now.ToString("yyyyMMddhhmm");
                    
break;
                
case "":
                    cacheKey 
= DateTime.Now.ToString("yyyyMMddhh");
                    
break;
                
case "":
                    cacheKey 
= DateTime.Now.ToString("yyyyMMdd");
                    
break;
                
case "":
                    cacheKey 
= DateTime.Now.ToString("yyyyMM");
                    
break;
                
default:
                    cacheKey 
= DateTime.Now.ToString("yyyy");
                    
break;
            }
            
return cacheKey;
        }

        
#endregion
    }
}

posted @ 2011-03-07 12:54  LanceZhang  阅读(1244)  评论(1编辑  收藏  举报