Silverlight 很酷的名字,在以前版本 Silverlight 只能承�在Web 上面,由於科技不�的更新�在Silverlight也可以做成桌面版模式去�行,是外�可以更好的�用���,�可以�用用�一些公用的�件如 Office 等功能。
本篇��文章只介�如何�建一�Silverlight 桌面程序 (OOB模式)。
���件:必�是Silverlight 4.0 或以上版本,使用VS 2010 SP1或以上版本。
本篇文章使用的是 VS 2012 和 Silverlight 5.0因�使用VS 2010 SP1 安�上比�麻�。
第一步:�建�目方案 如�:
��我把方案和�目名�起名� SL_Desktop 然後按�定出�下�:
���定
��我�看到了���目SL_Desktop 和 SL_Desktop.Web 一�是 Silverlight的界面��,另外一�承�的Silverlight�示的程序。
第二步:我�就需要�置一下Silverlight 的程序,在SL_Desktop �目右��性在Silverlight ��中看到”允�在游�器外�行�用程序” 上打勾,如�:
然後�� ”游�器外�置” 按�,首先�置窗口你想�示的��名�,�置默���后�度和高度你也可以�置��后的窗�位置默�居中
�置快捷�名��可以�上�用程序的�明内容,�置快捷�的��,��一共有4种格式
重要的一部,UI的美�度�有需要�� 3D 效果就需要把使用GOD加速勾上。
“�示安�菜�”默�是勾上,��是在程序的右�可以安�程序到用��子上,可以按��者需要��置
�了�行�的�用�的操作系��程序的信任度提高我�把“在游�器外�行�需要提升的信任”勾上
最後出�”窗口�式” ��
-默�值就像: Windows 的文件游�器一�有最小按� 最大化按�和��按�;
��框: 也就�有最小按� 最大化按�和��按�和���;
���角框: 在��框�置使用�角外��示,�����人比�喜�。
�置完成保存,�置界面如下�:
�了�示Silverlight 程序的��在 MainPage.xaml 添加了一些 文字和按�如�:
在按�添加��事件,用���程序。只要�用 Application.Current.MainWindow.Close(); 就可以了
第三步:�行看到�果如�:
奇怪怎麽�是在Web上面�示呢? �急�有其他步�的在 Silverlight 右�看到菜�中”� SL_Desktop 应用程序 安�到此�算�” ��后,在Windows 的桌面和�始菜�的所有程序都�出�一�快捷方式”SL_Desktop 应用程序”
如果已�安�后怎麽卸�呢可以在程序,可以在�行�程序后右���程序然後右��出�”�除�程序�用”就�自�卸�完。
就�麽��程序就可以在 Web 模式 或者 桌面模式下�行
Silverlight 程序中判�用�是使用Web游�器�用�是使用桌面模式�用
第一步:在MainPage.xaml 界面中增加一�TextBlock 控件 代�如下:
- <TextBlock x:Name="Txt_Msg" HorizontalAlignment="Center" Margin="0,60,0" textwrapping="Wrap" VerticalAlignment="Center" FontSize="20" FontWeight="Bold"/>
第二部:在MainPage.xaml.cs 文件的默��造方法中增加判�后在上面的TextBlock�示�果,判�方法只要使用Application.Current.IsRunningOutOfbrowser �性就可以知道�前程序使用什麽模式�示的。
代�如下:
第三部:�行�果如下:
Silverlight�建安�桌面程序菜�
在用�的��中鼠�右�在Web程序�面是一�不友好的方式,所以最好�是在界面中�示安�按�,之前已���了判�在游�器中使用�是在游�器中使用。��我�就可以�示和�藏�安�按�。
第一步:�查用�是否有安��程序到桌面使用Application.Current.InstallState�性就可以了;
第二步:安�方法只要�用 Application.Current.Install() 就可以安�到用的�算�上;
第三部:用�需要知道安�情�,可以�� Application.Current.InstallStateChanged 事件��控安�情�;
完整修改代�如下:
- /// <summary>
- /// Silverlight Demo
- /// </summary>
- public partial class MainPage : UserControl
- {
- public MainPage()
- {
- InitializeComponent();
- this.Btn_Install.Visibility = System.Windows.Visibility.Collapsed;
- if (Application.Current.IsRunningOutOfbrowser == true)
- {
- //使用桌面模式
- this.Txt_Msg.Text = "当前调用的是使用桌面模式";
- }
- else
- {
- //使用的游览器
- this.Txt_Msg.Text = "当前调用的是使用游览器模式";
- }
- this.displayInstallState();
- Application.Current.InstallStateChanged += Current_InstallStateChanged;
- }
- /// <summary>
- /// 检查安装状态
- /// </summary>
- private void displayInstallState()
- {
- switch (Application.Current.InstallState)
- {
- case InstallState.InstallFailed:
- //不能将该应用程序安装为在浏览器外部运行
- this.Txt_Status.Text = "不能将该应用程序安装为在浏览器外部运行";
- break;
- case InstallState.Installed:
- //已经将该应用程序安装为在浏览器外部运行
- this.Txt_Status.Text = "已经将该应用程序安装为在浏览器外部运行";
- break;
- case InstallState.Installing:
- //正在将此应用程序安装为在浏览器外部运行
- this.Txt_Status.Text = "正在将此应用程序安装为在浏览器外部运行";
- break;
- case InstallState.notinstalled:
- //尚未将该应用程序安装为在浏览器外部运行
- this.Txt_Status.Text = "尚未将该应用程序安装为在浏览器外部运行";
- this.Btn_Install.Visibility = System.Windows.Visibility.Visible;
- break;
- default:
- break;
- }
- }
- //安装状态
- void Current_InstallStateChanged(object sender, EventArgs e)
- {
- this.displayInstallState();
- }
- //关闭按钮
- private void Button_Click_1(object sender, RoutedEventArgs e)
- {
- Application.Current.MainWindow.Close();
- }
- //安装按钮
- private void Btn_Install_Click(object sender, RoutedEventArgs e)
- {
- try
- {
- Application.Current.Install();
- }
- catch (InvalidOperationException)
- {
- MessageBox.Show("应用已经安装.");
- }
- catch (Exception)
- {
- this.Txt_Status.Text = "安装失败";
- }
- }
- }
Silverlight桌面程序自�更新功能
�在程序的安�都完成了�差什麽呢?大家一定猜了就是更新��,因���也算半�桌面程序那麽如果原程序更改了,客�端又不知道���致程序用�的不能正常使用程序了。�不能要用�每次打�游�器去做�除安���程序吧!�在我�就�解�����。
所需要的方法 Application.Current.CheckAndDownloadUpdateAsync() �然也有事件可以知道更新的��只要��Application.Current.CheckAndDownloadUpdateCompleted 事件就可以清楚知道程序更新情�了
在界面上添加一�更新按�����事件内容事件Application.Current.CheckAndDownloadUpdateAsync();
在程序的�造方法中��事件 Application.Current.CheckAndDownloadUpdateCompleted 事件中 e.UpdateAvailable 可以知道 更新是否成功;
不���有很��的地方就是更新�然需要重�程序才能算真正的完成可惜在Silverlight程序��有�有重�程序的方法不�我想以後微��加入�些方法。
代�如下
- /// <summary>
- /// Silverlight Demo
- /// </summary>
- public partial class MainPage : UserControl
- {
- public MainPage()
- {
- InitializeComponent();
- this.Btn_Install.Visibility = System.Windows.Visibility.Collapsed;
- if (Application.Current.IsRunningOutOfbrowser == true)
- {
- //使用桌面模式
- this.Txt_Msg.Text = "当前调用的是使用桌面模式";
- }
- else
- {
- //使用的游览器
- this.Txt_Msg.Text = "当前调用的是使用游览器模式";
- }
- this.displayInstallState();
- Application.Current.CheckAndDownloadUpdateCompleted += Current_CheckAndDownloadUpdateCompleted;
- Application.Current.InstallStateChanged += Current_InstallStateChanged;
- }
- //更新事件
- void Current_CheckAndDownloadUpdateCompleted(object sender, CheckAndDownloadUpdateCompletedEventArgs e)
- {
- if (e.UpdateAvailable == true && e.Error == null)
- {
- MessageBox.Show("应用新版本已经下载成功,请重新启动程序。");
- Application.Current.MainWindow.Close();
- }
- else if (e.UpdateAvailable == false && e.Error == null)
- {
- MessageBox.Show("已经是最新版本了。");
- }
- else if (e.Error != null)
- {
- MessageBox.Show("在检测应用更新时, 出现以下错误信息:" + Environment.NewLine + Environment.NewLine + e.Error.Message);
- }
- }
- /// <summary>
- /// 检查安装状态
- /// </summary>
- private void displayInstallState()
- {
- switch (Application.Current.InstallState)
- {
- case InstallState.InstallFailed:
- //不能将该应用程序安装为在浏览器外部运行
- this.Txt_Status.Text = "不能将该应用程序安装为在浏览器外部运行";
- break;
- case InstallState.Installed:
- //已经将该应用程序安装为在浏览器外部运行
- this.Txt_Status.Text = "已经将该应用程序安装为在浏览器外部运行";
- break;
- case InstallState.Installing:
- //正在将此应用程序安装为在浏览器外部运行
- this.Txt_Status.Text = "正在将此应用程序安装为在浏览器外部运行";
- break;
- case InstallState.notinstalled:
- //尚未将该应用程序安装为在浏览器外部运行
- this.Txt_Status.Text = "尚未将该应用程序安装为在浏览器外部运行";
- this.Btn_Install.Visibility = System.Windows.Visibility.Visible;
- break;
- default:
- break;
- }
- }
- //更新
- private void Button_Click_2(object sender, RoutedEventArgs e)
- {
- Application.Current.CheckAndDownloadUpdateAsync();
- }
- //安装状态
- void Current_InstallStateChanged(object sender, EventArgs e)
- {
- this.displayInstallState();
- }
- //关闭按钮
- private void Button_Click_1(object sender, RoutedEventArgs e)
- {
- Application.Current.MainWindow.Close();
- }
- //安装按钮
- private void Btn_Install_Click(object sender, RoutedEventArgs e)
- {
- try
- {
- Application.Current.Install();
- }
- catch (InvalidOperationException)
- {
- MessageBox.Show("应用已经安装.");
- }
- catch (Exception)
- {
- this.Txt_Status.Text = "安装失败";
- }
- }
- }
�行�果
重新��程序
�果版本就一�了,如果�是后再��更新就�出�下�:
好了�在安�到更新都有了我��要�用�想一下,如果用�的���有�接��的情�或者使用中��桌面模式�取服�信息�候就�出�����用�就��得你的程序也太差了吧!
解�方法:就是�程序去�查是否有��存在;
我�可以使用 NetworkInterface.GetIsNetworkAvailable() 方法�取是否在������;
�然我�也要���控��的事件 NetworkChange.NetworkAddressChanged ��程序作出更好的用���。
��代�:
- //�查����
- private void checkNetworkStatus()
- {
- if (NetworkInterface.GetIsNetworkAvailable() == true)
- {
- this.Txt_NetState.Text = "�前���於�接��";
- }
- else
- {
- this.Txt_NetState.Text = "�前���於����";
- }
- }
- this.checkNetworkStatus();
- NetworkChange.NetworkAddressChanged += NetworkChange_NetworkAddressChanged;
�行查看�果
本篇教程完�! ���者!
大家可以去下�示例代�。