Silverlight 4:创建离线媒体播放应用程序






4.92/5 (12投票s)
探索 Silverlight 4 的功能。
引言
Silverlight 4 在 RIA 领域取得了一些巨大的进步,比如可以与 COM 组件(Office、Windows Media Player 等)通信,可以从网络摄像头和麦克风获取数据,并且对于本解决方案,可以访问用户的文件夹并从中消费数据。
我想特别感谢 Truetech 的开发团队,我的新家,以及帮助我实现这一成就的朋友们。
从何开始
对于这个项目,您将需要 Visual Studio 2010 Beta 2(当前版本不支持 Silverlight 4 的发布候选版)和适用于 Visual Studio 2010 的 Silverlight 工具或 .NET 4 的 Expression Blend Preview。
首先
安装这些工具后,您应该打开 Visual Studio 2010 并创建一个新的 Silverlight 项目。

之后,会弹出一个窗口

只需单击“确定”即可。
配置浏览器外体验
完成应用程序的初始配置后,您必须设置浏览器外体验的信息,这对于我们的方案是必要的,这可以通过右键单击 Silverlight 项目并选择“属性”来实现

以下选项卡将在 VS 中显示,将配置设置为与下方的图像匹配

然后单击浏览器外设置按钮,并像在下图中一样标记“在浏览器外运行时需要提升的信任级别”

现在是代码
我们有一个名为 IShell
的接口,此接口由 MainPage
实现,如下所示
public partial class MainPage : UserControl, IShell
{
public MainPage()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(MainPage_Loaded);
}
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
ResetVisual(false);
}
public void ResetVisual(bool justInstalled)
{
if (App.Current.InstallState == InstallState.Installed)
{
if (App.Current.IsRunningOutOfBrowser)
this.LayoutRoot.Content = new MainAppView();
else
{
if (justInstalled)
this.LayoutRoot.Content = new InstallationCompletedView();
else
this.LayoutRoot.Content = new RunningOnTheBrowserView();
}
}
else
{
var view = new ApplicationNotInstalledView();
view.Shell = this;
this.LayoutRoot.Content = view;
}
}
}
然后我们有另一个名为 IView
的接口,它由 ApplicationNotInstalledView
实现
public partial class ApplicationNotInstalledView : UserControl, IView
{
public static readonly DependencyProperty ShellProperty = DependencyProperty.Register(
"Shell", typeof(IShell), typeof(ApplicationNotInstalledView),
new PropertyMetadata(
(sender, e) =>
{
}));
public IShell Shell
{
get { return GetValue(ShellProperty) as IShell; }
set { SetValue(ShellProperty, value); }
}
public ApplicationNotInstalledView()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
App.Current.Install();
App.Current.InstallStateChanged +=
new EventHandler(Current_InstallStateChanged);
}
void Current_InstallStateChanged(object sender, EventArgs e)
{
if (App.Current.InstallState == InstallState.Installed)
Shell.ResetVisual(true);
App.Current.InstallStateChanged -= Current_InstallStateChanged;
}
}
如您所见,ApplicationNotInstalledView
连接了 App.Current.InstallStateChanged
事件,以便在安装完成后执行操作。我们也有一些信息视图,它们仅用于通知用户应用程序的状态(完全没有代码)。
主应用程序视图
在 MainAppView
类中,这是我们的实际应用程序,我们在“打开”按钮的点击事件中包含以下代码
private void Button_Click(object sender, RoutedEventArgs e)
{
var ofd = new OpenFileDialog();
ofd.Filter = "Media Files |*.wmv;*.wma;*.mp3";
ofd.Multiselect = false;
if((bool)ofd.ShowDialog())
{
if(File.Exists(ofd.File.FullName))
{
LoadMedia(ofd.File.OpenRead());
}
}
}
Silverlight 4 的概念是,在沙盒(浏览器外)中运行时,如果设置了正确的权限(这就是我们需要提升应用程序的原因),应用程序可以打开用户的特殊文件夹,例如“我的文档”、“我的音乐”、“我的视频”,并获取存储在这些文件夹中的数据,但如果尝试访问此范围之外的文件,它只会忽略它,因此也不会抛出异常。
关注点
在当前版本的 Silverlight 4 中,如果 Uri
是本地的(文件范围内的 Uri
不受 Silverlight 上 MediaElement
支持),我们无法通过其 Uri
访问 MediaElement
上的文件。 因此,要绕过此限制,您可以打开 File
的 Stream
,然后使用 SetSource
方法设置 MediaElement
的 Stream
,如下所示
void LoadMedia(Stream file)
{
player.SetSource(file);
}
历史
- 2010 年 3 月 11 日:初始发布