这个时候我们希望有一个控件,像Image一样直要传给它ImageUrl就可以自动显示出合适的东西
[Designer("System.Web.UI.Design.WebControls.PreviewControlDesigner, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
[DefaultProperty("ImageUrl")]
[ToolboxData("<{0}:MultiMedia runat=server></{0}:MultiMedia>")]
public class MultiMedia : Image
{
public FileType FileType = FileType.Other;
protected virtual void CalculateType()
{
if (!string.IsNullOrEmpty(ImageUrl))
{
int start = ImageUrl.LastIndexOf('.');
string type = ImageUrl.Substring(start, ImageUrl.Length - start).ToLower();
if (type == ".swf")
{
FileType = FileType.Flash;
}
else
{
FileType = FileType.Image;
}
}
}
protected override void Render(HtmlTextWriter writer)
{
CalculateType();
switch(FileType)
{
case FileType.Image:
base.Render(writer);
break;
case FileType.Flash:
RenderFlash(writer);
break;
}
}
public virtual void RenderFlash(HtmlTextWriter output)
{
string url = this.ResolveUrl(ImageUrl);
output.Write("<embed src='"+url+"' quality='high' pluginspage='http://www.macromedia.com/go/getflashplayer' type='application/x-shockwave-flash' width='"+this.Width+"' height='"+this.Height+"' wmode='Opaque'></embed>");
}
}
public enum FileType
{
Image,
Flash,
Other,
}
}
[DefaultProperty("ImageUrl")]
[ToolboxData("<{0}:MultiMedia runat=server></{0}:MultiMedia>")]
public class MultiMedia : Image
{
public FileType FileType = FileType.Other;
protected virtual void CalculateType()
{
if (!string.IsNullOrEmpty(ImageUrl))
{
int start = ImageUrl.LastIndexOf('.');
string type = ImageUrl.Substring(start, ImageUrl.Length - start).ToLower();
if (type == ".swf")
{
FileType = FileType.Flash;
}
else
{
FileType = FileType.Image;
}
}
}
protected override void Render(HtmlTextWriter writer)
{
CalculateType();
switch(FileType)
{
case FileType.Image:
base.Render(writer);
break;
case FileType.Flash:
RenderFlash(writer);
break;
}
}
public virtual void RenderFlash(HtmlTextWriter output)
{
string url = this.ResolveUrl(ImageUrl);
output.Write("<embed src='"+url+"' quality='high' pluginspage='http://www.macromedia.com/go/getflashplayer' type='application/x-shockwave-flash' width='"+this.Width+"' height='"+this.Height+"' wmode='Opaque'></embed>");
}
}
public enum FileType
{
Image,
Flash,
Other,
}
}
也许你发现了。什么在选择的时候没有发现*.swf 这个时候我们可以自己扩展
public class MediaUrlFilter : System.Web.UI.Design.UrlEditor
{
protected override string Filter
{
get
{
return "MultiType (*.swf *.jpg *.gif) |*.swf;*.gif;*.jpg;*.png|All files|*.*";
}
}
}
{
protected override string Filter
{
get
{
return "MultiType (*.swf *.jpg *.gif) |*.swf;*.gif;*.jpg;*.png|All files|*.*";
}
}
}
并重写
[Editor(typeof(MediaUrlFilter),typeof(System.Drawing.Design.UITypeEditor))]
public override string ImageUrl
{
get
{
return base.ImageUrl;
}
set
{
base.ImageUrl = value;
}
}
这样我们就可以发现了。public override string ImageUrl
{
get
{
return base.ImageUrl;
}
set
{
base.ImageUrl = value;
}
}
注意:System.Web.UI.Design 这个命名空间在 System.Design.dll里。需要使用browser把它加进来
一些需要用到的东西可以自己扩展
ADD:
public virtual void RenderVideo(HtmlTextWriter output)
{
output.NewLine = "\r\n";
string url = this.ResolveUrl(ImageUrl);
output.Write("<embed src='"+url+"' border='0' width='"+this.Width+"' height='"+this.Height+"' type='application/x-mplayer2'></embed>");
}
{
output.NewLine = "\r\n";
string url = this.ResolveUrl(ImageUrl);
output.Write("<embed src='"+url+"' border='0' width='"+this.Width+"' height='"+this.Height+"' type='application/x-mplayer2'></embed>");
}