[源码下载]

重新想象 Windows 8 Store Apps (69) - 其它: 自定义启动屏幕, 程序的运行位置, 保持屏幕的点亮状态, MessageDialog, PopupMenu

作者:webabcd

介绍
重新想象 Windows 8 Store Apps 之 其它

  • 自定义启动屏幕
  • 检查当前呈现的应用程序是运行在本地还是运行在远程桌面或模拟器
  • 保持屏幕的点亮状态
  • MessageDialog - 信息对话框
  • PopupMenu - 上下文菜单

示例
1、演示如何自定义启动屏幕
Feature/MySplashScreen.xaml

<Page
x:Class="XamlDemo.Feature.MySplashScreen"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:XamlDemo.Feature"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Grid Background="#1b4b7c"> <Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="180"/>
</Grid.RowDefinitions> <Canvas Grid.Row="0">
<Image x:Name="splashImage" Source="/Assets/SplashScreen.png" />
</Canvas>
<StackPanel Grid.Row="1" HorizontalAlignment="Center">
<ProgressRing IsActive="True" />
<TextBlock Name="lblMsg" Text="我是自定义启动页,1 秒后跳转到主页" FontSize="14.667" Margin="0 10 0 0" />
</StackPanel> </Grid>
</Page>

Feature/MySplashScreen.xaml.cs

/*
* 演示如何自定义启动屏幕
* 关联代码:App.xaml.cs 中的 override void OnLaunched(LaunchActivatedEventArgs args)
*
* 应用场景:
* 1、app 的启动屏幕就是一个图片加一个背景色,其无法更改,
* 2、但是启动屏幕过后可以参照启动屏幕做一个内容更丰富的自定义启动屏幕
* 3、在自定义启动屏幕显示后,可以做一些程序的初始化工作,初始化完成后再进入主程序
*/ using System.Threading.Tasks;
using Windows.ApplicationModel.Activation;
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls; namespace XamlDemo.Feature
{
public sealed partial class MySplashScreen : Page
{
/*
* SplashScreen - app 的启动屏幕对象,在 Application 中的若干事件处理器中的事件参数中均可获得
* ImageLocation - app 的启动屏幕的图片的位置信息,返回 Rect 类型对象
* Dismissed - app 的启动屏幕关闭时所触发的事件
*/ // app 启动屏幕的相关信息
private SplashScreen _splashScreen; public MySplashScreen()
{
this.InitializeComponent(); lblMsg.Text = "自定义 app 的启动屏幕,打开 app 时可看到此页面的演示";
} // LaunchActivatedEventArgs 来源于 App.xaml.cs 中的 override void OnLaunched(LaunchActivatedEventArgs args)
public MySplashScreen(LaunchActivatedEventArgs args)
{
this.InitializeComponent(); ImplementCustomSplashScreen(args);
} private async void ImplementCustomSplashScreen(LaunchActivatedEventArgs args)
{
// 窗口尺寸发生改变时,重新调整自定义启动屏幕
Window.Current.SizeChanged += Current_SizeChanged; // 获取 app 的启动屏幕的相关信息
_splashScreen = args.SplashScreen; // app 的启动屏幕关闭时所触发的事件
_splashScreen.Dismissed += splashScreen_Dismissed; // 获取 app 启动屏幕的图片的位置,并按此位置调整自定义启动屏幕的图片的位置
Rect splashImageRect = _splashScreen.ImageLocation;
splashImage.SetValue(Canvas.LeftProperty, splashImageRect.X);
splashImage.SetValue(Canvas.TopProperty, splashImageRect.Y);
splashImage.Height = splashImageRect.Height;
splashImage.Width = splashImageRect.Width; await Task.Delay(); // 关掉自定义启动屏幕,跳转到程序主页面
var rootFrame = new Frame();
rootFrame.Navigate(typeof(MainPage), args);
Window.Current.Content = rootFrame;
Window.Current.Activate();
} void Current_SizeChanged(object sender, Windows.UI.Core.WindowSizeChangedEventArgs e)
{
// 获取 app 启动屏幕的图片的最新位置,并按此位置调整自定义启动屏幕的图片的位置
Rect splashImageRect = _splashScreen.ImageLocation;
splashImage.SetValue(Canvas.LeftProperty, splashImageRect.X);
splashImage.SetValue(Canvas.TopProperty, splashImageRect.Y);
splashImage.Height = splashImageRect.Height;
splashImage.Width = splashImageRect.Width;
} void splashScreen_Dismissed(SplashScreen sender, object args)
{ }
}
}

