{"id":6707,"date":"2012-10-19T08:56:14","date_gmt":"2012-10-19T07:56:14","guid":{"rendered":"http:\/\/www.itwriting.com\/blog\/?p=6707"},"modified":"2012-10-19T08:56:14","modified_gmt":"2012-10-19T07:56:14","slug":"hands-on-windows-8-development-twitter-and-hyperlink-hassles","status":"publish","type":"post","link":"https:\/\/www.itwriting.com\/blog\/6707-hands-on-windows-8-development-twitter-and-hyperlink-hassles.html","title":{"rendered":"Hands on Windows 8 development: Twitter and hyperlink hassles"},"content":{"rendered":"<p>I have been messing around with a Windows 8 app to present content from ITWriting.com in an app, mainly as a learning exercise. I came up with the idea of showing recent tweets on the main page of the app. Like this:<\/p>\n<p><a href=\"http:\/\/www.itwriting.com\/blog\/wp-content\/uploads\/2012\/10\/image18.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" style=\"border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px\" border=\"0\" alt=\"image\" src=\"http:\/\/www.itwriting.com\/blog\/wp-content\/uploads\/2012\/10\/image_thumb18.png\" width=\"404\" height=\"181\" \/><\/a><\/p>\n<p>I thought this would be easy, but encountered several problems. I am developing in XAML and C#; this aspect would probably be easier in HTML.<\/p>\n<p>The first problem: retrieving the tweets. The Twitter REST API version 1.1 has <a href=\"https:\/\/dev.twitter.com\/docs\/api\/1.1\/get\/statuses\/user_timeline\" target=\"_blank\">GET statuses\/user_timeline<\/a> which does what I want, except that it requires \u201cuser context\u201d, in other words a Twitter log-in. That is an unacceptable requirement for a user simply viewing my tweets, rather than their own timeline. <\/p>\n<p>The deprecated&#160; Twitter <a href=\"https:\/\/dev.twitter.com\/docs\/faq#11716\" target=\"_blank\">RSS API<\/a> on the other hand is perfect. Unfortunately:<\/p>\n<blockquote>\n<p>Please note that there is no support for the RSS response format in API v1.1. Properly versioned API v1 URLs will cease functioning in March 2013<\/p>\n<\/blockquote>\n<p>Never mind, it will do for the moment. I created a Twitter data source which retrieves the tweets as RSS. In my XAML I have a ListView which is bound to this data source. This ListView has an ItemTemplate which defines what appears in the list. I added a TweetItemTemplate in the Resources section of the XAML which displays each tweet in a TextBlock. So far so good.<\/p>\n<p><a href=\"http:\/\/www.itwriting.com\/blog\/wp-content\/uploads\/2012\/10\/image19.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" style=\"border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px\" border=\"0\" alt=\"image\" src=\"http:\/\/www.itwriting.com\/blog\/wp-content\/uploads\/2012\/10\/image_thumb19.png\" width=\"404\" height=\"187\" \/><\/a><\/p>\n<p>No hyperlinks though \u2013 they are dead. What is the use of a tweet without hyperlinks? Not much. I thought of a hack which would let you click or tap an entire tweet, look to see if a hyperlink is there, and then launch it. Ugly, and would only work for one hyperlink per tweet.<\/p>\n<p>TextBlock does not support hyperlinks. However there is a way to do this using RichTextBlock. This supports a collection of inline elements. You can have a Run element containing text, then an InlineUIContainer containing a HyperLinkButton, then another Run element and so on. The Hyperlink will be out of alignment, as shown <a href=\"http:\/\/social.msdn.microsoft.com\/Forums\/en-US\/winappswithcsharp\/thread\/dc9bdac4-9b57-4e21-aadc-25c06f2efdc8\" target=\"_blank\">here<\/a>, but you can fix that by tweaking margins and padding.<\/p>\n<p>Of course, this approach does mean parsing the tweet to extract the URLs and then building the RichTextBlock content. So in place of my simple TextBlock binding I now have this:<\/p>\n<blockquote>\n<p>&#160; &lt;ContentControl Content=&quot;{Binding Path=Title,Converter={StaticResource tweetToBlocks}}&quot;&gt;&lt;\/ContentControl&gt;<\/p>\n<\/blockquote>\n<p>I have also written a converter class which takes the bound value, builds the RichTextBlock in C#, and returns it. This gets you the result in the first image in this post. Not too bad, and the links work.<\/p>\n<p>What is annoying though is that the mouse pointer does not change to a hand icon when you hover over the link. I thought I could fix this by subclassing HyperLinkButton and adding code to change the cursor on the PointerEntered event:<\/p>\n<blockquote>\n<p>Window.Current.CoreWindow.PointerCursor = new Windows.UI.Core.CoreCursor(Windows.UI.Core.CoreCursorType.Hand, 1);<\/p>\n<\/blockquote>\n<p>This does not work. At least, you can see a flash as the cursor tries to change, but it is overridden by the RichTextBlock which changes it back to a text select cursor. I have not found a way round this yet.<\/p>\n<p>I then tried another approach. You can use a RichEditBox which <strong>does<\/strong> support links. The approach is different; you set the text of the Document property and then use the Link method to assign a link to a TextRange within it. It works; but I was frustrated to find that the mouse pointer still does not change to a hand when over the link. The RichTextBlock actually works just as well and is more integrated with XAML.<\/p>\n<p><a href=\"http:\/\/www.itwriting.com\/blog\/wp-content\/uploads\/2012\/10\/image20.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" style=\"border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px\" border=\"0\" alt=\"image\" src=\"http:\/\/www.itwriting.com\/blog\/wp-content\/uploads\/2012\/10\/image_thumb20.png\" width=\"404\" height=\"177\" \/><\/a><\/p>\n<p>I am sure you could fix this by using a WebView \u2013 embedded IE \u2013 for each tweet, but that seems to me an unduly heavyweight approach. Better perhaps would be a single web view showing all the tweets, which I might try when I have a moment.<\/p>\n<p>Even so, I was surprised how tricky it is to show tweets with hyperlinks in a ListView.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have been messing around with a Windows 8 app to present content from ITWriting.com in an app, mainly as a learning exercise. I came up with the idea of showing recent tweets on the main page of the app. Like this: I thought this would be easy, but encountered several problems. I am developing &hellip; <a href=\"https:\/\/www.itwriting.com\/blog\/6707-hands-on-windows-8-development-twitter-and-hyperlink-hassles.html\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Hands on Windows 8 development: Twitter and hyperlink hassles<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,80,94,97],"tags":[236,580,999,1045],"class_list":["post-6707","post","type-post","status-publish","format-standard","hentry","category-net","category-software-development","category-visual-studio","category-windows","tag-c","tag-metro","tag-windows-8","tag-xaml"],"_links":{"self":[{"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/posts\/6707","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/comments?post=6707"}],"version-history":[{"count":0,"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/posts\/6707\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/media?parent=6707"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/categories?post=6707"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/tags?post=6707"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}