iT邦幫忙

0

Android 算法:遍歷巡覽ViewGroup找出所有子View[C# 以Xml方法3種實作] GetElementsByTagName、SelectNodes

緣起
全部程式碼
實境秀

Yes

感恩感恩 讚歎讚歎 南無阿彌陀佛


//用Xml提供的方法來遍歷巡覽View https://docs.microsoft.com/zh-tw/dotnet/api/system.xml.xmlnode.selectnodes?view=netcore-3.1
        void traverseViewGroup_Xml_Enabled(chooseAXmlMethodforTraverseViewGroup c, bool setEnabled)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(Application.Resources.GetLayout(Resource.Layout.activity_game));
            XmlElement xmlElement = doc.DocumentElement;
            XmlNodeList xmlNodeList = doc.ChildNodes;
            switch (c)
            {
                case chooseAXmlMethodforTraverseViewGroup.GetElementsByTagName:
                    xmlNodeList = doc.GetElementsByTagName("Button");
                    break;
                case chooseAXmlMethodforTraverseViewGroup.SelectNodes:
                    xmlNodeList = xmlElement.SelectNodes("//Button");//case-sensitive
                    break;
                case chooseAXmlMethodforTraverseViewGroup.SelectNodes_XmlNamespaceManager:
                    XmlNamespaceManager xmlNamespaceManager = new XmlNamespaceManager(doc.NameTable);
                    xmlNamespaceManager.AddNamespace("android", "http://schemas.android.com/apk/res/android");
                    xmlNodeList = xmlElement.SelectNodes("//Button", xmlNamespaceManager);
                    break;
                default:
                    break;
            }
            if (xmlNodeList.Count != doc.ChildNodes.Count)//因為以doc.ChildNodes初始化故
            {
                foreach (XmlNode item in xmlNodeList)
                {
                    XmlAttribute attributeContentDescription = item.Attributes["p0:contentDescription"];
                    if (attributeContentDescription != null)//具有指定名稱的屬性。 如果屬性 (attribute) 不存在,這個屬性 (property) 會傳回 null。https://docs.microsoft.com/zh-tw/dotnet/api/system.xml.xmlattributecollection.itemof?view=netcore-3.1#System_Xml_XmlAttributeCollection_ItemOf_System_String_
                    {
                        string valueAttributeContentDescription = attributeContentDescription.Value;
                        if (valueAttributeContentDescription != null &&
                            valueAttributeContentDescription.Contains//如果是數字按鈕
                                (Convert.ToString(Resource.String.buttonNum)))//因屬性值前有1個「@」
                        {
                            FindViewById<Button>(int.Parse(item.Attributes["p0:id"].Value.Substring
                                (1))).Enabled = setEnabled;
                        }
                    }
                }
            }
        }

  enum chooseAXmlMethodforTraverseViewGroup
        {
            GetElementsByTagName, SelectNodes, SelectNodes_XmlNamespaceManager
        }
        

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言