Searching with C# Metro Apps - how to use DefaultViewModel["Results"]

Unanswered Searching with C# Metro Apps - how to use DefaultViewModel["Results"]

  • Saturday, March 24, 2012 5:58 PM
     
      Has Code

    Hello!

    I've been playing around with some of the Charms features, and am still trying to figure out how to get search working. I've set up the search contract, etc. and am now actually trying to get results to display on the screen. The comments say the following:

    // TODO: Respond to the change in active filter by setting this.DefaultViewModel["Results"] // to a collection of items with bindable Image, Title, and Subtitle properties

    but I have really no idea how to create a collection of items with bindable Image, Title and Subtitle properties. Could anyone explain to me how to create this collection?

    Thank you very much,

    Matthew

All Replies

  • Sunday, March 25, 2012 4:08 AM
     
      Has Code

    Basically, you can create your own model that has those properties then build a list of instances of the model.  For example:

    //SearchResult.cs
    
    public class SearchResult
    {
      public string Title { get; set; }
      public string Subtitle { get; set; }
      public object Image { get; set; }
    }
    
    
    //Inline on your Search target page
    var Results = new List<SearchResult>()
    {
      new SearchResult()
      {
        Title = "Result 1",
        Subtitle = "Result 2",
        Image = "https://www.google.com/images/srpr/logo3w.png"
      },
      new SearchResult()
      {
        //set the values
      }
      // etc
    }
    
    this.DefaultViewModel["Results"] = Results;
    
    

    I made SearchResult.Image an object since an Image element can accept a URI to a resource such as the Google logo, a local file such as "Assets\icon.png" or you can be more elaborate and hand it a BitmapImage.


    Robert Mills

  • Sunday, March 25, 2012 5:09 PM
     
      Has Code

    Basically, you can create your own model that has those properties then build a list of instances of the model.  For example:

    //SearchResult.cs
    
    public class SearchResult
    {
      public string Title { get; set; }
      public string Subtitle { get; set; }
      public object Image { get; set; }
    }
    
    
    //Inline on your Search target page
    var Results = new List<SearchResult>()
    {
      new SearchResult()
      {
        Title = "Result 1",
        Subtitle = "Result 2",
        Image = "https://www.google.com/images/srpr/logo3w.png"
      },
      new SearchResult()
      {
        //set the values
      }
      // etc
    }
    
    this.DefaultViewModel["Results"] = Results;
    

    I made SearchResult.Image an object since an Image element can accept a URI to a resource such as the Google logo, a local file such as "Assets\icon.png" or you can be more elaborate and hand it a BitmapImage.


    Robert Mills

    Thank you very much, that worked perfectly!

    Now is there some easy way to link the SearchResult object to a page in my project?

    Thanks,

    Matthew

  • Saturday, April 07, 2012 2:44 PM
     
     

    That array works great but I need to have it search on items from an multiple RSS feeds used throughout the rest of my app. Just so I have the logic right.. would I use a foreach loop to create the list ?

    If there's a better way, I'd really appreciate the help. Cheers.

    I run XAML + C#


    techAU http://techAU.tv


    • Edited by techAU Saturday, April 07, 2012 2:44 PM
    •