Mobile Zone is brought to you in partnership with:

Michael Crump is a Microsoft MVP, INETA Community Champion, and an author of several .NET Framework eBooks. He speaks at a variety of conferences and has written dozens of articles on .NET development. He currently works at Telerik with a focus on our XAML control suite. You can visit his blog at: MichaelCrump.Net or follow him on Twitter at: @mbcrump Michael is a DZone MVB and is not an employee of DZone and has posted 115 posts at DZone. You can read more from them at their website. View Full User Profile

Retrieving a WebPage Title and Sharing it with the ShareLink in Windows Phone

02.12.2012
| 1809 views |
  • submit to reddit

The ShareLinkTask introduced in Windows Phone SDK 7.1 enables users to share a link on various social networks like Twitter, Facebook and LinkedIn. The only thing that I wish that it had was a way to grab the page title for the link before posting. I found myself typing the page title and thought I’d write some simple code and share with everyone. Below is a snapshot of a post to Facebook that automatically added the page title to the post.

image


ShareLinkTask Intro

The following code is all you need to share links on Social Media Networks with WP SDK 7.1.

ShareLinkTask shareLinkTask = new ShareLinkTask();

shareLinkTask.Title = "Telerik";
shareLinkTask.LinkUri = new Uri("http://www.telerik.com", UriKind.Absolute);
shareLinkTask.Message = "Here are some great controls for Windows Phone 7.";

shareLinkTask.Show();


It will pop up a list of all the Social Media accounts that the user has registered on their device and after you select it then it will post automatically.




Instead of hard coding the title or message, we can add in the following code in and get the page title automatically.

WebClient wc = new WebClient();
wc.DownloadStringAsync(new Uri("http://www.telerik.com", UriKind.RelativeOrAbsolute));
wc.DownloadStringCompleted += (s, ev) =>
{
    strTitle = Regex.Match(ev.Result, @"\<title\b[^>]*\>\s*(?<Title>[\s\S]*?)\</title\>", RegexOptions.IgnoreCase).Groups["Title"].Value;
};

Wrap-up

Thanks for the help Regex! This was fairly painless process that only took a few minutes! If this post helped you or you know a better way then I’d love to hear from you in the comments below.


Source: http://michaelcrump.net/retrieving-a-webpage-title-and-sharing-it-with-the-sharelinktask-in-wp7

Published at DZone with permission of Michael Crump, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)