App.xaml.cs

protected async override void OnLaunched(LaunchActivatedEventArgs args)
{
// 显示自定义启动屏幕,参见 Feature/MySplashScreen.xaml.cs
if (args.PreviousExecutionState != ApplicationExecutionState.Running)
{
XamlDemo.Feature.MySplashScreen mySplashScreen = new XamlDemo.Feature.MySplashScreen(args);
Window.Current.Content = mySplashScreen;
} // 确保当前窗口处于活动状态
Window.Current.Activate();
}

2、演示如何检查当前呈现的应用程序是运行在本地还是运行在远程桌面或模拟器
Feature/CheckRunningSource.xaml.cs

using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation; namespace XamlDemo.Feature
{
public sealed partial class CheckRunningSource : Page
{
public CheckRunningSource()
{
this.InitializeComponent();
} protected override void OnNavigatedTo(NavigationEventArgs e)
{
/*
* 如果当前呈现给用户的应用程序运行在本地,则 Windows.System.RemoteDesktop.InteractiveSession.IsRemote 为 false
* 如果当前呈现给用户的应用程序运行在远程桌面或运行在模拟器,则 Windows.System.RemoteDesktop.InteractiveSession.IsRemote 为 true
*/
lblMsg.Text = "Windows.System.RemoteDesktop.InteractiveSession.IsRemote: " + Windows.System.RemoteDesktop.InteractiveSession.IsRemote;
}
}
}

3、演示如何通过 DisplayRequest 来保持屏幕的点亮状态
Feature/KeepDisplay.xaml

<Page
x:Class="XamlDemo.Feature.KeepDisplay"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:XamlDemo.Feature"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Grid Background="Transparent">
<StackPanel Margin="120 0 0 0"> <MediaElement Name="mediaElement" AutoPlay="False" Source="http://media.w3.org/2010/05/sintel/trailer.mp4" PosterSource="/Assets/Logo.png" Width="480" Height="320" HorizontalAlignment="Left" VerticalAlignment="Top" /> <Button Name="btnPlay" Content="play" Click="btnPlay_Click" Margin="0 10 0 0" /> <Button Name="btnPause" Content="pause" Click="btnPause_Click" Margin="0 10 0 0" /> <TextBlock Name="lblMsg" FontSize="14.667" Margin="0 10 0 0" /> </StackPanel>
</Grid>
</Page>

Feature/KeepDisplay.xaml.cs

/*
* 演示如何通过 DisplayRequest 来保持屏幕的点亮状态
*
* 适用场景举例:
* 用户在观看视频时,在视频播放中我们是希望屏幕保持点亮的,但是如果用户暂停了视频的播放,我们则是希望不保持屏幕的点亮
*/ using System;
using Windows.System.Display;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls; namespace XamlDemo.Feature
{
public sealed partial class KeepDisplay : Page
{
private DisplayRequest dispRequest = null; public KeepDisplay()
{
this.InitializeComponent();
} private void btnPlay_Click(object sender, RoutedEventArgs e)
{
mediaElement.Play(); if (dispRequest == null)
{
// 用户观看视频,需要保持屏幕的点亮状态
dispRequest = new DisplayRequest();
dispRequest.RequestActive(); // 激活显示请求 lblMsg.Text += "屏幕会保持点亮状态";
lblMsg.Text += Environment.NewLine;
}
} private void btnPause_Click(object sender, RoutedEventArgs e)
{
mediaElement.Pause(); if (dispRequest != null)
{
// 用户暂停了视频,则不需要保持屏幕的点亮状态
dispRequest.RequestRelease(); // 停用显示请求
dispRequest = null; lblMsg.Text += "屏幕不会保持点亮状态";
lblMsg.Text += Environment.NewLine;
}
}
}
}

