设为首页 收藏本站
查看: 2439|回复: 0

重新想象 Windows 8 Store Apps (69)

[复制链接]

尚未签到

发表于 2015-5-23 06:50:20 | 显示全部楼层 |阅读模式
  [源码下载]




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

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


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


















  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(1000);
// 关掉自定义启动屏幕,跳转到程序主页面
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













  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












  Feature/MessageDialogDemo.xaml.cs



/*
* MessageDialog - 信息对话框
*     Content - 内容
*     Title - 标题
*     Options - 选项(Windows.UI.Popups.MessageDialogOptions 枚举)
*         None - 正常,默认值
*         AcceptUserInputAfterDelay - 为避免用户误操作,弹出对话框后短时间内禁止单击命令按钮
*     Commands - 命令按钮集合,返回 IList 类型的数据
*     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 = 0; // 按“enter”键后,激发第 1 个命令
messageDialog.CancelCommandIndex = 2; // 按“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








右键我或press-and-hold我,以弹出 PopupMenu




  Feature/PopupMenuDemo.xaml.cs



/*
* PopupMenu - 上下文菜单
*     Commands - 命令按钮集合,返回 IList 类型的数据
*     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
[源码下载]

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-69656-1-1.html 上篇帖子: Windows 8 文件关联和程序合约 下篇帖子: 与众不同 windows phone (8)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表