<?xml version="1.0" encoding="utf-8"?><rss version="2.0"><channel><title>WinRT</title><link>http://www.ralbu.com:80/tags/winrt</link><description>WinRT</description><item><title>Building WinRT / Windows 8 Grouped items using MVVMLight</title><link>http://www.ralbu.com:80/post/2012/11/18/Building-WinRT-Grouped-items-using-MVVMLight</link><description>&lt;p&gt;One of the reasons to use MVVM pattern is the fact that you can design before having the application fully implemented. Tests are also an important part but this is not the topic of this article. When creating WinRT applications Blend is the tool you definitely should use.&amp;nbsp; When working with Grouped Items in GridView you don't have out of the box support for design using MVVM. In the following steps I will start with a new application using MvvmLight and add all the necessary bits for binding and displaying the data in UI.&lt;/p&gt;
&lt;p&gt;In order to work with MvvmLight you need to install it. Get a copy from here &lt;a href="http://mvvmlight.codeplex.com/"&gt;MvvmLight&lt;/a&gt; After installation it will add an MvvmLight project to your list of projects and some MvvmLight templates to the list of templates which you can add on an already existing project.&lt;/p&gt;
&lt;p&gt;You can download the source code of this article from GitHub &lt;a href="https://github.com/ralbu/GroupedItemsWithMvvmLight"&gt;Grouped Items with MvvmLight&lt;/a&gt;. I removed some of the code created by MvvmLight template to keep it simpler to understand. If you want to check what MvvmLight creates just a create a new MvvmLight project and check the code. &lt;br /&gt;Fire Visual Studio and create a new project. This will be an MvvmLight project.&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;&lt;a href="http://www.ralbu.com/image.axd?picture=clip_image002.jpg"&gt;&lt;img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border-width: 0px;" title="clip_image002" alt="clip_image002" src="http://www.ralbu.com/image.axd?picture=clip_image002_thumb.jpg" border="0" height="367" width="600" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;When you chose the MvvmLight project it creates a few classes. You can use other type but you'll need to add these classes manually. &lt;br /&gt;First of all, the App.xaml file was updated with the following&lt;/p&gt;
&lt;pre class="brush: xml; auto-links: true; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;"&gt;&amp;lt;vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True"  /&amp;gt;&lt;/pre&gt;
&lt;p&gt;It references the ViewModelLocator class added in the ViewModel folder. This is a locator class and uses SimpleIoc as a dependency injector.&lt;/p&gt;
&lt;p&gt;MainViewModel class was created in the same ViewModel folder and has WelcomeTitle property binded &amp;ndash; this was created by MvvmLight and I didn&amp;rsquo;t remove it.&lt;/p&gt;
&lt;p&gt;The MainPage.xaml updated with the DataContext reference&lt;/p&gt;
&lt;pre class="brush: xml; auto-links: true; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;"&gt;DataContext="{Binding Main, Source={StaticResource Locator}}&lt;/pre&gt;
&lt;p&gt;Here Main is the 'Main' property of type MainViewModel defined in the ViewModelLocator.&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;We will create an example of products so for this purpose a new set of classes were added to the Model folder. The first one - ProductItem has Title, Price and images properties. The second one - ProductGroup has Title which is the title of the group and Items which is a collection of ProductItems for the same group.&lt;/p&gt;
&lt;p&gt;The MainViewModel class had a constructor with IDataService parameter which was created by MvvmLight. That constructor is not needed any more and we need to create a constructor with no parameters. I will explain later why we need it. In the constructor I will set the WelcomeTitle and I will populate the Products collection with data.&lt;/p&gt;
&lt;p&gt;Let's add a grid view to the MainPage&lt;/p&gt;
&lt;pre class="brush: xml; auto-links: true; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;"&gt;&amp;lt;Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}"&amp;gt;
        &amp;lt;Grid.RowDefinitions&amp;gt;
            &amp;lt;RowDefinition Height="13*"/&amp;gt;
            &amp;lt;RowDefinition Height="51*"/&amp;gt;
        &amp;lt;/Grid.RowDefinitions&amp;gt;
        &amp;lt;TextBlock FontSize="56"
                   Text="{Binding WelcomeTitle}"
                   VerticalAlignment="Top"
                   HorizontalAlignment="Left"
                   FontFamily="Segoe UI Light" 
                   Margin="120,49,0,0" /&amp;gt;

        &amp;lt;Grid Style="{StaticResource LayoutRootStyle}" Grid.Row="1"&amp;gt;

            &amp;lt;GridView
            x:Name="itemGridView"
            AutomationProperties.AutomationId="ItemGridView"
            AutomationProperties.Name="Grouped Items"
            Grid.RowSpan="2"
            Padding="116,1,40,46"
            ItemsSource="{Binding Source={StaticResource groupedItemsViewSource}}"
            ItemTemplate="{StaticResource Standard250x250ItemTemplate}"
            SelectionMode="None"
            IsSwipeEnabled="false"&amp;gt;

                &amp;lt;GridView.ItemsPanel&amp;gt;
                    &amp;lt;ItemsPanelTemplate&amp;gt;
                        &amp;lt;VirtualizingStackPanel Orientation="Horizontal"/&amp;gt;
                    &amp;lt;/ItemsPanelTemplate&amp;gt;
                &amp;lt;/GridView.ItemsPanel&amp;gt;
                &amp;lt;GridView.GroupStyle&amp;gt;
                    &amp;lt;GroupStyle&amp;gt;
                        &amp;lt;GroupStyle.HeaderTemplate&amp;gt;
                            &amp;lt;DataTemplate&amp;gt;
                                &amp;lt;Grid Margin="1,0,0,6"&amp;gt;
                                    &amp;lt;Button
                                    AutomationProperties.Name="Group Title"
                                    Style="{StaticResource TextPrimaryButtonStyle}"&amp;gt;
                                        &amp;lt;StackPanel Orientation="Horizontal"&amp;gt;
                                            &amp;lt;TextBlock Text="{Binding Title}" Margin="3,-7,10,10" Style="{StaticResource GroupHeaderTextStyle}" /&amp;gt;
                                            &amp;lt;TextBlock Text="{StaticResource ChevronGlyph}" FontFamily="Segoe UI Symbol" Margin="0,-7,0,10" Style="{StaticResource GroupHeaderTextStyle}"/&amp;gt;
                                        &amp;lt;/StackPanel&amp;gt;
                                    &amp;lt;/Button&amp;gt;
                                &amp;lt;/Grid&amp;gt;
                            &amp;lt;/DataTemplate&amp;gt;
                        &amp;lt;/GroupStyle.HeaderTemplate&amp;gt;
                        &amp;lt;GroupStyle.Panel&amp;gt;
                            &amp;lt;ItemsPanelTemplate&amp;gt;
                                &amp;lt;VariableSizedWrapGrid Orientation="Vertical" Margin="0,0,80,0"/&amp;gt;
                            &amp;lt;/ItemsPanelTemplate&amp;gt;
                        &amp;lt;/GroupStyle.Panel&amp;gt;
                    &amp;lt;/GroupStyle&amp;gt;
                &amp;lt;/GridView.GroupStyle&amp;gt;
            &amp;lt;/GridView&amp;gt;
        &amp;lt;/Grid&amp;gt;
