Silverlight 中的HtmlPage 和 HtmlElement 分别代表HTML页面和HTML元素
获取HTML元素改变他的属性
<div> <img id="img1" src="Images/logo.jpg" /> </div>
xaml.cs:
private void Button1_Click(object sender, RoutedEventArgs e)
{
//获取HTML中的img1对象
HtmlElement img = HtmlPage.Document.GetElementById("img1");
//根据输入值设置img1的html属性Width和Height
img.SetAttribute("width", textBox1.Text);
img.SetAttribute("height", textBox2.Text);
}
HTML元素操作Silverlight
<div>
<select id="sel"> <option value="Red">红ì色?</option> <option value="Green">绿ì色?</option> <option value="Blue">蓝?色?</option> </select> </div>
xaml.cs:
public htmldom2()
{
InitializeComponent();
//获取select对象
HtmlElement select = HtmlPage.Document.GetElementById("sel");
//添加select的onchange事件
select.AttachEvent("onchange"
, new EventHandler<HtmlEventArgs>(select_onChange));
}
public void select_onChange(object sender, HtmlEventArgs e)
{
//获取select的选择值
HtmlElement select = sender as HtmlElement;
string value = select.GetAttribute("value");
textBlock1.Text = value;
//根据用户选择值来改变椭圆形的Fill值
switch (value)
{
case "Red":
ellipse1.Fill = new SolidColorBrush(Colors.Red);
break;
case "Green":
ellipse1.Fill = new SolidColorBrush(Colors.Green);
break;
case "Blue":
ellipse1.Fill = new SolidColorBrush(Colors.Blue);
break;
}
}
HTML 文本 和 URL文本的编解码
Html编码
HttpUtility.HtmlEncode(textBlock1.Text);
HTML解码
HttpUtility.HtmlDecode(textBlock2.Text);
URL编码
HttpUtility.UrlEncode(textBlock3.Text);
URL解码
HttpUtility.UrlDecode(textBlock4.Text);
读写Cookie
//客户端Cookie读写类
public class CookieHelper
{
//根据Key和Value写客户端Cookie
public static void SetCookie(string key, string value)
{
DateTime expire = DateTime.UtcNow
+ TimeSpan.FromDays(30);
string cookie = string.Format("{0}={1};expires={2}"
, key, value, expire.ToString("R"));
HtmlPage.Document.SetProperty("cookie"
, cookie);
}
//根据Key读客户端Cookie
public static string GetCookie(string key)
{
key += '=';
//取出所有Cookie
string[] cookies =
HtmlPage.Document.Cookies.Split(';');
//遍历Cookie值
foreach (string cookie in cookies)
{
string cookieStr = cookie.Trim();
//获取Cookie的key名称的位置
if (cookieStr.StartsWith(key,
StringComparison.OrdinalIgnoreCase))
{
//分隔出key的值
string[] vals = cookieStr.Split('=');
if (vals.Length >= 2)
{
//返回值
return vals[1];
}
//如果没有找到则返回空白字符串
return string.Empty;
}
}
//如果没有Cookie则返回空白字符串
return string.Empty;
}
}
在Silverlight 中使用 Dom 的Window 对象
导航
//根据输入值创建URI对象
Uri uri = new Uri(tbUrl.Text, UriKind.RelativeOrAbsolute);
//导航到URI地址
HtmlPage.Window.Navigate(uri);
浏览器提示窗口
提示窗口
HtmlPage.Window.Alert("这是使用HtmlPage调用的消息框!");
判定窗口
if (HtmlPage.Window.Confirm("你确定吗?")){}
输入窗口
string password = HtmlPage.Window.Prompt("请输入密码");
示例出自Silverlight 3.0 开发详解与最佳实践一书
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?