Entries:
Comments:
Posts:

Loading User Information from Channel 9

Something went wrong getting user information from Channel 9

Latest Achievement:

Loading User Information from MSDN

Something went wrong getting user information from MSDN

Visual Studio Achievements

Latest Achievement:

Loading Visual Studio Achievements

Something went wrong getting the Visual Studio Achievements

Part 7: Data Binding and the SampleDataSource.cs

Download

Right click “Save as…”

In this lesson, we begin the process of understanding how to utilize data in our app.  We begin by looking at this from the GroupedItemsPage.xaml's perspective, and how it utilizes the CollectionViewSource to bind to a View Model provided in the LayoutAwarePage.cs.  We talk about the DefaultViewModel object which implements IObservableMap<K, V> and what that really means.  Finally, we pick apart the SampleDataSource.cs and observe it's inheritance hierarchy and the purpose of each of the classes defined in that file.

 

Download the entire series' source code 

Tags:

Follow the Discussion

  • In case any are curious, to close the SIMULATOR...

    Press Ctrl+Alt+F4.

    I was certainly curious, even once shutting down my system from within the SIM !

  • EatDrinkCodeEatDrinkCode

    Or we can click "disconnect" after clicking settings > power to close the simulator. This simulator is really something useful for developers. It almost can be used like a real tablet at the same time. I also tried to debug contoso cookbook in the simulator when I was using simulator (I mean simulator inside simulator), and obviously it didn't work :) Besides, it is actually difficult for me to understand the code hierarchy of the application. Is there any source you can advise me to understand this hierarchy Bob Tabor? Thank you...

  • @EatDrinkCode: What do you mean by the "code hierarchy" ... do you mean from a visual / aesthetic sense?  Or from a class-hierarchy sense ... i.e., which classes are related to other classes?  Which methods call other methods?  Visual Studio has ways of performing some of these things, but I may need a little clarification on precisely what it is you are looking for since I'm not familiar with your terminology.

  • EatDrinkCodeEatDrinkCode

    Sorry about my bad english. I couldn't understand what you refer as class-hierarchy. Especially I have some difficulty in understanding which classes are related to other classes. For example, if I want to write this application from scratch to the part we learned without using the available template of VS, I have no idea about what to do although I watched the video.

  • EatDrinkCodeEatDrinkCode

    What I asked is not something you can explain me in a few words, I'm aware of that. Therefore I wondered whether there is any source you can advise me.

  • @EatDrinkCode: Ok, got it.  Here's a few things I'd recommend.  (1) If you haven't already, you really should watch the C# Fundamentals course I created.  That's a good first step -- make sure you focus on classes, creating relationships between classes, creating instances of classes, etc.  (2) If your goal is to re-create this application from scratch using only your own code and are stumped by this part where you have to create a data source you might have to take a few intermediate steps first to make sure you really understand what the Grid App template and its various settings need from you.  I would recommend you start by simplifying the SampleDataSource.cs dramatically.  To do this, re-create the SampleDataSource.cs with your own code (but don't delete the original ... use it as a reference).  Create a Parent and a Child class, and keep them as simple as possible ... In other words, shave off all but one or two properties in the classes.  Next, remove a lot of the example data that is hard coded in the SampleDataSource.cs.  Only add enough code to get the Grid App template to data bind to your class in as few lines of code as possible.  This should help you wade through what matters and what is just "more of the same" or extraneous.  (3) As yourself critical questions, like "why did they make that property 'static'?" or "why are they doing this?  what is the relationship between this and that?"  or "What the heck does that keyword mean in the first place?"  (Then go off for a few minutes and remind yourself what that given keyword is contributing to the line of code.)  Everyone has to go through this ... it's challenging and time consuming, but insights come from this type of struggle.  I spent two or three days with a couple "side projects" to understand how all this data binding works, and then I pushed it further ... what if I have a relationship like Parent -> Child -> Grandchild ... how can I work with *that* hierarchy.  Once I played with this idea enough, I felt I had mastered enough of it to speak about it intelligently.  (4)  Turn off all distractions and force yourself to focus on this in quiet.  Keep staring at this.  Set break points and walk through, hovering your mouse cursor over the values or using the Watch or Locals windows in Visual Studio to help you monitor changes to variable or type values.  Take notes.  Keep doing it until it starts making sense. 

    If it's any consolation, understanding this was the hardest part for me and like I said, I needed to stop down, simplify by writing my own super-simple examples in side projects, etc. so that I could get it all sorted out in my mind.  I'm not one of these geniuses that can look at things like this and figure it out in 30 seconds.

    Hope that helps a little.

  • EatDrinkCodeEatDrinkCode

    Yes, this is very helpful. I will try to do what you recommend. Thank you

  • Hi Bob & channel9 community,

    I am struggling to understand how does the

    " { Binding etc. } "

    works and have few questions about it.

     E.G. file MainPage.xaml.cs

    I created a public class with public static members:

     public class MyClass
        {
            private static string field="Hei there! ";
            public static string MyProperty
            {
                get
                {
                    return field;
                }
                set
                {
                    field = value;
                }
            }
    
            public void DoSomething()
            {
            }
    
            public MyClass()
            {
                /* Set default values for the props and fields */
            }
        }

    then in the MainPage.xaml:

    I tried to bind the static string value to the TextBlock Text like this:

    <TextBlock Text="{ Binding MyClass.MyProperty }" Style="{StaticResource BasicTextStyle}"/>

    but it seems that I missed something...

    I read some articles on MSDN forums but I don't understand WHAT and HOW  ( code behind )  can I bind it to a Text, Style , Source, etc. . I know it has something to do with the Binding Type.

  • Hi Bob i have a doubt in here, i am wondering that every data in the sampledatasource (recipedatasource in case ) like title, subtitle, content, description have pretty much simple property which gets and sets the data in the respective datagroup or dataitem but not the imagepath it need some manipulation with the imagepath, now why is such happening, can't we just get and set the image string too?? . Now lets say i have to display a video in the mediaelement control and want to bind the path of the video would that require manipulation to?? How can i achieve that ?

  • @MustansirSabir: I wondered the same thing w/ regards to the image path.  I think they / you *could* add the full path in there if you wanted to.  Perhaps this would make it easier to change the path later ... you know, if folders change.  You wouldn't have to change all the data, just the one spot in code.  Regarding the media element control, I haven't tried that specifically, but I'm guessing you could probably do the same thing -- hard code the folder into the data, or at runtime figure out where the media was added, whether in a subfolder, or a special Windows folder.  Hope that helps?

  • @Mtan3, you can't refer to the class directly. Create an instance of the class e.g. in the code behind file (.xaml.cs) and set the this.datacontext to be that class instance. With that you set the data to be available on the page scope. then write ... {binding MyProperty}... this binds the text property that's located in the root of datacontext (the instance of the MyClass class that you created)
  • @jukkaw 

    I have been reading about it and I think I understand how it works, but thanks anyway.

    It seemed that I should've read more on data binding.

  • @Mtan3: Whoops ... sorry I missed your comment up there!!!  Can I do anything to help?  Again, sorry, and thanks to @jukkaw: for the assist.  Let me know.

  • it's okay, I get it know Bob, but thanks anyway. I was struggling about the binding part but I understand it now.

  • Hello Bob, 
    I finished building my app by adapting the SampleDataSource.cs file as the Data model for my Windows Store app.

    I want to port my app to Windows Phone 8 but I've been having difficulties with adapting my Current Data file.

     

    Any *pointers 

  • @aniebiet: Sorry for the delayed response ... missed your note.  What difficulties are you having? I'll try to help.  You should be able to take a very similar tack to accessing XML or JSON in your Phone project.

  • nerd question... what are the specs of your rig?.. just curious your VS and Windows 8 are super fast granted I have an older laptop but still its fast!....

  • @darthgreg78: I'll nerd out with you here.  Smiley  Actually, I should write a blog post about this.  I have two rigs ...

    The first, a custom box I built from scratch with hand-picked components including the Asus Sabertooth Z77 motherboard, an overclock-able Intel 3770K quad core 3.5ghz, a fast OCZ SSD, 16gb of fast Corsair Vengeance RAM and a GeForce GTX660 video card w/ 2gb ram all packed in a Cooler Master XAF XB test bench mid tower case.  It's a monster.  I use it as my main developer box when I write code for practice, for projects, etc.

    The second - the box I used for this series - is actually a virtual machine running inside of VMWare Fusion 5.x on a Mac Pro.  The physical Mac Pro is also monster, and I've owned it since spring 2009: dual quad core xeon nehalim processors, 16gb of ram.  I have about a dozen hard drives that I swap in and out of that box depending on my needs.  The video card is older, but I don't know how much it really contributes in a virtualized environment.  

    I use this box for videos because I love the screen casting software I've settled on: Screenflow by Telestream.  I prefer it over Camtasia, which is what most people use.  First of all, Screenflow is elegant.  Second, I was a Camtasia user since version 2.0 way back in 2001.  I made the switch to Apple hardware back when I needed to create some videos for Microsoft in 2007 and they wanted me to use Windows Vista.  Camtasia couldn't get their act together to support the video codecs in time, so I found a better solution ... do everything in a VM and record it "from the outside".  I've never had any drama since then.  Furthermore, it allows me to create pristine VM's that I can copy over, install new software on, etc.  I highly recommend this approach as a software developer if you can swing it.  Configuration conflicts are the biggest headaches.

  • I am using sqlite for storing data  but I am not able to encrypt my database file.How i can save it from getting stolen. pls help

  • Clint RutkasClint I'm a "developer"

    @cham3333: Sorry, hate doing this but that is outside the scope of the entire series.  I'd ask the question over at the forums on http://dev.windowsphone.com or www.stackoverflow.com

Remove this comment

Remove this thread

close

Comments Closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.