&amp;lt;/Grid&amp;gt;&lt;/pre&gt;
&lt;p&gt;Grid View ItemsSource is defined using groupedItemsViewSource&lt;/p&gt;
&lt;pre class="brush: xml; auto-links: true; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;"&gt;ItemsSource="{Binding Source={StaticResource groupedItemsViewSource}}"&lt;/pre&gt;
&lt;p&gt;This is the most important part. groupedItemsViewSource is a CollectionVewSource &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;pre class="brush: xml; auto-links: true; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;"&gt;&amp;lt;CollectionViewSource
    x:Name="groupedItemsViewSource"
    Source="{Binding Products}"
    IsSourceGrouped="true"
    ItemsPath="Items"
    d:Source="{Binding Products, Source={d:DesignInstance Type=data:MainViewModel, IsDesignTimeCreatable=True}}"/&amp;gt;&lt;/pre&gt;
&lt;p&gt;Pay attention at the &lt;span style="font-family: Courier New;" face="Courier New"&gt;&lt;span style="font-size: small;" size="2"&gt;d:Source&lt;/span&gt;.&lt;/span&gt; This is the code which binds the design view with the ViewModel. The 'd' namespace is ignored by the run time. It is used only for design time. &lt;br /&gt;The source is bindend to the Products collection of the MainViewModel. I said before that MainViewModel needs a parameter less constructor. This is the place where the constructor is used. If you don't define it then the source will not bind for design time.&lt;/p&gt;
&lt;p&gt;Now if you open the MainPage in Blend it will nicely display the design with all the images in place.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.ralbu.com/image.axd?picture=blend-mvvm.png"&gt;&lt;img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="blend-mvvm" alt="blend-mvvm" src="http://www.ralbu.com/image.axd?picture=blend-mvvm_thumb.png" border="0" height="322" width="600" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Source code is available on GitHub &lt;a href="https://github.com/ralbu/GroupedItemsWithMvvmLight"&gt;Grouped Items with MvvmLight&lt;/a&gt;.&lt;/p&gt;</description><pubDate>Sun, 18 Nov 2012 15:35:00 GMT</pubDate><guid isPermaLink="true">http://www.ralbu.com:80/post/2012/11/18/Building-WinRT-Grouped-items-using-MVVMLight</guid></item><item><title>A simple way of adding Privacy Policy to your WinRT application</title><link>http://www.ralbu.com:80/post/2012/10/07/A-simple-way-of-adding-Privacy-Policy-to-your-WinRT-application</link><description>&lt;p&gt;Every WinRT application should have a Privacy Policy otherwise you will fail the Store certification. You put the Privacy Policy in Charms, Settings. At the moment of writing this article there isn&amp;rsquo;t any component which can handle this for you property. All the components I&amp;rsquo;ve tried don&amp;rsquo;t work well, either because they haven&amp;rsquo;t been updated from Windows RC to RTM or just have some issues.&lt;/p&gt;
&lt;p&gt;If you finally finished your app and are rushing to submit it for certification and don&amp;rsquo;t want to spend time to implement your Privacy Policy the easiest way is to create a web page with Privacy Policy and link it from your application.&lt;/p&gt;
&lt;p&gt;In MainPage constructor (or you first page to load) add the following:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;pre class="brush: csharp; auto-links: true; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;"&gt;SettingsPane.GetForCurrentView().CommandsRequested += MainPage_CommandsRequested;&lt;/pre&gt;
&lt;p&gt;Next we need a method handler:&lt;/p&gt;
&lt;pre class="brush: csharp; auto-links: true; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;"&gt;private  void MainPage_CommandsRequested(SettingsPane sender, SettingsPaneCommandsRequestedEventArgs args)
{
    var cmd = new SettingsCommand("PrivacyPolicy", "Privacy Policy", new Windows.UI.Popups.UICommandInvokedHandler(x =&amp;gt;
    {
         ShowSettingPanel();
    }));

    args.Request.ApplicationCommands.Clear();
    args.Request.ApplicationCommands.Add(cmd);
}&lt;/pre&gt;
&lt;p&gt;and the method to run the web page with your Privacy Policy:&lt;/p&gt;
&lt;pre class="brush: csharp; auto-links: true; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;"&gt;private void ShowSettingPanel()
{
    Uri uri = new Uri("http://www.mywinrtapp.com/privacypolicy.html");
    Windows.System.Launcher.LaunchUriAsync(uri);
}&lt;/pre&gt;
&lt;p&gt;Now when you click on the Charms and Privacy Policy Internet Explorer will open the web page you specified in ShowSettingPanel.&lt;/p&gt;
&lt;p&gt;NOTE: Don&amp;rsquo;t forget to add this link in Windows Store Details page when you submit your application otherwise you will fail the certification&lt;/p&gt;</description><pubDate>Sun, 07 Oct 2012 09:02:00 GMT</pubDate><guid isPermaLink="true">http://www.ralbu.com:80/post/2012/10/07/A-simple-way-of-adding-Privacy-Policy-to-your-WinRT-application</guid></item></channel></rss>