locked
Filter Hub Tamplate List of results

    Question

  • Hi, Please can somebody help me figure this out. I am trying to filter the hub Template to show only items that satisfy my criteria.

    This is my JSON file look like:

    {"Groups":[
      {
        "UniqueId": "Group-1",
        "Title": "Snacks",
        "Subtitle": "Group subtitle: 1",
        "ImagePath": "Assets/DarkGray.png",
        "Description" : "Group Description: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus tempor scelerisque lorem in vehicula. Aliquam tincidunt, lacus ut sagittis tristique, turpis massa volutpat augue, eu rutrum ligula ante a ante",
        "Items":
            [
                        {"UniqueId":"42204","Title":"16","Subtitle":"1.0 cubic inch","ImagePath":" ","Description":"Rice cake, cracker (include hain mini rice cakes)","Content":"4.2"},
    					{"UniqueId":"19034","Title":"31","Subtitle":"1.0 cup","ImagePath":" ","Description":"Snacks, popcorn, air-popped","Content":"8"},
    					{"UniqueId":"19806","Title":"31","Subtitle":"1.0 cup","ImagePath":" ","Description":"Snacks, popcorn, air-popped, white popcorn","Content":"8"},
    					{"UniqueId":"19052","Title":"34","Subtitle":"1.0 cake","ImagePath":" ","Description":"Snacks, rice cakes, brown rice, buckwheat","Content":"9"},
    					{"UniqueId":"19817","Title":"34","Subtitle":"1.0 cake","ImagePath":" ","Description":"Snacks, rice cakes, brown rice, buckwheat, unsalted","Content":"9"},
    					{"UniqueId":"25059","Title":"35","Subtitle":"1.0 cake","ImagePath":" ","Description":"Snacks, brown rice chips","Content":"9"},

    and this is my code :

          private async void NavigationHelper_LoadState(object sender, LoadStateEventArgs e)
            {
                // TODO: Create an appropriate data model for your problem domain to replace the sample data.
                //var group = await SampleDataSource.GetGroupAsync((string)e.NavigationParameter);
                //this.DefaultViewModel["Group"] = group;
    
              ////My script
    
                var group = await SampleDataSource.GetGroupAsync((string)e.NavigationParameter);
                var ItemTitile = "35";
                var groupS = group.Items;//.Where(item => item.Title == ItemTitile);//from s in group where s.Title = "31" Select s;
                //var groupG = from zumal in groupS where zumal.Title == ItemTitile select zumal ; 
                this.DefaultViewModel["Group"] = groupS;
            }

    Thank you in advance. I highly appreciate the help.

    Tuesday, April 28, 2015 8:13 PM

Answers

  • I got it fixed by adding .ToList(); please see the solution below:

                var group = await SampleDataSource.GetGroupAsync((string)e.NavigationParameter);
                var ItemTitile = "35";
                var groupS = group.Items;//.Where(item => item.Title == ItemTitile);//from s in group where s.Title = "31" Select s;
                //var groupS2 = groupS.Where(s2=>s2.Title == ItemTitile).ToList();//this is working keep it
                var groupS2 = (from s2 in groupS where s2.Title == ItemTitile select s2).ToList();
                this.DefaultViewModel["Group"] = groupS2;

    • Marked as answer by YMBA Friday, May 1, 2015 3:23 PM
    Friday, May 1, 2015 3:22 PM

All replies

  • Can you show all your .xaml and .cs code in here?


    I sale myself ONLY half CNY!

    Wednesday, April 29, 2015 2:17 PM
  • Sure, just to let everyone know this is the default Hub Template from Visual Studio.

    This is the XAML for SectionPage.xaml

    <Page
        x:Name="pageRoot"
        x:Class="FoodApp.SectionPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:FoodApp"
        xmlns:data="using:FoodApp.Data"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        DataContext="{Binding DefaultViewModel.Group, RelativeSource={RelativeSource Self}}"
        d:DataContext="{Binding Groups[0], Source={d:DesignData Source=/DataModel/SampleData.json, Type=data:SampleDataSource}}"
        Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Page.Transitions>
            <TransitionCollection>
                <NavigationThemeTransition>
                    <NavigationThemeTransition.DefaultNavigationTransitionInfo>
                        <ContinuumNavigationTransitionInfo/>
                    </NavigationThemeTransition.DefaultNavigationTransitionInfo>
                </NavigationThemeTransition>
            </TransitionCollection>
        </Page.Transitions>
    
        <!--
            This grid acts as a root panel for the page that defines two rows:
            * Row 0 contains the page title
            * Row 1 contains the rest of the page layout
        -->
        <Grid>
            <Grid.ChildrenTransitions>
                <TransitionCollection>
                    <EntranceThemeTransition/>
                </TransitionCollection>
            </Grid.ChildrenTransitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
    
            <!-- Title Panel -->
            <StackPanel Grid.Row="0" Margin="19,0,0,0">
                <TextBlock x:Uid="Header" Text="application name" Style="{ThemeResource TitleTextBlockStyle}" Margin="0,12,0,0"/>
                <TextBlock Text="{Binding Title}" Margin="0,-6.5,0,26.5" Style="{ThemeResource HeaderTextBlockStyle}" TextWrapping="NoWrap" CharacterSpacing="{ThemeResource PivotHeaderItemCharacterSpacing}" />
            </StackPanel>
    
            <ListView
                x:Name="itemListView"
                AutomationProperties.AutomationId="ItemListView"
                AutomationProperties.Name="Items In Group"
                TabIndex="1"
                Grid.Row="1"
                ItemsSource="{Binding Items}"
                IsItemClickEnabled="True"
                ItemClick="ItemView_ItemClick"
                SelectionMode="None"
                IsSwipeEnabled="false"
                Margin="19,0,0,0">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>
    
                            <Border Background="{ThemeResource ListViewItemPlaceholderBackgroundThemeBrush}" Margin="0,9.5,0,0" Width="79" Height="79">
                                <Image Source="{Binding ImagePath}" Stretch="UniformToFill" AutomationProperties.Name="{Binding Title}"/>
                            </Border>
                            <StackPanel Grid.Column="1" VerticalAlignment="Top" Margin="14.5,0,0,0">
                                <TextBlock Text="{Binding Title}" Style="{ThemeResource ListViewItemTextBlockStyle}"/>
                                <TextBlock Text="{Binding Description}" Style="{ThemeResource ListViewItemContentTextBlockStyle}" Foreground="{ThemeResource PhoneMidBrush}"/>
                                <TextBlock Text="{Binding Subtitle}" Style="{ThemeResource ListViewItemSubheaderTextBlockStyle}"/>
                            </StackPanel>
                        </Grid>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </Grid>
    </Page>
    

    Also, this is the SectionPage.xaml.cs

    using FoodApp.Common;
    using FoodApp.Data;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Runtime.InteropServices.WindowsRuntime;
    using Windows.ApplicationModel.Resources;
    using Windows.Foundation;
    using Windows.Foundation.Collections;
    using Windows.Graphics.Display;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;
    using Windows.UI.Xaml.Controls.Primitives;
    using Windows.UI.Xaml.Data;
    using Windows.UI.Xaml.Input;
    using Windows.UI.Xaml.Media;
    using Windows.UI.Xaml.Navigation;
    
    // The Hub Application template is documented at http://go.microsoft.com/fwlink/?LinkId=391641
    
    namespace FoodApp
    {
        public sealed partial class SectionPage : Page
        {
            private readonly NavigationHelper navigationHelper;
            private readonly ObservableDictionary defaultViewModel = new ObservableDictionary();
    
            public SectionPage()
            {
                this.InitializeComponent();
    
                this.navigationHelper = new NavigationHelper(this);
                this.navigationHelper.LoadState += this.NavigationHelper_LoadState;
                this.navigationHelper.SaveState += this.NavigationHelper_SaveState;
            }
    
            /// <summary>
            /// Gets the <see cref="NavigationHelper"/> associated with this <see cref="Page"/>.
            /// </summary>
            public NavigationHelper NavigationHelper
            {
                get { return this.navigationHelper; }
            }
    
            /// <summary>
            /// Gets the view model for this <see cref="Page"/>.
            /// This can be changed to a strongly typed view model.
            /// </summary>
            public ObservableDictionary DefaultViewModel
            {
                get { return this.defaultViewModel; }
            }
    
            /// <summary>
            /// Populates the page with content passed during navigation.  Any saved state is also
            /// provided when recreating a page from a prior session.
            /// </summary>
            /// <param name="sender">
            /// The source of the event; typically <see cref="NavigationHelper"/>
            /// </param>
            /// <param name="e">Event data that provides both the navigation parameter passed to
            /// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested and
            /// a dictionary of state preserved by this page during an earlier
            /// session.  The state will be null the first time a page is visited.</param>
            private async void NavigationHelper_LoadState(object sender, LoadStateEventArgs e)
            {
                // TODO: Create an appropriate data model for your problem domain to replace the sample data.
                //var group = await SampleDataSource.GetGroupAsync((string)e.NavigationParameter);
                //this.DefaultViewModel["Group"] = group;
    
              ////YMH script
    
                var group = await SampleDataSource.GetGroupAsync((string)e.NavigationParameter);
                var ItemTitile = "35";
                var groupS = group.Items;//.Where(item => item.Title == ItemTitile);//from s in group where s.Title = "31" Select s;
                //var groupG = from zumal in groupS where zumal.Title == ItemTitile select zumal ; 
                this.DefaultViewModel["Group"] = groupS;
            }
    
            /// <summary>
            /// Preserves state associated with this page in case the application is suspended or the
            /// page is discarded from the navigation cache.  Values must conform to the serialization
            /// requirements of <see cref="SuspensionManager.SessionState"/>.
            /// </summary>
            /// <param name="sender">The source of the event; typically <see cref="NavigationHelper"/></param>
            /// <param name="e">Event data that provides an empty dictionary to be populated with
            /// serializable state.</param>
            private void NavigationHelper_SaveState(object sender, SaveStateEventArgs e)
            {
                // TODO: Save the unique state of the page here.
            }
    
            /// <summary>
            /// Shows the details of an item clicked on in the <see cref="ItemPage"/>
            /// </summary>
            /// <param name="sender">The GridView displaying the item clicked.</param>
            /// <param name="e">Event data that describes the item clicked.</param>
            private void ItemView_ItemClick(object sender, ItemClickEventArgs e)
            {
                var itemId = ((SampleDataItem)e.ClickedItem).UniqueId;
                if (!Frame.Navigate(typeof(ItemPage), itemId))
                {
                    var resourceLoader = ResourceLoader.GetForCurrentView("Resources");
                    throw new Exception(resourceLoader.GetString("NavigationFailedExceptionMessage"));
                }
            }
    
            #region NavigationHelper registration
    
            /// <summary>
            /// The methods provided in this section are simply used to allow
            /// NavigationHelper to respond to the page's navigation methods.
            /// <para>
            /// Page specific logic should be placed in event handlers for the
            /// <see cref="NavigationHelper.LoadState"/>
            /// and <see cref="NavigationHelper.SaveState"/>.
            /// The navigation parameter is available in the LoadState method
            /// in addition to page state preserved during an earlier session.
            /// </para>
            /// </summary>
            /// <param name="e">Event data that describes how this page was reached.</param>
            protected override void OnNavigatedTo(NavigationEventArgs e)
            {
                this.navigationHelper.OnNavigatedTo(e);
            }
    
            protected override void OnNavigatedFrom(NavigationEventArgs e)
            {
                this.navigationHelper.OnNavigatedFrom(e);
            }
    
            #endregion
        }
    }
    
    You will see that I did try several scripts to filter the result by Calories which is the "Title" in my JSON file.

    Thursday, April 30, 2015 5:24 PM
  • I got it fixed by adding .ToList(); please see the solution below:

                var group = await SampleDataSource.GetGroupAsync((string)e.NavigationParameter);
                var ItemTitile = "35";
                var groupS = group.Items;//.Where(item => item.Title == ItemTitile);//from s in group where s.Title = "31" Select s;
                //var groupS2 = groupS.Where(s2=>s2.Title == ItemTitile).ToList();//this is working keep it
                var groupS2 = (from s2 in groupS where s2.Title == ItemTitile select s2).ToList();
                this.DefaultViewModel["Group"] = groupS2;

    • Marked as answer by YMBA Friday, May 1, 2015 3:23 PM
    Friday, May 1, 2015 3:22 PM