Windows Phone:如何检查WMAppManifest中的Capability属性

原文: Windows Phone:如何检查WMAppManifest中的Capability属性

在Windows Phone应用中有一个应用程序清单(WMAppManifest.xml),其中对于不同的应用可以设定Capability来告知需要哪些特性或功能,详细内容可以参考官方文档: http://msdn.microsoft.com/zh-CN/library/windowsphone/develop/jj206936(v=vs.105).aspx

比如说,开发一个不需要麦克风的应用,就不需要ID_CAP_MICROPHONE标记,一般来说也都清理得差不多,但又有时候需要检查,比如我们在写一个触发震动的操作类库时候碰巧需要判定ID_CAP_SENSORS时该怎么办呢,微软没有提供相应的检查接口,所以只好自己来解决。

下面是针对于WP7的检查,WP8上也通用,只不过需要检查WP8自己特殊Capability,需要自己加一些代码。

/// <summary>

/// For Windows Phone 7

/// </summary>

static class CapabilityHelper

{

    private const string WMAppManifest = "WMAppManifest.xml";

    private const string ID_CAP_CONTACTS = "ID_CAP_CONTACTS";  

    private const string ID_CAP_NETWORKING = "ID_CAP_NETWORKING";  

    private const string ID_CAP_IDENTITY_DEVICE = "ID_CAP_IDENTITY_DEVICE";  

    private const string ID_CAP_IDENTITY_USER = "ID_CAP_IDENTITY_USER";  

    private const string ID_CAP_LOCATION = "ID_CAP_LOCATION";  

    private const string ID_CAP_SENSORS = "ID_CAP_SENSORS";  

    private const string ID_CAP_MICROPHONE = "ID_CAP_MICROPHONE";  

    private const string ID_CAP_MEDIALIB = "ID_CAP_MEDIALIB";  

    private const string ID_CAP_GAMERSERVICES = "ID_CAP_GAMERSERVICES";  

    private const string ID_CAP_PHONEDIALER = "ID_CAP_PHONEDIALER";  

    private const string ID_CAP_PUSH_NOTIFICATION = "ID_CAP_PUSH_NOTIFICATION";  

    private const string ID_CAP_WEBBROWSERCOMPONENT = "ID_CAP_WEBBROWSERCOMPONENT";  

    private const string CAPABILITIES = "Capabilities";  

    private const string NAME = "Name";  

  

    static CapabilityHelper()  

    {

        try

        {

            Check();

        }catch(Exception ex)

        {

            Console.WriteLine(ex.ToString());

        }

    }

    static void Check()

    {



        using (var xmlreader = XmlReader.Create(WMAppManifest))

        {

            while (xmlreader.Read())

            {

                switch (xmlreader.NodeType)

                {

                    case XmlNodeType.Element:

                        if (xmlreader.Name == "Capability")

                        {

                            var value = xmlreader.GetAttribute(NAME);

                            switch (value)

                            {

                                case ID_CAP_NETWORKING:

                                    IsNetworkingCapability = true;

                                    break;

                                case ID_CAP_IDENTITY_DEVICE:

                                    IsDeviceIdentityCapability = true;

                                    break;

                                case ID_CAP_IDENTITY_USER:

                                    IsUserIdentityCapability = true;

                                    break;

                                case ID_CAP_LOCATION:

                                    IsLocationCapability = true;

                                    break;

                                case ID_CAP_SENSORS:

                                    IsSensorsCapability = true;

                                    break;

                                case ID_CAP_MICROPHONE:

                                    IsMicrophoneCapability = true;

                                    break;

                                case ID_CAP_MEDIALIB:

                                    IsMediaLibCapability = true;

                                    break;

                                case ID_CAP_GAMERSERVICES:

                                    IsGamerServicesCapability = true;

                                    break;

                                case ID_CAP_PHONEDIALER:

                                    IsPhoneDialerCapability = true;

                                    break;

                                case ID_CAP_PUSH_NOTIFICATION:

                                    IsPushNotificationCapability = true;

                                    break;

                                case ID_CAP_WEBBROWSERCOMPONENT:

                                    IsWebBrowserComponentCapability = true;

                                    break;

                                case ID_CAP_CONTACTS:

                                    IsContactsCapability = true;

                                    break;

                                default:

                                    break;

                            }

                        }

                        break;

                    default:

                        break;

                }

            } 

        }  

    }

    public static bool IsNetworkingCapability { get;private set; }

    public static bool IsDeviceIdentityCapability { get; private set; }

    public static bool IsUserIdentityCapability { get; private set; }

    public static bool IsLocationCapability { get; private set; }

    public static bool IsSensorsCapability { get; private set; }

    public static bool IsMicrophoneCapability { get; private set; }

    public static bool IsMediaLibCapability { get; private set; }

    public static bool IsGamerServicesCapability { get; private set; }

    public static bool IsPhoneDialerCapability { get; private set; }

    public static bool IsPushNotificationCapability { get; private set; }

    public static bool IsWebBrowserComponentCapability { get; private set; }

    public static bool IsContactsCapability { get; private set; }

}

希望能够给一些朋友带来帮助。

 

你可能感兴趣的:(windows phone)