Part 9: Data Binding to the RecipeDataSource.cs

Sign in to queue

Description

In this lesson we follow the instructions of the Hands On Lab to add and wire up the RecipeDataSource.cs and its images and data to make sweeping changes to the app.
 

Embed

Download

Download this episode

The Discussion

  • User profile image
    chilero

    How difficult would it be to separate the recipes.txt into separate files like Chinese.txt, French.txt etc... and connect each group to the corresponding .txt file?  What would have to be done?

  • User profile image
    BobTabor

    @chilero: It would be pretty easy to do what you want to do.  You would have to make a few decisions up front, such as how you'll introduce your data files to the app ... ex. you could put them in a single folder, and use WinRT API to iterate through all files in that folder, open each up, create new items and group using something akin to the RecipeDataSource.cs' approach.  Besides that, and designing the JSON to contain your object's properties, the RecipeDataSource.cs provides a nice template that will get you 80% of the way where you need to go.  Hope that helps!

  • User profile image
    matija4444

    Hey Bob!  When I try to build the program I get this error :

    Error 1 The type or namespace name 'Common' does not exist in the namespace 'ContosoCookbook' (are you missing an assembly reference?) C:\Users\Matija\Desktop\New folder\Problem1\WinStoreAppDev_CS\CS_24\App1\App1\RecipeDataSource.cs 51 62 App1

     

    What do you thin I did wrong?

  • User profile image
    BobTabor

    @matija4444: Hmm... I think you're missing a using statement.  Can you share out: (1) your using statements at the top of that code file, and (2) the exact line of code that it's complaining about?  That might help me help you further.  DON'T GIVE UP!  When you run into stuff like this, you learn a lot!

  • User profile image
    matija4444

    @BobTabor   it seems like I´m missing something in RecipeDataSource.cs.

    public abstract class RecipeDataCommon : ContosoCookbook.Common.BidingData

     

    It shows error on .BidingData. And after that it shows error on this part of code:

     public string UniqueId 
           {            
    get { return this._uniqueId; }           
     set { this.SetProperty(ref this._uniqueIdvalue);        
          }

     

    and using statements are:

    using System; 
    using System.Linq;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using System.Runtime.CompilerServices;
    using Windows.ApplicationModel.Resources.Core;
    using Windows.Foundation;
    using Windows.Foundation.Collections;
    using Windows.UI.Xaml.Data;
    using Windows.UI.Xaml.Media;
    using Windows.UI.Xaml.Media.Imaging;
    using System.Net.Http;
    using Windows.Data.Json;
    using Windows.ApplicationModel;
    using Windows.Storage.Streams;
    using System.Threading.Tasks;
    using Windows.Storage;
    using ContosoCookbook.Data;
  • User profile image
    BobTabor

    @matija4444: First ... shouldn't it be binding, not biding?  (Emphasis on the 'n' in 'binding')

    Second ... can you make sure (Ctrl + F) that there's a class called BindingData defined in the project, and that it belongs to the ContosoCookbook.Common namespace?



  • User profile image
    ambar007

    I am getting the same error as @matija4444 

    Error 1 The type or namespace name 'Common' does not exist in the namespace 'ContosoCookbook' (are you missing an assembly reference?) K:\ContossoCookBook\ContossoCookBook\DataModel\RecipeDataSource.cs 51 62 ContossoCookBook

     

  • User profile image
    ambar007

    It shows an error in :  public abstract class RecipeDataCommon : ContosoCookbook.Common.BindableBase..at common 

  • User profile image
    ambar007

    also it shows an error in all the set property saying contosocookbook.data.recipe data doesnot contain any definition for set property 

  • User profile image
    ambar007

    error solved there are 2 contosocookbook files ..one has a common clas the other one just has a data class ...so to remove the error just delete that statement and rewrite it using intellisense 

     

  • User profile image
    matija4444

    @ambar007

    Can you give me some more details?

  • User profile image
    ambar007

    just remove that line manually and then retype it 

    public abstract class RecipeDataCommon : ContosoCookbook.Common.BidingData 


    this line ..using telesense ..my error just got removed like this make sure you use telesense to
    invoke the common.bindablebase method
  • User profile image
    matija4444

    @ambar007: I folow your direction and solve the error but to me code is little diferent

    public abstract class RecipeDataCommon : ContosoCookbook.Common.BindableBase


    we will see is it corect!
  • User profile image
    gerdi

    I was getting the same error. If you have named your project other then the actual name of "ContosoCookbook" ( may you using  lower case first letter style ) then you need to change it to that exact name.

    public abstract class RecipeDataCommon : Foo.Common.BindableBase

     

  • User profile image
    matija4444

    @gerdi:Does the program imports pictures when you use this code? That is a problem to me!

  • User profile image
    santra26413​84

    @matija4444 just chnage your ContosoCookbook to your project name, and it's case sensitive

    public abstract class RecipeDataCommon : [LabProjectName].Common.BindableBase

     
  • User profile image
    miltoncab

    Excellent! I have the same error and immediately change the [LabProjectName] in the same way that @santra2641384 said, they problem solved. Thanks guys,

    @BobTabor great tutorial....

  • User profile image
    danncoba

    I have a problem here. It tells me that i don't have a method GetGroups in RecipeDataSource

  • User profile image
    BobTabor

    @danncoba: Did you solve this?  Can you explain more ... at what step did you experience this?  Can you point to the time marker in the video (example: 4:15) where what YOU see in your code and what I see in my code are different?

  • User profile image
    Mustansir

    Hi bob, i have been following up your tutorials pretty closely
    and i'm stuck at the data part of app, unlike in the video i want to use a database to connect to the app and display the data in the database using sqlite, as i'm new to the app development or pretty much any kind of development can you point me or suggest me anything about the problem i'm facing

  • User profile image
    BobTabor

    @Mustansir: Sorry, SQLite is outside of my sphere of knowledge.  You have the bones of the application, you would just need to replace the implementation specific to grabbing data from the text file with your SQLite code.  I would have to do the same as you ... look for a few examples online, etc.

  • User profile image
    cakmak

    MR Bob Tabor ı am watching your tutorials  ı wanted to tell  you  your tutorials very helpful thx  for  your help  I  want ask  you question  I am Using bing map in ItemDetailPage.xaml  in Flipview my map is in templated in flipview  ok. question is  How ı can send diferent  location for each page in ItemdetailPage  because  ı  am using  SampleDataSource  so  I am confuse   NavigationParameter  how  will  know it  thx for your hellp     

  • User profile image
    BobTabor

    @cakmak: You can pass any object of any type as the NavigationParameter.  Create a new type with the kind of data you want to pass.  If you just need to pass a string, that's cool too ... no need to create a whole type for it.  In the click event of the grid, you figure out what it is you want to send to the ItemDetailPage (using the name of the item, or some property of the item, or the class the item is associated with).  Check this out:

     

    http://stackoverflow.com/questions/13917303/how-to-pass-a-complex-object-between-pages-in-a-windows-store-app

  • User profile image
    ANAS55

    Hi Bob, besides only reading from the json file i also want to store or update data in the recipe.txt file e.g. updating whether the recipe is user favorite or not and also allowing the user to store their own recipes. how can this all be done with json or should i adopt some other way to do this (which way)?

  • User profile image
    BobTabor

    @ANAS55: Here's a few links that should get you oriented in the right direction.  This looks like a job for the JavaScriptSerializer Class:

    http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx

    Here's a StackExchange post that might also be helpful:

    http://stackoverflow.com/questions/8569855/reading-from-writing-and-appending-to-json-files

    Good luck!

  • User profile image
    ANAS55

    @BobTabor: Thanks, that really helped.

  • User profile image
    aka17

    suddenly groupdetailpage.xaml.cs and groupitempage.xaml.cs both r opening in notepad n they r empty plzz hlp me..

  • User profile image
    BobTabor

    @aka17: Yikes!  That's not good.  You must have the file associations incorrect.  Have you done anything recently like attempted to open any C# files in Notepad?  Or has anyone tried this on the computer you're using?  I think it's easily remedied ... I would start here:

    http://winsupersite.com/article/windows8/windows-8-tip-change-file-associations-144102

  • User profile image
    aka17

    its nt working ,do i hv to do it al over again , ahhh plzz suggest.

  • User profile image
    aka17

    i only tried to use simulator and that hanged the system and i had to restart the system may be that is the problem...

  • User profile image
    BobTabor

    @aka17: Hmm... that's a tough one.  You might try to re-install Visual Studio to re-create that association for the .cs files.  If that doesn't work?  I'm not sure what to try next.  What you described has never happened to me before.  

    You might need to contact support or someone on Visual Studio's forums.  This is where I would post it.  MAKE SURE YOU PUT AS MUCH DETAIL AS POSSIBLE IN YOUR FORUM POST to get the best results.

    http://social.msdn.microsoft.com/Forums/en-US/winappswithcsharp/threads

    Good luck!

  • User profile image
    Coolrw3

    How to hide all items, but not Group name and group box.. I have too many items and i do want all item to this page. When click group box or title, find groupdetail page and there is all this group items.<br><br>

  • User profile image
    BobTabor

    @Coolrw3: I believe you can limit the number of items you display.  The Windows Store (C#) Grid App Template does this by default ... see the SampleDataSource.cs, beginning in line 133 ... here they create a public property that will ONLY reveal 12 items, and spend a lot of code managing which 12 items will be displayed (you may not need all of that complexity).  You could limit that to just 6 or 1 if you like.

  • User profile image
    Coolrw3

    Is that possible what chilero asked before? Is there any nice sample?

    Grid template is huge mess in default

  • User profile image
    BobTabor

    @Coolrw3: I'm sure there are some examples out there, but I don't know of any.  I would have to spend some time on Bing.com and pour over a lot of code to find what I needed.  If you're a C# developer, chances are, you already have the skills to do this ... you simply need to decide HOW you'll do it ... how will you store the data?  How will you dynamically add the data to the project?  How will you retrieve the data and deserialize it into objects in your app.  I'll bet you could figure it out if you don't have an example to pull from.

  • User profile image
    moosetracker

    This class has gotten me further then any other I've taken, as most want to show how to bind with hardcoded data and brush over the data file aspect all together, but I too am looking to link up to some kind of SQL DB, don't care if it is SQL compact, SQL express, or sounds like there is now a SQL local..  I have found a piece & part on each, but not the whole enchilada on any..  Which is best (or pros & cons) How to hook up the DB to VS, how to read & update, how to deploy, how to update with changes to DB or without wiping out the users data..  Now with the change to async reading & writing, I fear the mystery will keep mystifying and still no one will tackle it with a training video.

    It would be great for a video on that in LearnVisualStudio, once you are done creating videos for channel 9..

  • User profile image
    BobTabor

    @moosetracker: Great feedback.  re: data ... I think you haven't seen much about databases and Windows Store apps because Microsoft is hoping you'll use Azure for large data needs.  If your needs are small, then serializing an object graph out to JSON may suit most small data needs.  I suppose SQL Lite is available or perhaps SQL local (although I'm not sure you can use that for Windows Store apps .. not sure, really), but I've not touched that scenario yet ... just haven't needed to.

  • User profile image
    ReidSmiths

    @BobTabor: Do you think the method for

    public static async Task LoadLocalDataAsync()
    can load data from WCF service ? Thanks.

  • User profile image
    Najeeb Anwer

    Hi Bob,
    I have found that despite what the lab instructions say, the loading of the recipe data should happen BEFORE the check for applicationstate=terminated. This is because the call to RestoreAsync causes the LoadState method to be called in GroupDetailPage.xaml.cs and ItemDetailPage.xaml.cs and these will throw an exception because they both use RecipeDataSource and expect it to be populated.
    Even the code provided by Microsoft (say, for lab 4) will throw an exception if you go to a recipe detail, terminate the app, and then restart it.

    Najeeb

  • User profile image
    BobTabor

    @Najeeb Anwer: Ok, thanks for the feedback.  You may want to give that feedback to the Lab guys ... they will definitely want to fix that for the next iteration!  Thanks!

  • User profile image
    BobTabor

    @ReidSmiths: Hmm... off the top of my head, I can't see why it wouldn't work.  I would have to try it -- have you made any progress with that?

  • User profile image
    ReidSmiths

    @BobTabor: Thanks for the reply. It works, but now I am trying to pass parameter to the SampleDataSource to limit the data. I create a MainPage user than login, after logging in depending on the user role it displays different thing. But how do I pass the parameter to the SampleDataSource. After setting the breakpoints I somehow figure out how it pass but not sure how to implement it again in different method ? Thanks.

  • User profile image
    BobTabor

    Opening comments

  • User profile image
    Edin

    Hi Bob, Thanks for such a great tutorial series. I'm actually using VS 2013 with Windows App 8.1. I haven't had too much trouble up until this lesson. Do you have any hints as to what we need to do if we're using VS 2013? The template used in VS 2012 and 2013 are quite different. (missing certain classes etc).

  • User profile image
    BobTabor

    @Edin: Hi, admittedly I've not touched Win Store app dev since I finished this series.  The series is  due for an update, however that's really Microsoft's call.  Ideally, they would update the Hands On Labs (which I could be mistaken but I'm not sure they've done that yet.)  However, I think the series is still helpful, but you'll have to take away the "big ideas" and be flexible enough to work around the template and API changes.

  • User profile image
    steve

    I followed the steps with your video, and fromthe lab and after this lesson i run the program and only see the Contoso Cookbook No arrow and no tiles. last line of output is "The program '[6436] ContosoCookbook.exe: Managed (v4.0.30319)' has exited with code -1 (0xffffffff).".
    What does this mean ?

  • User profile image
    albert

    The black flashes in the vid make me feel like I'm blacking out LOL!
    Nonetheless, your vids are awesome! :D

  • User profile image
    Clint

    @albert: black flashes?  what browser / version are you using?

  • User profile image
    steve

    Never mind, I figured it out, had to remove the c# "if *** running" statement and the "return", moved the "Window.Current.Activate();" down in the code after the next catch.
    Must say Bob, you are one of the best teachers I have ever come across. From my Comodore days (30 years ago) to C#/xaml in less then 3 weeks.

  • User profile image
    Asad

    Bob GURU ! awesome job but you did not explain how to use any other database like Microsoft SQL Server / MySQL with WinStore ?
    if you already have any tutorial for VS2013+8.1+database then kindly share here . thanks a lot!

  • User profile image
    Clint
  • User profile image
    dub

    Anyone having trouble with BindableBase Class

    http://www.getcodesamples.com/src/E0B61897/85E7DA46

  • User profile image
    dub

    ....Actually , just go here. Download the project, open the first solution folder and convert to windows 8.1.

    http://msdn.microsoft.com/en-us/windows/apps/jj674832.aspx

  • User profile image
    Sam

    Hi Guys I am getting an error at BindableBase (The type or Name Space Does not exist in ContosoCookBook.Common)
    Help needed..

  • User profile image
    Kobie

    Hi Bob,

    I have a silly question, I followed this, but am wondering how one would go by creating item templates, that contain forms, for instance, to edit an Item? Do I need to create a separate page, that then just updates the file or makes changes to it? If not how do I load it from Data? Confused...

Add Your 2 Cents