1. 程式人生 > >背水一戰 Windows 10 (122) - 其它: 通過 Windows.System.Profile 命名空間下的類獲取信息, 查找指定類或接口的所在程序集的所有子類和子接口

背水一戰 Windows 10 (122) - 其它: 通過 Windows.System.Profile 命名空間下的類獲取信息, 查找指定類或接口的所在程序集的所有子類和子接口

enter 轉換 local frame long windows 添加 roo schema

[源碼下載]


背水一戰 Windows 10 (122) - 其它: 通過 Windows.System.Profile 命名空間下的類獲取信息, 查找指定類或接口的所在程序集的所有子類和子接口



作者:webabcd


介紹
背水一戰 Windows 10 之 其它

  • 通過 Windows.System.Profile 命名空間下的類獲取信息
  • 查找指定類或接口的所在程序集的所有子類和子接口



示例
1、演示如何通過 Windows.System.Profile 命名空間下的類獲取信息
Information/ProfileInfo.xaml

<Page
    x:Class
="Windows10.Information.ProfileInfo" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:Windows10.Information" 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="10 0 10 10"> <TextBlock Name="lblMsg" TextWrapping="Wrap" Margin="0 10 10 10" /> </StackPanel> </Grid> </Page>

Information/ProfileInfo.xaml.cs

