惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Attack and Defense Labs
Attack and Defense Labs
T
Threatpost
C
Cybersecurity and Infrastructure Security Agency CISA
H
Hackread – Cybersecurity News, Data Breaches, AI and More
I
Intezer
C
Cyber Attacks, Cyber Crime and Cyber Security
The Register - Security
The Register - Security
量子位
Security Latest
Security Latest
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
大猫的无限游戏
大猫的无限游戏
小众软件
小众软件
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
C
CXSECURITY Database RSS Feed - CXSecurity.com
MyScale Blog
MyScale Blog
J
Java Code Geeks
Apple Machine Learning Research
Apple Machine Learning Research
Google DeepMind News
Google DeepMind News
WordPress大学
WordPress大学
Spread Privacy
Spread Privacy
Jina AI
Jina AI
博客园 - 【当耐特】
P
Palo Alto Networks Blog
Last Week in AI
Last Week in AI
SecWiki News
SecWiki News
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
G
GRAHAM CLULEY
宝玉的分享
宝玉的分享
Hacker News - Newest:
Hacker News - Newest: "LLM"
T
The Blog of Author Tim Ferriss
V
Vulnerabilities – Threatpost
有赞技术团队
有赞技术团队
T
Tor Project blog
H
Hacker News: Front Page
A
Arctic Wolf
NISL@THU
NISL@THU
A
About on SuperTechFans
云风的 BLOG
云风的 BLOG
Engineering at Meta
Engineering at Meta
V
V2EX
N
News and Events Feed by Topic
Webroot Blog
Webroot Blog
Know Your Adversary
Know Your Adversary
P
Privacy International News Feed
I
InfoQ
D
Docker
L
LINUX DO - 最新话题
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
U
Unit 42

博客园 - LanceZhang

【招聘】招高级.Net研发工程师(上海-普陀) 测试 应对.NET Reflector过期的小技巧 不需编译让aspx页自主筛选数据绑定记录 也谈谈“加班”问题 【开源】二手火车票监控程序 抢票快手 V1.0发布 【开源】Winform甘特日程控件GanttPlanner V1.0发布 移除txt文本中不必要的换行符 在MOSS2007中开发并部署自定义WebService SQL Server 中统计各用户表记录条数 的两种方法 代友招中高级.NET开发工程师【上海-徐汇】 在64位Windows系统上安装Toad和PLSQL Developer等Oracle客户端工具 又是一年生日到,哥老了。。。 一个简单的PV统计例子,演示如何利用内存缓冲高并发环境下的计数 - LanceZhang - 博客园 文章目录:ASP.NET AJAX Advance Tips & Tricks 系列文章【共10篇】 Digg被黑了?No,呵呵 ASP.NET AJAX Advance Tips & Tricks (11) 三种方法动态创建Tooltip PPT:ASP.NET AJAX 性能优化 文章目录:使用VisualStudio 2010从分析到实施系列【共5+3篇】
共享一个封装Silverlight的WebPart
LanceZhang · 2011-03-07 · via 博客园 - LanceZhang

不管是报表图表还是电子地图,将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 Propertiesprivate 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 Membersprotected 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
    }
}