4

WPF 判断当前应用被打包为 UWP 而运行

 2 years ago
source link: https://lindexi.gitee.io/post/WPF-%E5%88%A4%E6%96%AD%E5%BD%93%E5%89%8D%E5%BA%94%E7%94%A8%E8%A2%AB%E6%89%93%E5%8C%85%E4%B8%BA-UWP-%E8%80%8C%E8%BF%90%E8%A1%8C.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
WPF 判断当前应用被打包为 UWP 而运行

本文告诉大家如何在应用运行过程判断自己的 WPF 应用被转制为 UWP 应用运行

通过 kernel32 的 GetCurrentPackageFullName 方法即可判断,此方法要求是在 Win10 或以上版本才能使用哦。当然了,如果在 Win10 以下的版本,如 Win7 那默认就跑不了 UWP 应用,也就不需要判断了

判断代码如下

        const long APPMODEL_ERROR_NO_PACKAGE = 15700L;

        [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
        static extern int GetCurrentPackageFullName(ref int packageFullNameLength, StringBuilder packageFullName);

        public bool IsRunningAsUwp()
        {
                int length = 0;
                StringBuilder sb = new StringBuilder(0);
                int result = GetCurrentPackageFullName(ref length, sb);

                sb = new StringBuilder(length);
                result = GetCurrentPackageFullName(ref length, sb);

                return result != APPMODEL_ERROR_NO_PACKAGE;
        }

在开始判断之前,先判断系统版本,代码如下

        public bool IsRunningAsUwp()
        {
            if (IsWindows7OrLower)
            {
                return false;
            }
            else
            {
                int length = 0;
                StringBuilder sb = new StringBuilder(0);
                int result = GetCurrentPackageFullName(ref length, sb);

                sb = new StringBuilder(length);
                result = GetCurrentPackageFullName(ref length, sb);

                return result != APPMODEL_ERROR_NO_PACKAGE;
            }
        }

        private bool IsWindows7OrLower
        {
            get
            {
                int versionMajor = Environment.OSVersion.Version.Major;
                int versionMinor = Environment.OSVersion.Version.Minor;
                double version = versionMajor + (double)versionMinor / 10;
                return version <= 6.1;
            }
        }

以上代码由微软提供,请看 microsoft/Windows-AppConsult-Tools-DesktopBridgeHelpers: Simple libraryto detect if a desktop application is running as classic Win32 or packaged with the Desktop Bridge

Call Windows Runtime APIs in desktop apps - Windows apps

Desktop Bridge – Identify the application’s context


本文会经常更新,请阅读原文: https://blog.lindexi.com/post/WPF-%E5%88%A4%E6%96%AD%E5%BD%93%E5%89%8D%E5%BA%94%E7%94%A8%E8%A2%AB%E6%89%93%E5%8C%85%E4%B8%BA-UWP-%E8%80%8C%E8%BF%90%E8%A1%8C.html ,以避免陈旧错误知识的误导,同时有更好的阅读体验。

如果你想持续阅读我的最新博客,请点击 RSS 订阅,推荐使用RSS Stalker订阅博客,或者前往 CSDN 关注我的主页

本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可。欢迎转载、使用、重新发布,但务必保留文章署名林德熙(包含链接: https://blog.lindexi.com ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请 与我联系

无盈利,不卖课,做纯粹的技术博客

以下是广告时间

推荐关注 Edi.Wang 的公众号
lindexi%2F201985113622445

欢迎进入 Eleven 老师组建的 .NET 社区
lindexi%2F20209121930471745.jpg

以上广告全是友情推广,无盈利


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK