コード例 #1
0
        private void loadPreview(string pageUrl, PreviewType type)
        {
            string htmlCode = new WebClient().DownloadString(pageUrl);
            string title = FormatString(Regex.Match(htmlCode, @"\<title\>([^>]+)\<\/title\>").Groups[1].Value);
            string imageUrl = "";

            switch (type)
            {
                case PreviewType.Youtube:
                    imageUrl = YouTubePreview(pageUrl);
                    break;
                case PreviewType.Viemo:
                    imageUrl = ViemoPreview(pageUrl);
                    break;
                case PreviewType.DailyMotion:
                    imageUrl = DailyMotionPreview(pageUrl);
                    break;
                case PreviewType.Flickr:
                    imageUrl = FlickrPreview(pageUrl);
                    break;
                //case PreviewType.Picasa:
                //    ImageUrl = PicasaPreview(pageUrl);
                //    break;
                case PreviewType.JPEG:
                case PreviewType.PNG:
                case PreviewType.BMP:
                case PreviewType.GIF:
                    imageUrl = ImagePreview(pageUrl);
                    break;
            }

            Dispatcher.BeginInvoke(new Action(delegate()
                {
                    Hyperlink hyperlink = new Hyperlink();
                    hyperlink.NavigateUri = new Uri(pageUrl);
                    hyperlink.Inlines.Add((title == String.Empty) ? pageUrl.Split('/').Last() : title);
                    hyperlink.ToolTip = ("Go to: " + pageUrl);
                    hyperlink.Click += hyperlink_Click;
                    ContextMenu contextMenu = new ContextMenu();
                    MenuItemWithLink internalBrowser = new MenuItemWithLink();
                    internalBrowser.hyperlink = hyperlink;
                    internalBrowser.Header = "Internal Browser";
                    internalBrowser.Click += internalBrowser_Click;
                    MenuItemWithLink externalBrowser = new MenuItemWithLink();
                    externalBrowser.hyperlink = hyperlink;
                    externalBrowser.Header = "External Browser";
                    externalBrowser.Click += externalBrowser_Click;
                    contextMenu.Items.Add(internalBrowser);
                    contextMenu.Items.Add(externalBrowser);
                    hyperlink.ContextMenu = contextMenu;

                    PreviewTitle.Inlines.Add(hyperlink);
                    PreviewImage.Source = new BitmapImage(new Uri(imageUrl));
                }));
        }
コード例 #2
0
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="post">Post to be showed by this interface.</param>
        internal UIPost(WPost post, Boolean enableProfileRedirect)
        {
            Contract.Requires(post != null);

            InitializeComponent();
            _post = post;
            _enableProfileRedirect = enableProfileRedirect;

            if (String.IsNullOrEmpty(_post.User.Avatar))
                AvatarImage.InternalImage.Source = new BitmapImage(new Uri("pack://*****:*****@"http"))
                {
                    //last element of words array is always "http:/…"
                    if (!words[i].Equals("http:/…"))
                    {
                        Hyperlink hyperlink = new Hyperlink();
                        hyperlink.Inlines.Add(words[i]);
                        hyperlink.NavigateUri = new Uri(words[i]);
                        hyperlink.ToolTip = ("Go to: " + words[i]);
                        hyperlink.Click += hyperlink_Click;
                        ContextMenu contextMenu = new ContextMenu();
                        MenuItemWithLink internalBrowser = new MenuItemWithLink();
                        internalBrowser.hyperlink = hyperlink;
                        internalBrowser.Header = "Internal Browser";
                        internalBrowser.Click += internalBrowser_Click;
                        MenuItemWithLink externalBrowser = new MenuItemWithLink();
                        externalBrowser.hyperlink = hyperlink;
                        externalBrowser.Header = "External Browser";
                        externalBrowser.Click += externalBrowser_Click;
                        contextMenu.Items.Add(internalBrowser);
                        contextMenu.Items.Add(externalBrowser);
                        hyperlink.ContextMenu = contextMenu;
                        TextTextBlock.Inlines.Add(hyperlink);
                        TextTextBlock.Inlines.Add(" ");
                    }
                    string link = words[i];

                    ThreadStart starter = delegate
                    {
                        try
                        {
                            checkMediaContent(link);
                        }
                        catch (Exception ex)
                        {
                            log.Error(ex.Message, ex);
                        }
                    };

                    Thread previewThread = new Thread(starter);
                    previewThread.Name = "Preview";
                    previewThread.Start();
                }
                else
                {
                    TextTextBlock.Inlines.Add(words[i]);
                    TextTextBlock.Inlines.Add(" ");
                }
            }

            TimeSpan difference = DateTime.UtcNow - _post.CreateAt;

            if (DateTime.Now.Hour < _post.CreateAt.Hour)
                difference += new TimeSpan(1, 0, 0, 0);

            if (difference.Days > 30)
                TimeSourceTextBlock.Text = "more than a month ago";
            else if (difference.Days > 1 && difference.Days <= 30)
                TimeSourceTextBlock.Text = "about " + difference.Days + " days ago";
            else if (difference.Days == 1)
                TimeSourceTextBlock.Text = "about a day ago";
            else
            {
                if (difference.Hours > 1)
                    TimeSourceTextBlock.Text = "about " + difference.Hours + " hours ago";
                else if (difference.Hours == 1)
                    TimeSourceTextBlock.Text = "about an hour ago";
                else
                {
                    if (difference.Minutes > 1)
                        TimeSourceTextBlock.Text = "about " + difference.Minutes + " minutes ago";
                    else if (difference.Minutes == 1)
                        TimeSourceTextBlock.Text = "about a minute ago";
                    else
                        TimeSourceTextBlock.Text = "a few seconds ago";
                }
            }

            TimeSourceTextBlock.Text += " from " + _post.Service.Name;
        }