Пример #1
0
        public static Category ParseJson(JToken cat)
        {
            Category pc = new Category();

            pc.CategoryId = Json.TryGetJsonProperty(cat, "id");
            // NEW API will return these OK, old does not...
            // Debug.Assert(pc.CategoryId != null);

            pc.FullPath = Json.TryGetJsonProperty(cat, "fullpathname"); // old v1!
            pc.NodeName = Json.TryGetJsonProperty(cat, "name");

            pc.PluralName = Json.TryGetJsonProperty(cat, "pluralName"); // v2

            if (string.IsNullOrEmpty(pc.PluralName))
            {
                pc.PluralName = pc.NodeName;
            }

            var subcats = cat["categories"]; // v2, recursive
            if (subcats != null)
            {
                List<Category> sc = new List<Category>();
                foreach (var sub in subcats)
                {
                    if (sub != null)
                    {
                        Category c = Category.ParseJson(sub);
                        if (c != null)
                        {
                            sc.Add(c);
                        }
                    }
                }
                if (sc.Count > 0)
                {
                    pc._subs = sc;
                }
            }

            var parents = cat["parents"]; // old v1!
            if (parents != null)
            {
                // I wonder, HOW MULTIPLE PARENTS WOULD WORK.
                JArray prnts = (JArray) parents;
                foreach (var item in prnts)
                {
                    // V2 NOTE: THIS IS INCORRECT since its not really the full
                    // path, but instead the node.
                    pc.FullPath = item.ToString();
                    break;
                }
            }

            string primary = Json.TryGetJsonProperty(cat, "primary");
            if (primary != null && (primary == "true" || primary == "True"))
            {
                pc.IsPrimary = true;
            }

            // Since the isolated storage may still have older versions of the
            // data, this should use a fallback-mechanism.
            try
            {
                var icon = cat["icon"];
                if (icon != null)
                {
                    MultiResolutionImage mri = MultiResolutionImage.ParseJson(icon);
                    if (mri != null)
                    {
                        pc.MultiResolutionIcon = mri;
                    }
                }
            }
            catch (InvalidOperationException)
            {
                try
                {
                    string uri = Json.TryGetJsonProperty(cat, "icon");
                    if (uri != null)
                    {
                        pc._iconUri = new Uri(uri);
                    }
                }
                catch (Exception)
                {
                }
            }

            return pc;
        }
        private void SelectCategory(Category cc)
        {
            SelectedCategoryId = cc.CategoryId;
            SelectedCategoryName = cc.PluralName;

            Dispatcher.BeginInvoke(() => NavigationService.GoBackWhenReady());
        }