public StoryCommentViewModel(string story_id, StoryExtra extra)
        {
            _story_id = story_id;
            _se = extra;

            Update();
        }
示例#2
0
        /// <summary>
        /// 获取文章额外信息
        /// </summary>
        /// <param name="story_id"></param>
        /// <returns></returns>
        public async Task<StoryExtra> GetStoryExtra(string story_id)
        {
            try
            {
                if (NetworkManager.Current.Network == 4)
                {
                    StoryExtra se = await FileHelper.Current.ReadObjectAsync<StoryExtra>(story_id + "_story_extra.json");
                    return se;
                }
                else
                {
                    string url = string.Format(ServiceURL.StoryExtra, story_id);
                    JsonObject json = await GetJson(url);
                    if (json != null)
                    {
                        string comments = json["comments"].GetNumber().ToString();
                        string long_comments = json["long_comments"].GetNumber().ToString();
                        string short_comments = json["short_comments"].GetNumber().ToString();
                        string popularity = json["popularity"].GetNumber().ToString();


                        bool favorite = DataShareManager.Current.FavoriteList.Contains(story_id) ? true : false;

                        StoryExtra tmp = new StoryExtra { Comments = comments, LongComments = long_comments, Polularity = popularity, ShortComments = short_comments, Favorite = favorite };
                        await FileHelper.Current.WriteObjectAsync<StoryExtra>(tmp, story_id + "_story_extra.json");

                        return tmp; 
                    }
                    else
                    {
                        return null;
                    }
                }
            }
            catch
            {
                return null;
            }
        }
        public async void Update()
        {
            IsLoading = true;

            var t1 = _api.GetStoryContent(_story_id);
            var t2 = _api.GetStoryExtra(_story_id);

            StoryContent sc = await t1;
            StoryExtra se = await t2;

            if (sc != null)
            {
                ID = sc.ID;
                Title = sc.Title;
                Image = sc.Image;
                ImageSource = sc.Image_Source;
                RecommenderAvatars = sc.RecommnderAvatars;
                ShareUrl = sc.Share_URL;
                string css = "<style>"
                        + "html{-ms-content-zooming:none;font-family:微软雅黑;}"
                        + ".author{font-weight:bold;} .bio{color:gray;}"
                        + "body{padding:20px;word-break:break-all;} p{margin:30px auto;} a{color:skyblue;} .content img{width:95%;}"
                        + "body{line-height:150%;}"
                        + "</style>";   //基础css
                string ex_mark = "<base target='_blank'/>";
                string css2 = "";   //主题css
                string css3 = "";   //字体css
                string js = "";   //图片加载脚本
                string body = "";

                if (DataShareManager.Current.APPTheme == Windows.UI.Xaml.ElementTheme.Dark) //夜间主题
                {
                    css2 = "<style>" 
                        + "body{background-color:black !important;color:gray !important;}"
                        + "</style>";
                }
                else
                {
                    css2 = "";
                }
                if (DataShareManager.Current.BigFont)  //大字号
                {
                    
                    css3 = "<style>body{font-size:52px;} h1{font-size:62px;} h2{font-size:58px;} h3{font-size:52px;} h4,h5,h6{font-size:48px;}</style>";
                }
                else
                {
                    css3 = "<style>body{font-size:44px;} h1{font-size:55px;} h2{font-size:50px;} h3{font-size:45px;} h4,h5,h6{font-size:40px;}</style>";
                }
                
                if (DataShareManager.Current.NOImagesMode)  //无图模式
                {
                    if (NetworkManager.Current.Network != 3)  //非wifi
                    {
                        body = Regex.Replace(sc.Body, @"<img.*?src=(['""]?)(?<url>[^'"" ]+)(?=\1)[^>]*>", (m) =>
                         {
                             if (m.Value.Contains("avatar"))
                             {
                                 return m.Value;
                             }
                             else
                             {
                                 Match match = Regex.Match(m.Value.ToString(), @"<img\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>");
                                 if (match.Success)
                                 {
                                     return @"<img src=""ms-appx-web:///Assets/default_image.png"" onclick=""click2loadimage(this,'" + match.Groups["imgUrl"].Value + @"');""/>";
                                 }
                                 else
                                 {
                                     return m.Value;
                                 }
                             }
                         }, RegexOptions.IgnoreCase);  //替换所有img标签 为本地图片

                        js = "<script>"  //点击加载图片
                            + "function click2loadimage(obj,source)"
                            + "{"
                            + "obj.setAttribute('src','ms-appx-web:///Assets/default_image_loading.png');"
                            + "obj.setAttribute('src',source);"
                            + "}"
                            + "</script>";
                    }
                    else
                    {
                        body = sc.Body;
                    }
                }
                else
                {
                    body = sc.Body;
                }
                // <link rel='stylesheet' type='text/css' href='" + sc.CSS + "'/> 官方css文件不好控制  所以没有使用 

                //合并
                BodyHtml = "<html><head>" + ex_mark + css + css2 + css3 + js + "</head>" + "<body>" +  body + "</body></html>";  //附加css
            }
            if (se != null)
            {
                Comments = se.Comments;
                Popularities = se.Polularity;
                Favorite = se.Favorite;
                _se = se;
            }

            IsLoading = false;
        }