4、演示 MessageDialog 的应用
Feature/MessageDialogDemo.xaml

<Page
x:Class="XamlDemo.Feature.MessageDialogDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:XamlDemo.Feature"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Grid Background="Transparent">
<StackPanel Margin="120 0 0 0"> <TextBlock Name="lblMsg" FontSize="14.667" /> <Button Name="btnShowMessageDialogSimple" Content="弹出一个简单的 MessageDialog" Click="btnShowMessageDialogSimple_Click_1" Margin="0 10 0 0" /> <Button Name="btnShowMessageDialogCustomCommand" Content="弹出一个自定义命令按钮的 MessageDialog" Click="btnShowMessageDialogCustomCommand_Click_1" Margin="0 10 0 0" /> </StackPanel>
</Grid>
</Page>

Feature/MessageDialogDemo.xaml.cs

/*
* MessageDialog - 信息对话框
* Content - 内容
* Title - 标题
* Options - 选项(Windows.UI.Popups.MessageDialogOptions 枚举)
* None - 正常,默认值
* AcceptUserInputAfterDelay - 为避免用户误操作,弹出对话框后短时间内禁止单击命令按钮
* Commands - 命令按钮集合,返回 IList<IUICommand> 类型的数据
* DefaultCommandIndex - 按“enter”键后,激发此索引位置的命令
* CancelCommandIndex - 按“esc”键后,激发此索引位置的命令
* ShowAsync() - 显示对话框,并返回用户激发的命令
*
* IUICommand - 命令
* Label - 显示的文字
* Id - 参数
*/ using System;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls; namespace XamlDemo.Feature
{
public sealed partial class MessageDialogDemo : Page
{
public MessageDialogDemo()
{
this.InitializeComponent();
} // 弹出一个简单的 MessageDialog
private async void btnShowMessageDialogSimple_Click_1(object sender, RoutedEventArgs e)
{
MessageDialog messageDialog = new MessageDialog("内容", "标题"); await messageDialog.ShowAsync();
} // 弹出一个自定义命令按钮的 MessageDialog
private async void btnShowMessageDialogCustomCommand_Click_1(object sender, RoutedEventArgs e)
{
MessageDialog messageDialog = new MessageDialog("内容", "标题"); messageDialog.Commands.Add(new UICommand("自定义命令按钮1", (command) =>
{
lblMsg.Text = string.Format("label:{0}, commandId:{1}", command.Label, command.Id);
}, "param1")); messageDialog.Commands.Add(new UICommand("自定义命令按钮2", (command) =>
{
lblMsg.Text = string.Format("label:{0}, commandId:{1}", command.Label, command.Id);
}, "param2")); messageDialog.Commands.Add(new UICommand("自定义命令按钮3", (command) =>
{
lblMsg.Text = string.Format("label:{0}, commandId:{1}", command.Label, command.Id);
}, "param3")); messageDialog.DefaultCommandIndex = ; // 按“enter”键后,激发第 1 个命令
messageDialog.CancelCommandIndex = ; // 按“esc”键后,激发第 3 个命令
messageDialog.Options = MessageDialogOptions.AcceptUserInputAfterDelay; // 对话框弹出后,短时间内禁止用户单击命令按钮,以防止用户的误操作 // 显示对话框,并返回用户激发的命令
IUICommand chosenCommand = await messageDialog.ShowAsync(); lblMsg.Text += Environment.NewLine;
lblMsg.Text += string.Format("label:{0}, commandId:{1}", chosenCommand.Label, chosenCommand.Id);
}
}
}

