Edi Wang

.NET and Azure Developer

Windows Development UWP, WPF, WinForm, etc

Windows 10 IoT开发:UWP应用调用关机和重启命令

UWP通常是没有让设备关机和重启的权限的。但是在Windows 10 IoT Core中是有办法实现的。安装Windows 10 Iot Core之后默认启动的那个欢迎页面右上角是有关机和重启按钮的。其实就是个UWP,它的源代码在:https://github.com/ms-iot/samples/tree/develop/IoTCoreDefaultApp  分析源代码可以发现,关机和重启是通过ShutDownManager这个class实现的。 #region Assembly Windows.System.SystemManagementContract, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null, ContentType=WindowsRuntime // C:\Program Files (x86)\ …
Windows UWP IoT

Azure Remote Controlled Light with Windows 10 IoT Core

For those who have watched the "Big Bang Theory", you would probably remember that those physicists build an Internet controlled lamp. The signal send from their laptop traveled around the world and come back to their house to light up the lamp. Now, with Windows 10 IoT Core and Microsoft Azure, we could also achieve the same thing on a Raspberry Pi 3. First, I strongly recommend you to read …
Windows Azure IoT Raspberry Pi

Windows 10 UWP开发:如何不让界面卡死

自从.NET 4.5开始,C#多了一对新的异步关键词 async 和 await,如果不了解的朋友可以简单的看下下面的示意图。 简单的说,就是在通常情况下,用户在界面上进行的操作,比如点击一个按钮之后,如果进行大量的计算,或者读写文件、网络请求等耗时的操作,那么程序的界面就会卡住,在这段时间里,任何交互都不会响应,直到后台的代码执行完毕才会继续响应用户操作。 这种现象和你的应用是不是UWP没有什么卵关系,WinForm,WPF,Windows Phone SL/RT应用都会有这个问题。 这个问题的原因是因为这些耗时的操作和界面(UI线程)没有关系,但却在UI线程上执行了。解决办法就是另起一个线程,让它在UI线程之外去执行代码,这样就不会锁死UI。就像这图: 在.NET 4.5以前,要进行一个异步操作写法有点繁琐,.NET 4.5以后。我们只要把一个方法的签名声明为 …
Windows Async Async Await UWP

Windows 10 UWP开发:页面导航及后退的处理技巧

这篇文章主要总结一下Windows 10 UWP开发中关于页面导航及后退按钮的一些省时省力的技巧。 一、页面导航 导航到另一个页面,用的是Frame的Navigate方法,parameter参数是可选的: public System.Boolean Navigate(Type sourcePageType, System.Object parameter); 比如你的目标页面是TargetPage.xaml,通常类型就是TargetPage,所以就可以这样搞: Frame.Navigate(typeof(TargetPage), ParametersObject); 在目标页面接受参数,用的是OnNavigatedTo,然后把e.Parameter转换成你的参数类型,再自己撸一下就好,比如: protected override void OnNavigatedTo( …
Windows UWP

How to Make Circular Avatar in Windows 10 UWP

Once upon a time, all user's avatar used to be square. Since Apple's design, now a lot of apps and websites would use a circular avatar instead of a squre one. This is how we used to make square avatar: Starting from Windows 10, Microsoft became the biggist Apple fans, so they changed avatars to circular in Windows system. So how can we make one in XAML without acutally cropping the …
Windows UWP

Windows 10 UWP开发:支持异步的全局异常处理和堆栈信息

我们写UWP应用的时候难免遇到未处理的异常,不然你的应用就会在用户面前闪退,非常没有逼格。然而Windows.UI.Xaml.Application的UnhandledException事件里面有个巨坑,就是它不能处理async异步方法里的异常。注释里也没提到这回事: // // Summary: // Occurs when an exception can be handled by app code, as forwarded from a native-level // Windows Runtime error. Apps can mark the occurrence as handled in event data. public event UnhandledExceptionEventHandler UnhandledException; 处理全局异常确实 …
Windows Async Exception UWP

Windows 10 UWP开发:报错和反馈页面的实现

我的《上海轨道交通》应用里有个允许用户报告错误的页面,会把当前视图(xaml页面名称)、页面摘要、用户反馈的内容以及设备的软硬件信息生成邮件发给应用作者,界面如下: 生成的邮件如下: 问题描述:测试 (程序版本:3.2.0.0, 所在页面:StationDetail, 页面摘要:宜山路, 设备名:ISAAC, 操作系统:WINDOWS, SKU:Surface_Pro_3, 产品名称:Surface Pro 3, 制造商:Microsoft Corporation, 固件版本:, 硬件版本:) 要实现这样的报错页面非常容易 1.获取软硬件信息 我们要用的类是: Windows.Security.ExchangeActiveSyncProvisioning.EasClientDeviceInformation 比如获得设备名称,就可以直接: var deviceInfo = new …
Windows UWP

Windows 10 UWP开发:如何本地化APP名称

我们在设计APP的时候,可能会考虑到不同国家和地区的用户使用,并在界面里针对不同的语言写资源文件做本地化。但是,如何让你的APP名称也本地化?比如在简体中文的系统上显示在程序列表里的是“调色板”,而在英文系统上显示的是“Color Palette”。 做法很简单,首先,和通常做本地化一样,你得有个"Strings\语言\Resources.resw"的文件夹结构。比如简体中文用的是“zh-CN”,美国英语是“en-US” 然后分别在resw文件里加一个key,比如AppName,然后针对不同语言填写不同的名称。这里简体中文是“调色板”,英语是“Color Palette” 最后这部最关键,打开你的Package.appxmanifest文件,把Display Name改成: ms-resource:AppName 现在,部署你的APP,就能看到不同的结果了。 补充:如果要让应用商店根 …
Windows UWP

How to save image to file in Windows 10 UWP

My UWP application "Shanghai Metro" has an option to allow user save the metro graph to local file system. The image file is located under project directory, "Assets\shanghaimetro-xl.jpg", and will ask the user to choose a location when saving the image. This is a very common functionality, it's also easy to achieve in UWP applications. The most easy way to do it is to use my library "Edi.UWP. …
Windows UWP

Windows 10 UWP开发:如何定时触发后台任务

今天在爆UWP的定时后台任务,坑有点多,爆出屎来了。有的坑在很多网上的文章里都没提到,非常的坑。刚刚开荒成功了,把经验写出来分享: 1. 写一个后台任务的类,继承IBackgroundTask接口 通常,在设计应用程序结构的时候,我们会建类库项目(Class Library)放这些类。比如 FarkBackgroundTask.Core 因为是UWP工程,所以建的类库也要是Universal Windows的。注意,这里我们已经埋下了一个巨坑,稍后会解释。 我们的类代码如下: public class SayFarkTask : IBackgroundTask { public void Run(IBackgroundTaskInstance taskInstance) { Debug.Write("================ Fark the …
Windows Timer UWP Task