Microsoft Visual Studio 2010 Express for Window Phone

Guardando i video del MIX mi è venuta voglia di provare a fare un piccolo programma in Window Phone per la lettura dei feed RSS. Così presi armi e bagagli mi sono documentato ed ecco qua in pochi click ho il mio lettore.

image

Ma è così difficile ? Non direi proprio anzi ( una goduria… ) Prima di tutto ho scaricato dal sito http://www.microsoft.com/express/phone/ la mia versione di Microsoft Visual Studio 2010 Express for Window Phone che contiene “solo” IExplorer ( la prima cosa che ho fatto è stata collegarmi a facebook ed inserire un post ). Poi al solito creo un nuovo progetto e mi si apre una fantastica schermata ( xaml a destra ed emulatore a sinistra ).

image

inizio ad inserire i miei Textbox, Button, Label come se fossi su un’applicazione Silverlight

Xaml

   1: <phoneNavigation:PhoneApplicationPage 
   2:     x:Class="WindowsPhoneApp.MainPage"
   3:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   4:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   5:     xmlns:phoneNavigation="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Navigation"
   6:     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
   7:     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
   8:     mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="800"
   9:     FontFamily="{StaticResource PhoneFontFamilyNormal}"
  10:     FontSize="{StaticResource PhoneFontSizeNormal}"
  11:     Foreground="{StaticResource PhoneForegroundBrush}">
  12:  
  13:     
  14:     <Grid x:Name="LayoutRoot" HorizontalAlignment="Stretch"
  15:           Background="{StaticResource PhoneBackgroundBrush}">
  16:         <Grid.RowDefinitions>
  17:             <RowDefinition Height="Auto"/>
  18:             <RowDefinition Height="Auto"/>
  19:             <RowDefinition Height="*"/>
  20:         </Grid.RowDefinitions>
  21:         
  22:         <Grid x:Name="TitleGrid" Grid.Row="0">
  23:             <TextBlock Text="MY APPLICATION" Grid.Row="0" x:Name="textBlockPageTitle" Style="{StaticResource PhoneTextPageTitle1Style}"/>
  24:             <TextBlock Text="RSS Reader" Grid.Row="0" x:Name="textBlockListTitle" Style="{StaticResource PhoneTextPageTitle2Style}"/>
  25:         </Grid>
  26:          <StackPanel Grid.Row="1" Orientation="Horizontal">
  27:             <TextBlock HorizontalAlignment="Left" Margin="10,30,10,10" Text="Url :" VerticalAlignment="Top" />
  28:             <TextBox HorizontalAlignment="Left" Text="http://blogs.ugidotnet.org/sonny/Rss.aspx" Width="310"
  29:                      Name="_tbUrl" VerticalAlignment="Top" HorizontalContentAlignment="Stretch"/>
  30:             <Button Content="Read" HorizontalAlignment="Left" Name="_bRead" VerticalAlignment="Top" Click="_bRead_Click"  />
  31:          </StackPanel>
  32:         <ListBox Grid.Row="2" HorizontalAlignment="Left" HorizontalContentAlignment="Stretch"
  33:                  Name="_lbRssItems" VerticalAlignment="Top">
  34:             <ListBox.ItemTemplate>
  35:                 <DataTemplate>
  36:                     <StackPanel Orientation="Horizontal" Height="132">
  37:                         <StackPanel Width="390">
  38:                             <TextBlock Text="{Binding Title}" Foreground="#FFC8AB14" FontSize="28" />
  39:                             <TextBlock Text="{Binding Description}" Height="100" TextWrapping="Wrap" FontSize="24" />
  40:                             <HyperlinkButton NavigateUri="{Binding Link}" Content="Apri Feed" />
  41:                         </StackPanel>
  42:                     </StackPanel>
  43:                 </DataTemplate>
  44:             </ListBox.ItemTemplate>
  45:  
  46:         </ListBox>
  47:     </Grid>
  48:     
  49: </phoneNavigation:PhoneApplicationPage>

Metto in ordine i miei Binding, creo la mia entità RSSItem

   1: public class RSSItem
   2:     {
   3:         public string Title { get; set; }
   4:         public string Link { get; set; }
   5:         public string Description { get; set; }
   6:     }

Ed inizio a scrivere nella mia classe MainPage

   1: public partial class MainPage : PhoneApplicationPage
   2:     {
   3:         public MainPage()
   4:         {
   5:             InitializeComponent();
   6:  
   7:             SupportedOrientations = SupportedPageOrientation.Portrait | SupportedPageOrientation.Landscape;
   8:         }
   9:  
  10:         private void _bRead_Click(object sender, RoutedEventArgs e)
  11:         {
  12:             GetRSSfile();
  13:         }
  14:  
  15:         private void GetRSSfile()
  16:         {
  17:             if (_tbUrl.Text == string.Empty) return;
  18:  
  19:             System.Uri uri = new Uri(_tbUrl.Text);
  20:             HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(uri);
  21:             httpWebRequest.AllowReadStreamBuffering = false;
  22:             IAsyncResult result = (IAsyncResult)httpWebRequest.BeginGetResponse(
  23:                                         new AsyncCallback(RespCallback), httpWebRequest);
  24:         }
  25:  
  26:         private void RespCallback(IAsyncResult asynchronousResult)
  27:         {            
  28:                 HttpWebRequest myHttpWebRequest2 = (HttpWebRequest)asynchronousResult.AsyncState;
  29:                 HttpWebResponse response = (HttpWebResponse)myHttpWebRequest2.EndGetResponse(asynchronousResult);
  30:  
  31:                 string text;
  32:                 using(StreamReader sr = new StreamReader(response.GetResponseStream()))
  33:                           text = sr.ReadToEnd();
  34:  
  35:                 LoadRSSItems(text);            
  36:         }
  37:  
  38:         private void LoadRSSItems(string text)
  39:         {
  40:             if (text == string.Empty) return;
  41:  
  42:             XElement element = XElement.Parse(text);
  43:  
  44:             var ents = from node in element.Descendants("channel").Descendants("item")
  45:                                         select new RSSItem
  46:                                         {
  47:                                             Title = node.Descendants("title").FirstOrDefault().Value,
  48:                                             Link = node.Descendants("link").FirstOrDefault().Value,
  49:                                             Description = HttpUtility.HtmlDecode(
  50:                                                 node.Descendants("description").FirstOrDefault().Value),
  51:                                         };
  52:  
  53:             Dispatcher.BeginInvoke(() => { _lbRssItems.ItemsSource = ents; });            
  54:         }
  55:     }

F5 e voilà parte l’emulatore

image

Certo, c’è sicuramente qualcosa da sistemare ( vedi la descrizione in html ) ma per mezz’ora di divertimento ne vale proprio la pena.

Sonny

posted @ sabato 27 marzo 2010 02:13

Print
Comments have been closed on this topic.