5、演示 PopupMenu 的应用
Feature/PopupMenuDemo.xaml

<Page
x:Class="XamlDemo.Feature.PopupMenuDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:XamlDemo.Feature"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Grid Background="Transparent">
<StackPanel Margin="120 0 0 0"> <TextBlock Name="lblMsg" FontSize="14.667" /> <TextBlock Name="lblDemo" FontSize="26.667" Margin="0 10 0 0">
右键我或press-and-hold我,以弹出 PopupMenu
</TextBlock> </StackPanel>
</Grid>
</Page>

Feature/PopupMenuDemo.xaml.cs

/*
* PopupMenu - 上下文菜单
* Commands - 命令按钮集合,返回 IList<IUICommand> 类型的数据
* ShowAsync(), ShowForSelectionAsync() - 在指定的位置显示上下文菜单,并返回用户激发的命令
*
* IUICommand - 命令
* Label - 显示的文字
* Id - 参数
*/ using System;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;
using XamlDemo.Common; namespace XamlDemo.Feature
{
public sealed partial class PopupMenuDemo : Page
{
public PopupMenuDemo()
{
this.InitializeComponent(); lblDemo.RightTapped += lblDemo_RightTapped;
} async void lblDemo_RightTapped(object sender, RightTappedRoutedEventArgs e)
{
PopupMenu menu = new PopupMenu(); menu.Commands.Add(new UICommand("item1", (command) =>
{
lblMsg.Text = string.Format("label:{0}, commandId:{1}", command.Label, command.Id);
}, "param1")); menu.Commands.Add(new UICommand("item2", (command) =>
{
lblMsg.Text = string.Format("label:{0}, commandId:{1}", command.Label, command.Id);
}, "param2")); // 分隔符
menu.Commands.Add(new UICommandSeparator()); menu.Commands.Add(new UICommand("item3", (command) =>
{
lblMsg.Text = string.Format("label:{0}, commandId:{1}", command.Label, command.Id);
}, "param3")); // 在指定的位置显示上下文菜单,并返回用户激发的命令
IUICommand chosenCommand = await menu.ShowForSelectionAsync(Helper.GetElementRect((FrameworkElement)sender), Placement.Below);
if (chosenCommand == null) // 用户没有在上下文菜单中激发任何命令
{
lblMsg.Text = "用户没有选择任何命令";
}
else
{
lblMsg.Text += Environment.NewLine;
lblMsg.Text += string.Format("label:{0}, commandId:{1}", chosenCommand.Label, chosenCommand.Id);
}
}
}
}

OK
[源码下载]

重新想象 Windows 8 Store Apps (69) - 其它: 自定义启动屏幕, 程序的运行位置, 保持屏幕的点亮状态, MessageDialog, PopupMenu的更多相关文章

  1. 重新想象 Windows 8 Store Apps 系列文章索引

    [源码下载][重新想象 Windows 8.1 Store Apps 系列文章] 重新想象 Windows 8 Store Apps 系列文章索引 作者:webabcd 1.重新想象 Windows ...

  2. 重新想象 Windows 8 Store Apps (34) - 通知: Toast Demo, Tile Demo, Badge Demo

    [源码下载] 重新想象 Windows 8 Store Apps (34) - 通知: Toast Demo, Tile Demo, Badge Demo 作者:webabcd 介绍重新想象 Wind ...

  3. 重新想象 Windows 8 Store Apps (35) - 通知: Toast 详解

    [源码下载] 重新想象 Windows 8 Store Apps (35) - 通知: Toast 详解 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 通知 Toa ...

  4. 重新想象 Windows 8 Store Apps (36) - 通知: Tile 详解

    [源码下载] 重新想象 Windows 8 Store Apps (36) - 通知: Tile 详解 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 通知 Tile ...

  5. 重新想象 Windows 8 Store Apps (37) - 契约: Settings Contract

    [源码下载] 重新想象 Windows 8 Store Apps (37) - 契约: Settings Contract 作者:webabcd 介绍重新想象 Windows 8 Store Apps ...

  6. 重新想象 Windows 8 Store Apps (38) - 契约: Search Contract

    [源码下载] 重新想象 Windows 8 Store Apps (38) - 契约: Search Contract 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 ...

  7. 重新想象 Windows 8 Store Apps (39) - 契约: Share Contract

    [源码下载] 重新想象 Windows 8 Store Apps (39) - 契约: Share Contract 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之  ...

  8. 重新想象 Windows 8 Store Apps (40) - 剪切板: 复制/粘贴文本, html, 图片, 文件

    [源码下载] 重新想象 Windows 8 Store Apps (40) - 剪切板: 复制/粘贴文本, html, 图片, 文件 作者:webabcd 介绍重新想象 Windows 8 Store ...

  9. 重新想象 Windows 8 Store Apps (41) - 打印

    [源码下载] 重新想象 Windows 8 Store Apps (41) - 打印 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 打印 示例1.需要打印的文档Pr ...

