教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 文库大全 > 专业资料 >

Silverlight初学者的入门课程(6)

来源:网络收集 时间:2026-05-23
导读: MediaElement media = new MediaElement(); Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream( ~ 37 ~ 程序员之家 MyNamespace.Sound1.wav); media.SetSource(stream); media.AutoPlay

MediaElement media = new MediaElement(); Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(
~ 37 ~
程序员之家

"MyNamespace.Sound1.wav"); media.SetSource(stream); media.AutoPlay = false; media.Stop(); media.Play();
代码分析:
Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream( "MyNamespace.Sound1.wav");
上面的代码行从可执行程序集捕获视频流。 记住这个视频文件时通过设置 “生成操作” “嵌 为 入的资源”嵌入在程序集中的。 在上面代码中, “MyNamespace”表示应用程序的命名空间。 “Sound1.wav”是视频的文 明名, 假设这个文件在应用程序的根目录。 如果文件在子目录, 也需要包括目录名, “.”
用 号隔开。
media.SetSource(stream);
上面的代码行,设置了播放媒体的源。
media.AutoPlay = false;
上面的代码行,表示播放器不能自动播放媒体,只有点击了开始才会播放。
media.Stop(); media.Play();
上面

资料,教程,编程,文集

的 diamond 行,可以看到在.Play()之前做了.Stop()。这个停止仅在你多次播放 相同的 MediaElement 时才需要。
第三十章 在 Silverlight 中如何显示右键菜单?
当右键 Web 页面中 Silverlight 控件的任何地方,会显示一个默认的 Silverlight 右 键菜单 – 简单的 Silverlight 配置项。可以拦截这个右键菜单并显示自己的菜单。 需要做的第一步是,在承载 Silverlight 控件的 aspx 页面中设置 xaml 元素的 “Windowless”属性为“True” 。
现在,可以在 xaml 页面中设置浏览器文档对象的“AttachEvent”属性并为 “oncontextmenu”事件附加需要执行的事件操作。
~ 38 ~
程序员之家

示例代码:
System.Windows.Browser.HtmlPage.Document.AttachEvent("oncontextmenu", this.OnContextMenu);
可以在 XAML 页面的构造方法中附加你要执行的事件操作。甚至可以从 App.xaml 的构造 方法中执行,这样在项目中的每个 xaml 页面都会触发这个事件处理。 下面是事件处理代码:
private void OnContextMenu(object sender, System.Windows.Browser.HtmlEventArgs e) { MessageBox.Show("You clicked at " + e.OffsetX + "," + e.OffsetY); e.PreventDefault(); }
上面的示例代码显示点击位置的坐标。如下图所示:
你可以在代码中显示自己的菜单或弹出窗口。代码“e.PreventDefault();”放置事件 传递到其他子控件。
第三十一章 介绍 Silverlight 绑定数据
数据绑定可以动态的将对象属性绑定到 Silverlight 中 UI 控件的属性中。 例如,一个简单的 Silverlight 控件用于接收用户的名字和地址。可以创建一个 xaml 控件具有以下文本字段: 1. Name 2. Address1 3. Address2 4. City 5. State 6. Zip code 在这里 XAML 中都有一个 Grid 用于摆放示例中的控件:
<Grid x:Name="LayoutRoot" Background="AliceBlue" Width="300" Height="300">
~ 39 ~
程序员之家

<Grid.RowDefinitions> <RowDefinition Height="30"></RowDefinition> <RowDefinition Height="30"></RowDefinition> <RowDefinition Height="30"></RowDefinition> <RowDefinition Height="30"></RowDefinition> <RowDefinition Height="30"></RowDefinition> <RowDefinition Height="30"></RowDefinition> <RowDefinition Height="30"></RowDefinition> <RowDefinition Height="30"></RowDefinition> <RowDefinition Height="30"></RowDefinition> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="100"></ColumnDefinition> <ColumnDefinition Width="200"></ColumnDefinition> </Grid.ColumnDefinitions> <TextBlock Text="Name" Grid.Row="0" Grid.Column="0"></TextBlock> <TextBlock Text="Address 1" Grid.Row="1" Grid.Column="0"></TextBlock> <TextBlock Text="Address 2" Grid.Row="2" Grid.Column="0"></TextBlock> <TextBlock Text="City" Grid.Row="3" Grid.C
olumn="0"></TextBlock> <TextBlock Text="State" Grid.Row="4" Grid.Column="0"></TextBloc

资料,教程,编程,文集

k> <TextBlock Text="Zipcode" Grid.Row="5" Grid.Column="0"></TextBlock> <TextBox x:Name="txtName" Text="{Binding Name, Mode=TwoWay}" Grid.Row="0" Grid.Column="1" Height="20" Width="100"></TextBox> <TextBox x:Name="txtAddress1" Text="{Binding Address1, Mode=TwoWay}" Grid.Row="1" Grid.Column="1" Height="20" Width="100"></TextBox> <TextBox x:Name="txtAddress2" Text="{Binding Address2, Mode=TwoWay}" Grid.Row="2" Grid.Column="1" Height="20" Width="100"></TextBox> <TextBox x:Name="txtCity" Text="{Binding City, Mode=TwoWay}" Grid.Row="3" Grid.Column="1" Height="20" Width="100"></TextBox> <TextBox x:Name="txtState" Text="{Binding State, Mode=TwoWay}" Grid.Row="4"
~ 40 ~
程序员之家

Grid.Column="1" Height="20" Width="100"></TextBox> <TextBox x:Name="txtZipcode" Text="{Binding Zipcode, Mode=TwoWay}" Grid.Row="5" Grid.Column="1" Height="20" Width="100"></TextBox> </Grid>
在上面的 XAML 定义了一个 7 行 2 列的网格。这些控件通过使用各自的 Grid.Row 和 Grid.Column 属性防止合适的行、列中。当 Silverlight 控件放在 Web 页面显示如下 所示:
现在创建一个名为“Address”的类,它的各个属性表示我们需要的域。看“Address” 类的示例代码:
public class Address { public string Name { get; set; } public string Address1 { get; set; } public string Address2 { get; set; } public string City { get; set; } public string State { get; set; } public string Zipcode { get; set; …… 此处隐藏:3378字,全部文档内容请下载后查看。喜欢就下载吧 ……

Silverlight初学者的入门课程(6).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wenku/269917.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)