RSS Reader demonstrates how to use an ItemsControl to display an RSS Feed
Step 1
If not already, follow Setup and Start on how to Install and get Started with Visual Studio 2017 or in Windows 10 choose Start, and then from the Start Menu find and select Visual Studio 2017.
Step 2
Once Visual Studio Community 2017 has started, from the Menu choose File, then New then Project…
Step 3
From New Project choose Visual C# from Installed, Templates then choose Blank App (Universal Windows) and then type in a Name and select a Location and then select Ok to create the Project
Step 4
Then in New Universal Windows Project you need to select the Target Version this should be at least the Windows 10 Fall Creators Update (10.0; Build 16299) and the Minimum Version to be the same.
The Target Version will control what features your application can use in Windows 10 so by picking the most recent version you’ll be able to take advantage of those features. To make sure you always have the most recent version, in Visual Studio 2017 select Tools Extensions and Updates… then and then see if there are any Updates
Step 5
Once done select from the Menu, Project, then Add New Item…
Step 6
From the Add New Item window select Visual C#, then Code from Installed then select Code File from the list, then type in the Name as Library.cs before selecting Add to add the file to the Project
Step 7
Once in the Code View for Library.cs the following should be entered:
using System; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Input; using Windows.Web.Syndication; public class Library { SyndicationClient _client = new SyndicationClient(); SyndicationFeed _feed = new SyndicationFeed(); private async void Load(ItemsControl list, Uri uri) { _client = new SyndicationClient(); _feed = await _client.RetrieveFeedAsync(uri); list.ItemsSource = _feed.Items; } public void Go(ref ItemsControl list, string value, KeyRoutedEventArgs args) { if (args.Key == Windows.System.VirtualKey.Enter) { try { Load(list, new Uri(value)); list.Focus(FocusState.Keyboard); } catch { } } } }
In the Library.cs there are using statements to include the necessary functionality. There’s a SyndicationClient to use a SyndicationFeed with to retrieve an RSS feed. The Load Method uses these to get the RSS feed with RetrieveFeedAsync to set the ItemsSource of a ItemsControl to the feed contents. The Go method is triggered each time a keystroke is make and once this is an Enter keystroke then it will use the text from the TextBox to call the Load method.
Step 8
In the Solution Explorer select MainPage.xaml
Step 9
From the Menu choose View and then Designer
Step 10
The Design View will be displayed along with the XAML View and in this between the Grid and /Grid elements, enter the following XAML:
<Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <TextBox Grid.Row="0" Name="Value" Margin="20" VerticalAlignment="Center" KeyDown="Go_KeyDown"/> <ScrollViewer Grid.Row="1" Margin="20" BorderThickness="0"> <ItemsControl Name="Display"> <ItemsControl.ItemTemplate> <DataTemplate> <StackPanel> <HyperlinkButton NavigateUri="{Binding Path=Links[0].Uri}"> <HyperlinkButton.Content> <TextBlock TextWrapping="Wrap" Text="{Binding Path=Title.Text}"/> </HyperlinkButton.Content> </HyperlinkButton> <TextBlock Text="{Binding Path=PublishedDate}"/> </StackPanel> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> </ScrollViewer> </Grid>
The first block of XAML the main user interface of the Application, this features a Grid with a top row to display a TextBox which is where the RSS feed address can be typed in, followed by Enter to navigate and then there’s the ItemsControl itself in the second row.
Step 11
From the Menu choose View and then Code
Step 12
Once in the Code View, below the end of public MainPage() { … } the following Code should be entered:
Library library = new Library(); private void Go_KeyDown(object sender, KeyRoutedEventArgs e) { library.Go(ref Display, Value.Text, e); }
Below the MainPage() Method an instance of the Library Class is created. In the Go_KeyDown this passes through the text of the TextBox along with additional event information for the keystrokes.
Step 13
That completes the Universal Windows Platform Application so Save the Project then in Visual Studio select the Local Machine to run the Application
Step 14
After the Application has started running you can then type in the URL of any RSS feed then press or tap enter to load it.
Step 15
To Exit the Application select the Close button in the top right of the Application