随机推荐

  1. Windows调试学习笔记:(二)WinDBG调试.NET程序示例

    好不容易把环境打好了,一定要试试牛刀.我创建了一个极其简单的程序(如下).让我们期待会有好的结果吧,阿门! using System; using System.Collections.Generic ...

  2. 修饰者模式(装饰者模式,Decoration)

    1. 装饰者模式,动态地将责任附加到对象上.若要扩展功能,装饰者提供了比继承更加有弹性的替代方案. 2.组合和继承的区别 继承.继承是给一个类添加行为的比较有效的途径.通过使用继承,可以使得子类在拥有 ...

  3. Workday为何迟迟不进入中国

    全球知名HRM SaaS厂商Workday在世界各地攻城拔寨,俨然是HR SaaS的代名词,更是HRM市场的领导品牌.但是却单单在中国市场悄无声息,除了为数不多的海尔海外.联想海外等规模客户和部分ro ...

  4. PL/SQL Developer去掉启动时自动弹出的Logon弹出框方法

    以前用PL/SQL Developer 7.0版本,最近升级到PL/SQL Developer 11.0版本,但每次启动PL/SQL Developer都会自动弹出Logon窗口,并且选中其中的登录历 ...

  5. iOS 企业证书发布app 流程

    企业发布app的 过程比app store 发布的简单多了,没那么多的要求,哈 但是整个工程的要求还是一样,比如各种像素的icon啊 命名规范啊等等. 下面是具体的流程 1.修改你的 bundle i ...

  6. debian+apache+acme_tiny+lets-encrypt配置笔记

    需要预先将需要申请ssl的域名指向到服务器,此方法完全通过api实现,好处是绿色无污染,不需要注册账号,不会泄露私人信息环境为 debian7+apache apt-get install apach ...

  7. shell来start、stop、restart应用程序模板

    这里使用shell中的case语法: case分支语句格式如下: case $变量名 in 模式1) 命令列表 ;; 模式2) 命令列表 ;; *) ;; esac case行尾必须为单词“in”,每 ...

  8. Ubuntu 13.04 双显卡安装NVIDIA GT 630M驱动

    [日期:2013-05-24]   Linux系统:Ubuntu 13.04 安装 bumblebee 以管理双显卡,下面命令会自动安装NVIDIA显卡驱动 sudo add-apt-reposito ...

  9. Discuz & UCenter 修改手记 - 2014.12.19

    最近在整JAVA和UCENTER的东西,受限于项目架构需要,无法完全以UCENTER为中心,所以在对接过程中遇到了许多不愉快的事情.经历多番研究,终于解决了其中了两个大问题,现记录下来,以备日后查看. ...

  10. 【转】Oracle RAC 环境下的连接管理

    文章转自:http://www.oracle.com/technetwork/cn/articles/database-performance/oracle-rac-connection-mgmt-1 ...