/*
* 演示如何通過 Windows.System.Profile 命名空間下的類獲取信息 * * 主要可獲取到設備類型,系統版本號等 */ using System; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.System.Profile; namespace Windows10.Information { public sealed partial class ProfileInfo : Page { public ProfileInfo() { this.InitializeComponent(); this.Loaded += ProfileInfo_Loaded; } private void ProfileInfo_Loaded(object sender, RoutedEventArgs e) { // 獲取設備類型,目前已知的返回字符串有:Windows.Mobile, Windows.Desktop, Windows.Xbox lblMsg.Text = string.Format("DeviceFamily: {0}", AnalyticsInfo.VersionInfo.DeviceFamily); lblMsg.Text += Environment.NewLine; // 獲取系統版本號,一個長整型值 lblMsg.Text += string.Format("DeviceFamilyVersion: {0}", AnalyticsInfo.VersionInfo.DeviceFamilyVersion); lblMsg.Text += Environment.NewLine; // 將長整型的系統版本號轉換為 major.minor.revision.build 的方式 string versionString = AnalyticsInfo.VersionInfo.DeviceFamilyVersion; ulong version = ulong.Parse(versionString); ulong v1 = (version & 0xFFFF000000000000L) >> 48; ulong v2 = (version & 0x0000FFFF00000000L) >> 32; ulong v3 = (version & 0x00000000FFFF0000L) >> 16; ulong v4 = (version & 0x000000000000FFFFL); string v = $"{v1}.{v2}.{v3}.{v4}"; lblMsg.Text += string.Format("DeviceFamilyVersion(major.minor.revision.build): {0}", v); lblMsg.Text += Environment.NewLine; // 獲取當前的“向 Microsoft 發送你的設備數據”的收集等級。在“設置”->“隱私”->“反饋和診斷”中配置(Security, Basic, Enhanced, Full) lblMsg.Text += string.Format("PlatformDiagnosticsAndUsageDataSettings.CollectionLevel: {0}", PlatformDiagnosticsAndUsageDataSettings.CollectionLevel); lblMsg.Text += Environment.NewLine; // 檢查當前配置是否允許指定級別的信息收集 lblMsg.Text += string.Format("PlatformDataCollectionLevel.Full: {0}", PlatformDiagnosticsAndUsageDataSettings.CanCollectDiagnostics(PlatformDataCollectionLevel.Full)); lblMsg.Text += Environment.NewLine; // 在“設置”->“隱私”->“反饋和診斷”中配置的“向 Microsoft 發送你的設備數據”發生變化時觸發的事件 PlatformDiagnosticsAndUsageDataSettings.CollectionLevelChanged += PlatformDiagnosticsAndUsageDataSettings_CollectionLevelChanged; } private async void PlatformDiagnosticsAndUsageDataSettings_CollectionLevelChanged(object sender, object e) { await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { lblMsg.Text += string.Format("PlatformDiagnosticsAndUsageDataSettings.CollectionLevel: {0}", PlatformDiagnosticsAndUsageDataSettings.CollectionLevel); lblMsg.Text += Environment.NewLine; lblMsg.Text += string.Format("PlatformDataCollectionLevel.Full: {0}", PlatformDiagnosticsAndUsageDataSettings.CanCollectDiagnostics(PlatformDataCollectionLevel.Full)); lblMsg.Text += Environment.NewLine; }); } } }


2、用於查找指定類或接口的所在程序集的所有子類和子接口
Tools/FindSubClass.xaml

<Page
    x:Class="Windows10.Tools.FindSubClass"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Windows10.Tools"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="Transparent">
        <ScrollViewer Margin="10 0 10 10">
            <StackPanel Name="root" Margin="5">

                <Button Name="btnFind" Content="查找指定類或接口的所在程序集的所有子類或子接口" Margin="1 5 1 20" Click="btnFind_Click" />

            </StackPanel>
        </ScrollViewer>
    </Grid>
</Page>

Tools/FindSubClass.xaml.cs

/*
 * 用於查找指定類或接口的所在程序集的所有子類和子接口
 */

using System;
using System.Reflection;
using System.Collections.Generic;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using System.Linq;

namespace Windows10.Tools
{
    public sealed partial class FindSubClass : Page
    {
        public FindSubClass()
        {
            this.InitializeComponent();
        }

        private void btnFind_Click(object sender, RoutedEventArgs e)
        {
            // 這樣不行
            // Type type = Type.GetType("Windows.UI.Xaml.Controls.Button");

            Type type = typeof(Windows.UI.Xaml.UIElement);

            List<Type> subTypes = GetSubTypes(type);
            AddWrapGrid(subTypes);
        }

        private void AddWrapGrid(List<Type> subTypes)
        {
            VariableSizedWrapGrid wrapGrid = CreateWrapGrid();
            root.Children.Add(wrapGrid);

            foreach (Type type in subTypes)
            {
                Button button = CreateButton(type);
                wrapGrid.Children.Add(button);
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            FrameworkElement button = sender as FrameworkElement;
            Type type = button.Tag as Type;

            int index = 0;
            // 刪除被選中按鈕的所屬容器,以及此容器之後的所有控件
            if (button.Parent.GetType() == typeof(VariableSizedWrapGrid))
                index = root.Children.IndexOf(button.Parent as UIElement);
            // 刪除被選中按鈕,以及此按鈕之後的所有控件
            else
                index = root.Children.IndexOf(button);
            while (root.Children.Count > index)
            {
                root.Children.RemoveAt(root.Children.Count - 1);
            }

            // 將被選中按鈕添加到根容器
            Button buttonNew = CreateButton(type);
            root.Children.Add(buttonNew);
            root.Children.Add(new Grid() { Height = 20 });

            // 將被選中類的所有子類添加到根容器
            List<Type> subTypes = GetSubTypes(type);
            AddWrapGrid(subTypes);
        }

        private VariableSizedWrapGrid CreateWrapGrid()
        {
            VariableSizedWrapGrid wrapGrid = new VariableSizedWrapGrid();
            wrapGrid.Orientation = Orientation.Vertical;
            wrapGrid.ItemWidth = 9999;
            wrapGrid.HorizontalAlignment = HorizontalAlignment.Stretch;

            return wrapGrid;
        }

        private Button CreateButton(Type type)
        {
            Button button = new Button();
            button.Content = type.ToString();
            button.Margin = new Thickness(1);
            button.Tag = type;
            button.Click += Button_Click;

            return button;
        }

        // 獲取兒子類,孫子及以下級別不會返回
        private List<Type> GetSubTypes(Type type)
        {
            List<Type> subTypes = new List<Type>();

            Type[] assemblyTypes = type.GetTypeInfo().Assembly.GetTypes();

            foreach (Type t in assemblyTypes)
            {
                if (type.GetTypeInfo().IsInterface)
                {
                    if (t.GetInterfaces().Contains(type))
                    {
                        subTypes.Add(t);
                    }
                }
                else 
                {
                    if (t.GetTypeInfo().BaseType == type)
                    {
                        subTypes.Add(t);
                    }
                }
            }

            subTypes = subTypes.OrderBy(p => p.FullName).ToList();

            return subTypes;
        }
    }
}



OK
[源碼下載]

背水一戰 Windows 10 (122) - 其它: 通過 Windows.System.Profile 命名空間下的類獲取信息, 查找指定類或接口的所在程序集的所有子類和子接口