zoukankan      html  css  js  c++  java
  • 【Win10】一些零碎不好归档的小总结(原谅我这个该死的标题吧)

    一、同步方式获取设备的屏幕分辨率

    public static class ScreenResolution
    {
        /// <summary>
        /// 获取屏幕高度。
        /// </summary>
        public static int Height
        {
            get
            {
                var rect = PointerDevice.GetPointerDevices().Last().ScreenRect;
                var scale = DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel;
                return (int)(rect.Height * scale);
            }
        }
    
        /// <summary>
        /// 获取屏幕宽度。
        /// </summary>
        public static int Width
        {
            get
            {
                var rect = PointerDevice.GetPointerDevices().Last().ScreenRect;
                var scale = DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel;
                return (int)(rect.Width * scale);
            }
        }
    }

    最初思路来源:http://bbs.wfun.com/thread-860192-1-1.html

    测试结果:

    Desktop(device Win10 10240)
    Correct Value:1920x1080
    Test Result:1920x1080

    Mobile(emulator W10M 10240 WXGA 4.5 inch)
    Correct Value:768x1280
    Test Result:768x1280

    Mobile(emulator W10M 10240 WVGA 4 inch)
    Correct Value:480x800
    Test Result:480x854

    只有最后一个是不相等的,个人认为是因为虚拟导航栏的原因,因此我觉得该代码理论上是没有问题的,可以投入生产环境。

    当然如果不放心的话也可以使用 Win32 API 来获取屏幕分辨率。(已验证此 API 不能在商店应用包中使用,谢谢 @flashcancer 指出)

    [DllImport("user32.dll")]
    public static extern int GetSystemMetrics(SystemMetric smIndex);
    int x = GetSystemMetrics(SystemMetric.SM_CXSCREEN);
    int y = GetSystemMetrics(SystemMetric.SM_CYSCREEN);

    SystemMetric 是一个枚举类型,定义可以参见:http://pinvoke.net/default.aspx/Enums.SystemMetric

    获取出来的结果跟上面 Test Result 是一样的,并且该 API 在 desktop 和 mobile 都可以用。(然而商店不能用,所以然并卵)

    商店中允许使用的 Win32 API 一览:https://msdn.microsoft.com/zh-cn/library/windows/apps/jj662956(v=vs.105).aspx(该链接是 WP8 的,但 Win10 应该也差不多)

    二、运行时访问 Package.appxmanifest 文件

    有些数据是写在 Package.appxmanifest 文件里的,例如磁贴用的图片、启动屏幕用的图片,运行时获取这些信息就只能访问 Package.appxmanifest 文件了。

    string manifestPath = Path.Combine(Package.Current.InstalledLocation.Path, "AppxManifest.xml");
    XDocument document = XDocument.Load(manifestPath);

    编译后,解包我们得知是会变成 AppxManifest.xml 文件,接下来用 XDocument 或者 XmlDocument 解释就可以了。

    manifest 的 schema 文档地址:https://msdn.microsoft.com/zh-cn/library/windows/apps/dn934760.aspx

    三、获取 GB2312 Encoding

    以前 WP8.1 没记错是得到 codeplex 上找这个 Encoding 的,现在不用了。

    // 这两句执行一次就可以了,当然重复执行也没坏的。
    EncodingProvider provider = CodePagesEncodingProvider.Instance;
    Encoding.RegisterProvider(provider);
    
    Encoding gb2312 = Encoding.GetEncoding("gb2312");
    
    // 抛出 ArgumentException
    // Encoding doge = Encoding.GetEncoding("doge");

    前两句可以放在 App.cs(当然某些人手贱将入口点改成别的也没大问题)里。执行一次就已经可以了。接下来就可以获取 Encoding 了,当然前提是你机子上有这个 Encoding,没有的话就像上面 doge 那样抛出一个 ArgumentException。

    四、获取主题色(AccentColor)

    public static Color AccentColor
    {
        get
        {
            return new UISettings().GetColorValue(UIColorType.Accent);
        }
    }

    UIColorType 是一个枚举类型,里面还有 Background 的颜色之类的。

    当然继续沿用 WP8.1 的方法也可,通过 Resource 来获取,只不过 Win10 里的 ResourceKey 变了。

    2015/9/23 更新:

    上面该方式仅能在 Desktop 下使用,Mobile 会抛异常。

    public static Color AccentColor
    {
        get
        {
            return (Color)Application.Current.Resources["SystemAccentColor"];
        }
    }

    所以还是用回 Resource 来获取吧。。。

    五、App.cs 里的 Launched 和 Actived 有啥区别

    一图流不解释。

    应用生命周期Ex

    之前周末休息整理的,不整理不知道,原来这么乱。话说好久没用 Paint.Net 有点不习惯。

    此图乃个人整理,请勿当真,如有错漏,请在评论指出。


    最后希望工作的面试能过,顺利拿到 offer,然后早点交接完辞掉现在这份好好休息几天(最近感觉有点累)。

  • 相关阅读:
    管道命令'|' 和xargs find命令找到后把所有找到的删除
    UVa
    【手势交互】9. PS Move
    jquery时间格式化插件
    Android学习路线(十三)Activity生命周期——暂停和恢复(Pausing and Resuming )一个Activity
    hdu 2604 Queuing (矩阵高速幂)
    【Linux驱动】TQ2440 DM9000E网卡驱动移植(Linux-2.6.30.4)
    bzoj2648 SJY摆棋子
    Hive编程指南_学习笔记01
    通信协议中的转义符
  • 原文地址:https://www.cnblogs.com/h82258652/p/4817932.html
Copyright © 2011-2022 走看看