Part 12: Improving the View Model and Sample Data

Play Part 12: Improving the View Model and Sample Data
Sign in to queue

Description

Source Code: https://aka.ms/absbeginnerdevwp8
PDF Version: https://aka.ms/absbeginnerdevwp8pdf

Now that we've completed the basic setup chores, we want to focus on the heart of the application: the data model. This requires we translate our low-tech mockups from the previous lesson into a viable data model that best represents the data and hierarchical relationships between the data.

Here's our game plan for this lesson:

  1. We'll briefly analyze our "requirements" (the mockup is all we have) and will quickly diagram a data model that we'll implement in our app.
  2. We'll implement the data model in code.
  3. We'll create some data in an XML file that will be used as DESIGN-TIME data for the purpose of displaying something in our MainPage's XAML Designer.
  4. Modify the MainPage.xaml's binding expressions to point to instances of classes and properties in our data model.

1. Analyze our mockup and design a data model

After looking over the mockups and thinking about how best to delegate responsibilities to the various classes, I come up with a very basic class model that is my first best guess at how to structure the data I'll need for the app:

Generic Episode Image

The SoundModel class will contain references to each of the five groupings of sounds in the app. Notice that each of these properties are of type SoundGroup.

The SoundGroup class represents what I've been calling a "view" or "type". I've struggled for the correct terms, but in essence it is a grouping, not a view (which only connotes the visual aspect of the sound tiles) or a type (which has special connotations in the .NET world). A SoundGroup has a title (that will be used to display as the title of the PivotItem in the Pivot control on the MainPage.xaml) and a generic collection of SoundData.

The SoundData class represents the sounds themselves. Each instance of the SoundData will be displayed in a tile, and by tapping the tile, we'll play the sound associated with the SoundData as stored in the FilePath property.

Given this clarity, it's time to implement this diagram in code. I'm sure there will be a few additions as we go along, but this is a good start.

2. Create the new data model classes for our app

With a plan and our data model diagram in tow, we'll begin by implementing the new data model.

Right-click ViewModels folder, Add | New Item:

 

Generic Episode Image

 

... open the Add New Item dialog:

 

Generic Episode Image

 

  1. Make sure you're in the Visual C# file templates
  2. Select the Class file template
  3. Rename to: SoundData.cs
  4. Click Add button

The SoundData.cs file is created and loaded into the main area:

 

Generic Episode Image

 

... we'll add two public properties: Title and FilePath:

 

Generic Episode Image

 

Add another new item to the ViewModels folder using the technique described a moment ago. You'll create another Class called: SoundGroup.cs:

 

Generic Episode Image

 

In the new SoundGroup.cs, we'll create two public properties and a constructor:

 

Generic Episode Image

 

  1. Create the Title property and Items property. Items will be a generic list of SoundData.
  2. In the constructor, we initialize Items setting it to a new instance of List<SoundData>.

We'll add one more class file to the ViewModels folder called SoundModel. SoundModel will contain a SoundGroup for each "view" or "categories" of sounds we'll display in our app. It will also implement the logic to determine whether data has been loaded into the new data model, and the vast majority of the code therein will create instances of the SoundData classes for each sound we want to use in our app.

We'll start simple and grow this important class throughout this lesson:

 

Generic Episode Image

 

  1. In lines 11 through 15 we'll create properties representing each SoundGroup (i.e, category) of sounds.
  2. We'll use the IsDataLoaded property to determine whether or not we need to perform the LoadData() method to create instances of SoundGroup and SoundData.
  3. We'll load the data into the model using a number of private helper method which we'll implement in just a moment.

 

3. Modify the App.xaml.cs to use the new data model

Before we perform the bulk of the work in the LoadData() method (and create helper methods to the heavy lifting) I want to Angel replace the OLD data model (from the project template) in the app with our new one we just created by changing the class that the App.xaml.cs uses to back its ViewModel property.

First, I'll comment out references to the OLD model in the App.xaml.cs. I typically comment out things I no longer THINK I need before I actually delete them from the project. This is a habit I've acquired through the years. I've hastily deleted code thinking I'll replace it, only to realize there was some small detail I was missing. However, since I deleted the old working version and can no longer reference it, it becomes more time consuming and painful to determine where I made the mistake.

Here I comment out the implementation of MainViewModel property, both the private backing field as well as the public getter and setter:

 

Generic Episode Image

 

Next, I'll implement the new version of the ViewModel using our new data model type SoundModel:

 

Generic Episode Image

 

That should be all that's required. References to the SoundModel's IsDataLoaded property and LoadData() method should work just fine.

Now that I've removed the old data model from the app, I'm sure I've broken several other things. At this moment, I'm most concerned about the XAML views. The binding expressions are pointing at properties that no longer exist. Furthermore, at DESIGN-TIME, there's no sample data to display in the XAML designer, and at RUNTIME there's no data either.

 

4. Create sample / design-time display data

It will take some time to massage all of this back into working form, so I have to do this in small chunks. My first priority is to make some DESIGN-TIME data available so that I can then edit the XAML views and make sure they're displaying data from my new model correctly.

I'll right-click the SampleData folder, select Add | New Item ... from the context menu:

 

Generic Episode Image

 

... which opens the Add New Item dialog:

 

Generic Episode Image

 

  1. Make sure you're in the Windows Phone file templates
  2. Select the Windows Phone Portrait Page file template
  3. Rename the file to: SampleData.xaml
  4. Click Add

I'll use this file for sample data, not as a XAML page, therefore I'll need to remove everything about the templated file that makes it uniquely

When the new SampleData.xaml file opens in the main area, I'll remove all of the XAML inside that file:

Generic Episode Image

 

In the Solution Explorer, I'll delete the code behind file that's associated with my new SampleData.xaml file by right-clicking and selecting Delete from the context menu:

 

Generic Episode Image

 

Back in the SampleData.xaml, I hide the visual XAML designer. This file will contain just data, nothing that could be displayed in the designer:

I add quite a bit of code in the SampleData.xaml file:

 

Generic Episode Image

Notice the XAML Namespace prefix "vm"—it references the CLR namespace SoundBoard.ViewModels—the Namespace for the new data model we just created. We want the XAML in this document to create instances of our SoundModel, SoundGroup, and SoundData classes.

 

5. Use the new sample data in the MainPage.xaml

Next, I want to modify the MainPage.xaml file to change the design-time data (i.e., the data we're loading into our design-time) that the XAML Designer loads and displays:

 

Generic Episode Image

 

Now that we have a data model and real "fake" data, we can see the impact of changes we make in the DataTemplate for our LongListSelector. I'll make changes to the binding expressions in each of the TextBlocks:

 

Generic Episode Image

 

  1. I'll set the ItemSource binding of the LongListSelector to Animals.Items ... Animals is a public property of the SoundModel, which is the parent class that's defined in the SampleData.xaml file, which we set as the DataContext of the entire MainPage.xaml page. Therefore, every child property of the SoundModel in that file is accessible (such as Animals, Cartoons, Taunts, etc.).
  2. Since the LongListSelector.ItemsSource is set to Animals.Items, and Items is a List<SoundData>, we can reference any property of the SoundData class inside the DataTemplate. So, I set the Text binding to the Title property (of the SoundData class), and ...
  3. I set the Text binding to the FilePath property (of the SoundData class).
  4. I also set the Header binding in for the PivotItem to Animals.Title. Again, I can reference Animals because it's parent, the SoundModel, is set as the DataContext for the entire MainPage.xaml.

In the XAML designer, we should now see sample data appear:

 

Generic Episode Image

 

Hopefully you can see the correlation between the MainPage.xaml's controls and the sample data for Animals:

 

Generic Episode Image

 

We made a change to the FIRST PivotItem, and now we will change the second pivot item to display the SoundGroup Cartoons. Notice it's practically identical to what I wrote for Animals. I simply substituted Animals for Cartoons and it works.

 

Generic Episode Image

 

When I put my mouse cursor anywhere inside the XAML definition for the second PivotItem, the XAML designer changes its view to that PivotItem:

 

Generic Episode Image

 

Recap

To recap, the big takeaway in this lesson is how we replaced the default data model and sample data in the Pivot project template with our own new data model for working with groups of sounds and bound to the new sample data by modifying the MainPage.xaml's page and control declarations. We learned about data contexts and how they're set and accessed from within the page.

Embed

Download

The Discussion

  • User profile image
    Craig Goacher

    Hi Bob, Thanks for these videos I really enjoyed your C# tutorials and am hoping with these WP8 ones I might one day be able to develop an app of my own! I just wanted to share a strange little issue I 'hope' I have just managed to resolve...

    I'm 99.9% certain that I've followed your steps to the letter but when it came to building the solution to stop Visual Studio from questioning the SampleData.xaml file contents (as happens in your video), I was having a few errors thrown at me. The main error seemed to be 'The name "LocalizedStrings" does not exist in the namespace "clr-namespace:SoundBoard"'. The project would not build because of this.

    Google suggests that a number of people seem to get similar errors on occasions when just trying to use some of the standard WP templates for some reason.

    Anyway, when I realised that Visual Studio wasn't happy about the reference for LocalizedStrings in my App.xaml file (which I assume was there to begin with or had been added for me) I tried moving the LocalizedStrings.cs file in my project to 'within' the Resources folder and then a Build of my project made the errors go away.

    Hopefully this won't come back to haunt me further along the tutorials but it might also save someone some time if they run into the same problem.

    Thanks again for the tutorials!

  • User profile image
    Gabe Payne

    Bob, I believe that I am having the same issue as Craig was/is having. I got a long list of errors when trying to build the actual app.

  • User profile image
    Clint

    @Craig Goacher: and @Gabe Payne: can you guys email me the sample so I can quickly look into this where it doesn't compile?  clint.Rutkas@Microsoft.com 

  • User profile image
    Jamie

    @Craig Goacher and @Gabe Payne - I had a similar issue, and tried re-creating the project several times, triple checking my code.
    The fix for me was to build the app each time a change was made to anything relating to three class files. I'm guessing as the class files (and in turn the Data/ViewModel) are changed, the project needs to build to have the changes take effect?

  • User profile image
    RobertHrmn

    Yea, I'm having issues with the SampleData.xaml file too. getting squiggly lines all over it with the error that the namespace doesn't exit. triple checked and even copy pasted the namespace name with no luck. Even ran the completed solution I downloaded from here and get the same error's from it. 

    I also get the same "LocalizedStrings" error that the others received in addition to the Soundboard.ViewModels namespace errors I mentioned.

  • User profile image
    Richard Freytag

    Got stuck for a few hours with the same problem others have reported above where 'clr-namespace:SoundModel.ViewModels' does not contain the classes SoundModel, SoundGroups, SoundData or their members.

    After reading https://tinyurl.com/not-defined-in-namespace I decided to back out the changes to the basic Phone Pivot app and BUILD AT EVERY STEP!

    By going through each build and fixing compile-time errors I found the bug in MainPage.xaml.cs which caused the later XAML errors created when SampleData.xaml was added (such a large number of errors that they swamp the earlier errors trying to build MainPage.xaml.cs - which in turn caused the XAML parser [converter?] to fail to fill the 'clr-namespace:SoundBoard.ViewModels' namespace with the Sound* classes defined in their respective *.cs files.

    The bug in MainPage.xaml.cs appears to be a need to surround all uses of App.ViewModel with parethesis like so:

    Change line 22 in MainPage.xaml.cs from:
    DataContext = App.ViewModel;
    to:
    DataContext = (App.ViewModel());

  • User profile image
    RobertHrmn

    Yup, looks like that did the trick. I got tired of figuring out what was wrong with it so I just started over from scratch, this time compiling after every change and this time no errors at all and yet the code is the same form the old one to the new one.

    So is win phone 8 development really this picky/troubled that you have to build it after every page change or more just to make sure it works? I never had this kind of problems with asp.net web forms or mvc...

  • User profile image
    Clint

    @RobertHrmn: Model View can get tricky, more so with renaming stuff and adding stuff.  Life gets a lot easier if you don't want sample data but sample data makes life easy for designing with instant results Smiley

  • User profile image
    CodeSon

      Hi Clint, i've also run into the same challenge. Tried rebuilding at every step and  the change suggested in line 22 in MainPage.xaml.cs by Richard doesn't help either. Any other ways around this issue? Perplexed

  • User profile image
    rfreytag

    CodeSon - Richard here again (with a new UID).  

    I only mentioned one of 4 or 5 compilation errors in MainPage.xaml.cs I found at that step.  I had to fix them all before going on to add the SourceData.xaml file supplied in the SoundBoard folder that you had to download for the tutorial. 

    My operating hypothesis is that the XAML needs objects defined in C# which the compiler considers undefined until those errors are cleared up.  However, the XAML errors appear first and swamp out the C# error reports.  Stepping through and compiling the exercise frequently throws up the C# errors which, if resolved, allows the XAML to use the C# classes without any problem. 

    So my best advice is go back and make sure ALL the C# coding errors are gone before turning to the XAML errors. 

    Good luck. 

  • User profile image
    Clint

    @rfreytagMadCodeSon: I'd have to see what you guys did to help debug what is wrong.  I built the app from file->new so I know it works, I just did a lot of renaming of stuff however.

    if you guys could zip it up or even create a bug on codeplex with the zip, I'll take a look.  https://absolutebeginner.codeplex.com/workitem/list/basic

  • User profile image
    HarshDoshi

    Okay so I am facing the same error as you guys.. it gives me 35 errors of this kind

    The name "SoundModel" does not exist in the namespace "clr-namespace:SoundBoard.ViewModels". 

    I tried the line 22 fix that was suggested by Richard. I have not rebuild the entire solution with building solution at each step..

    So what do I do? Do that or is there any other work around here?

  • User profile image
    Clint

    @HarshDoshi: I'd verify in your SoundModel class what your namespace is.  Else, I'd suggest zipping up your solution (remove the bin/obj) and creating a bug at https://absolutebeginner.codeplex.com/workitem/list/basic so I can take a look quickly

  • User profile image
    HarshDoshi

     @Clint: I did my best to verify the code there.. couldn't find anything.. I zipped up the solution and uploaded to my skydrive here..  http://sdrv.ms/1cJ7aip

    was facing some problems on site above so uploaded it there.. Please check it out and let me know where I have made the mistake.. Thank you!

     

  • User profile image
    Clint

    @HarshDoshi: right way, your ViewModel you have this in your App.xaml.cs.  the compiler only sees that ViewModel as a method that should return an object of type object, not of type MainViewModel.

    internal static object ViewModel()
    {
        throw new NotImplementedException();
    }

    Change that line to:

    private static SoundModel _viewModel = null;
      /// <summary>
      /// A static ViewModel used by the views to bind against.
      /// </summary>
      /// <returns>The MainViewModel object.</returns>
      public static SoundModel ViewModel
      {
       get
       {
        // Delay creation of the view model until necessary
        if (_viewModel == null)
         _viewModel = new SoundModel();
        return _viewModel;
       }
     }

     

    and in MainPage.xaml.cs change

    DataContext = (App.ViewModel());

    to

    DataContext = (App.ViewModel);

     

  • User profile image
    HarshDoshi

    @Clint: Thank you very much this works! But im not sure that I completely understand it. Can you just explain me this in a simplified manner? Just a beginner so am confused atm! Thank you!

  • User profile image
    Clint

    @HarshDoshi: Best way I'd suggest is to download the source code from the series and compare this lesson's code to your own.

    A lot of it is I think you're getting confused on the syntax of what is a method versus what is a property.  You have ViewModel listed as a method (aka function) that returned an object when the application was looking for a SoundModel as a Property.

    A method typically performs actions and returns data, a property you can set / get data.  Think of it as "I have an dog".  My dog has a Name (property) and it can play Fetch(Toys toy).  I can do myDog.Name = "Fido"; and myDog.Fetch(Toys.Frisbee);

  • User profile image
    DHC

    I also had the "blue squigglies" in my SampleData.xaml code. The reason for the problem was in line 4. The code in the downloaded file is: xmlns:vm="clr-namespace:SoundBoard.ViewModel">
    But it should be:
    xmlns:vm="clr-namespace:SoundBoard.ViewModels"> (Note the "s" on the end.
    That should get rid the "blue squigglies."

  • User profile image
    Clint

    @DHC: so this actually is a difference between my base code and the code Bob's made for the videoes.  I had my folder / namespace as ViewModel and Bob had his as ViewModels

    Basically pick whatever matches the SoundModel.cs's namespace.  Since we want to sync with the video, this will be from the "Bob" version of the code.

    namespace SoundBoard.ViewModels

    {

    public classSoundModel : INotifyPropertyChanged .....

     

    Little things like this are what debugging is all about.  Things like an incorrect file path or ViewModels versus ViewModel.  Smiley

  • User profile image
    Wilfre

    Hi Bob, I recently started watching your series. I've followed your  steps exactly the same way you did, but I've been having error when I copy the code to SampleData.xaml I made sure the namespaces are right. It gives errors like this one:

    1 The name "SoundModel" does not exist in the namespace "clr-namespace:SoundBoard.ViewModel". 

    It says it for SoundData and SoundGroup also. I've tried to figure it out but I can't. Any help I can get will be appreciated.

  • User profile image
    Wilfre

    figured it out!

  • User profile image
    BobTabor

    @Wilfre: Great!  Just curious what it was (so I can help others avoid it in the future).  thanks!

  • User profile image
    Wilfre

    Hi Bob, the SampleData.xaml file that we download from the Source Codes...starts like this:

        xmlns:vm="clr-namespace:SoundBoard.ViewModel">

    In the last line the namespace is declared as 'SoundBoard.ViewModel' but in the videos we declare 'SoundBoard.ViewModels' (plural) so it was giving all kinds of errors. I know it's simple but since it's my first time seeing xaml it took me a little while. Hope it can help anyone. Thanks!

  • User profile image
    Clint

    @Wilfre: see my comment on this.  https://channel9.msdn.com/Series/Windows-Phone-8-Development-for-Absolute-Beginners/Part-12-Improving-the-View-Model-and-Sample-Data#c635119182494689493  Basically Bob and my base samples had a very small difference on our namespace.  Follow my comment and should resolve your problem

  • User profile image
    Abubakar

    @Wilfire: hahah thanx :D
    @Wilfire: oh i c thanx :D

  • User profile image
    Abubakar

    @Wilfire: hahah thanx :D
    @Clint: oh i c thanx :D

  • User profile image
    JacksonLeroy

    ok Im going nuts here. I have rebuilt this project multiple times, and I believe that I have done every suggestion I have seen here. I must be either misreading people or I am too tired. When I build, I get 31 errors all related to something not existing in the namespace "clr-namespace.SoundBoard.Viewmodels". I have attached a link to my project file. I just did the entire thing, sorry its 50mb.

    You can get my project file here: http://wernerclan.com/fbsharing/O7NM59PA

     

     

  • User profile image
    anzem

    I have noticed: then I create new class-file (SoundData.cs, SoundGroup.cs, SoundModel.cs) Visual Studio declares classes as "not a public" classes, so I have manually added "public" declaration for them. And it helps me with error "The name "SoundModel" does not exist in the namespace "clr-namespace:SoundBoard.ViewModels"

  • User profile image
    Clint

    @JacksonLeroy: can't seem to pull down the file.  based on what @anzem: said, I'm agreeing with him.  When in doubt like this, look at our sample code we provided and compare.  Every lesson has a matching solution to it.

    Big things to remember with c# is it is a case sensitive language and things like private, public and protected can change what is allowed to access different classes.  By default, a class / property / method is marked private if nothing is there.

  • User profile image
    Halil

    For those VB.NET guys:

    You should use
    xmlns:vm="clr-namespace:SoundBoard">

    instead of
    xmlns:vm="clr-namespace:SoundBoard.ViewModel">

    VB.NET and C# differs at this very point.

  • User profile image
    Clint

    @Halil: Why?  Is ViewModel(s) a reserved word in VB?

  • User profile image
    Szymon

    Still have a problem with SampleData. Error: The name "SoundModel" does not exist in the namespace "clr-namespace:SoundBoard.ViewModels".
    But when I try to open downloaded project in Emulator I do not have any text. Just empty screen

  • User profile image
    Clint

    @Szymon: chances are you have SoundModel instead of SoundModels.  be sure your name spaces and class names match up to what you have.

    If you still can't get it working even after comparing it to our finished sample for this lesson, create a bug over at https://absolutebeginner.codeplex.com/WorkItem/Create and I'll take a look.

  • User profile image
    usaidather

    i am having the same problem can you please help me out

     

  • User profile image
    Clint

    @usaidather: chances are you have SoundModel instead of SoundModels.  be sure your name spaces and class names match up to what you have.

    If you still can't get it working even after comparing it to our finished sample for this lesson, create a bug over at https://absolutebeginner.codeplex.com/WorkItem/Create and I'll take a look.

  • User profile image
    usaidather

    from where i have to change this soundmodel ...because i did in sample data .xaml and i guess nothing happend

  • User profile image
    usaidather

    i some how manage to reduce to 7 errors

    how to remove this error

     

       The name "SoundModel" does not exist in the namespace "clr-namespace:SoundBoard.ViewModels".   

  • User profile image
    usaidather

    well thanks its working now

  • User profile image
    Clint

    @usaidather: awesome.  debugging is major part of programming.

  • User profile image
    Dimitar

    Hey guys,

    I got the same error when building the project at SampleData part. The problem is the code that was given for the SampleData file has a spelling mistake

    line 4 "xmlns:vm="clr-namespace:SoundBoard.ViewModel">"

    is ment to be "xmlns:vm="clr-namespace:SoundBoard.ViewModels">"

    with the "s" at the end of ViewModel.

    Easy fix don't panic ! :)

  • User profile image
    dzuvu

    Hi everybody , i'm a newbie.
    I created project step by step but project build fail and show error :
    The name "SoundModel" does not exist in the namespace "clr-namespace:SoundBoard.ViewModels"

    How i can fix it ? Please help me

  • User profile image
    Clint

    @dzuvu: chances are you named it "SoundModels" instead of "SoundModel"

  • User profile image
    dzuvu

    Many thanks @Clint,
    I didn't change anything and restarted my visual studio, that error disappeared . I don't know what happened ^^

  • User profile image
    SickSpider

    Hello, I think I'm having something wrong and I'm pretty sure its not the "s" issue.
    Can someone take a look please!?!
    Here, its called "PivotTest"
    I did go off with naming and all but the classes structure is the same... I just missing something and not sure what.

    Thank You !!!

  • User profile image
    Clint

    @SickSpider: showed you what was wrong.  When in doubt, look at our sample for the lesson and compare.  Also remember that c# is case sensitive.

  • User profile image
    SickSpider

    @Clint:I tired and replayed...

    This goes worst than I thought... Sad
    Thank you!
    I hope its your working hours Wink

  • User profile image
    SickSpider

    Solved.
    I created my own and a bit different namespaces, all good.

    I replayed, thank you !

  • User profile image
    fengzili

    @Richard Freytag: i think it`s because your SoundData\SoundGroups\SoundModel classes are all "private". turn to SoundData.cs and change "class SoundData "into "public class SoundData"may be ok

  • User profile image
    ktl94

    Hi Clint,

    I too have the same problem, but not in the .cs files. None of them, I checked n I rechecked.

    I have the problem SampleData.xaml.

    In the SampleData.xaml you opened the <vm:SoundModel, but did not close it. And I get red squiggly lines all over <vm:SoundModel...... It tells me the namespace prefix <vm is not defined.

    Please help. Thanks a ton!

     

  • User profile image
    WSims
    Great work guys, Love it,
    One small problem Bob your killing me with the Ginos T Shirt, now I've got to get a load to Chicago.
    Maybe some time I can bribe myself in the door to shake your hand with a supreme LOL
    My wife used to live in Chicago always makes me bring a box down to Texas
  • User profile image
    BobTabor

    @ktl94: Try: SoundModels (see the 's' on the end)

    @WSims: Bringing a Supreme from Geno's is the best way to show up at my front door.  Tongue Out  Went on a first date with my wife of 23 years at Geno's so not only great pizza, great memories as well.

  • User profile image
    Stefanovic45

    I am facing the ViewModels versus ViewModel as well I think. The error I get: The name "SoundModel" does not exist in the namespace "clr-namespace:SoundBoard.ViewModels". I reverifies all the class names and the seems to be right. Could anyone take a look at my project please?

    https://absolutebeginner.codeplex.com/workitem/1294

  • User profile image
    TranHung

    Hi Bob, I encountered a problem when running this program on my phone lumia 625. It displays a black screen with the words "soundboard" and nothing more. I think there is a change in code , but when I dowloand and run the program again it still is. Why?

  • User profile image
    Clint

    @Stefanovic45: delete your SampleData.xaml.cs file, and look at the small tweaks I did I listed in the comments

  • User profile image
    Clint

    @TranHung: this lesson shows how to do sample data.  That data is for design time only (aka when you are in Visual Studio).  Lesson 14 shows how to hook up data when the application really runs.

  • User profile image
    TranHung

    Really. Thanks Clint, I will try to build the data with my visual studio. I hope it will work well.

  • User profile image
    Subscriber

     It displays a black screen with the words "soundboard" and nothing more.

     

    Same here, it all went hocuspocus with the sampledata xaml / soundmodel(S)

    Renamed the soundmodel.cs ( For the S /whitout the S ) in the solution explorer, and a wizard comes up and asks that i would like a rename of all the refferences for the code element "soundmodel".

    I clicked on Yes, and had no errors anymore during the build and the debug, 
    but on my little phony its the same thing as TranHung

    Can't swipe, can't do anything, except whatching at a  beautiful black screen  called "soundboard".

    Will look in to it tomorrow,
    it's here 04:36 in the morning.
    Good night Wink

     

  • User profile image
    Clint

    @Subscriber: First, this is for design time only for sampledata.xaml.  You won't see this at all in the emulator or on a phone which is run time.

    it isn't renaming the files, it is making sure what you are declaring in xaml is the same as  If you hit a roadblock, view the sample code that goes with the series as well.  The suggestion with viewmodel VS viewmodels is that the base code I wrote and the code Bob wrote for the lessons differ ever so slightly for the namespace here between viewmodel and viewmodels.

    When in doubt, look over the code from the lesson.  Every lesson has a complete copy of the project that matches what we covered

  • User profile image
    Subscriber

    Thx, wil go over it tomorrow.
    It looks quite simple, but in some things i just cant see the logic.

  • User profile image
    varunsinghal

    @JacksonLeroy

    i was also facing the exact problem (31 errors).

    after breaking my head for sometime , i realized that i had not declared all the classes(SoundModel , SoundGroup , SoundData) as public . Then i declared them as public and that did the trick , didn't even require the line 22 fix Big Smile .

  • User profile image
    varunsinghal

    @clint 

    Sir , 

    if i do not declare the SoundModel , SoundGroup , SoundData classes as public then why is the parser not able to find my classes in the namespace SoundBoard.ViewModels .

    and can you explain "Animals.Items"  in  ItemSource="{Binding Animals.Items}"

    in line 43 in MainPage.xaml.cs.

     

     

     

  • User profile image
    BobTabor

    @varunsinghal: Are you asking why public is required to expose methods from outside a class?  Or are you asking why public is required in this particular scenario?  If you don't understand the public keyword, I strongly recommend looking at the Channel9 C# Fundamentals for Absolute Beginners series.  

    re: Items ... that property references the collection that contains the data.  Try changing it to something else ... like just Animals.  Inspect the Animals class to see its properties and methods.  That binding expression needs access to the items collection to show a tile per sound.

  • User profile image
    varunsinghal

    @BobTabor:

    Thankyou sir for the animals.item explanation .

    and as for the public keyword , it was a stupid doubt and now i fully understand why public keyword was required in this scenario .

    Thanks for the assistance.

    and for the record

    yr series are super awsome , without them win phone app development would have been very tough .Thanks sir for making them easy.

     

     

     

  • User profile image
    Carlos Garcia

    I had the same "The name "SoundModel" does not exist in the namespace "clr-namespace:SoundBoard.ViewModels"" error that many people here had. In my case all my code was correct, but the problem was that I had created the ViewModel class inside App.xaml.cs using the "prop" code snippet. (It adds a set method that is not properly defined. If you delete the "set;" line, you get another error stating that the viewModel variable cannot be changed).

    All I had to do was delete this ViewModel function and write it again manually (without adding a set method), and the error was gone.

    I hope this helps anyone who comes across this issue. Bob, could you please explain why the compiler behaves this way? Thanks.

  • User profile image
    BobTabor

    @Carlos Garcia: Sorry, I have no idea.  :(

  • User profile image
    Abhijith

    guys i tried the steps mentioned by clint but they are not working for me...
    i renamed my names to ViewModels but still there are 31 errors in my code.. pls help me its frustating.. :(

  • User profile image
    nighilanth

    Before you start debugging try restart Visual Studio. It does helps with issues when you have classes in you ViewModel folder but app does not recognize them (hence 30+ errors). Learned it hard way :) 

  • User profile image
    John

    Bob,

    I am going on part 12 of your tutorials for windows 8 phone applications. I have really enjoyed your course so far and most likely will continue. You have a very fun attitude with creating this project which shows that you must really enjoy what you do which really helps the viewer learning get more excited about it as well. Great information and great videos all around as well.

    John

  • User profile image
    Dzung Tran Anh

    Hi Bob, I have a little trouble when following this your tutorial.
    That is in my MainPage.xaml, I have changed the code to bind to my custom data and my data has shown in design console. But when I debug the app on my device, the main page shows nothing!!!
    I have tried your sample code on my device and it does result the same.
    Could you please tell me what's happening and solution for this!

  • User profile image
    BobTabor

    @John: Thanks!

    @Dzung Tran Anh: Sorry, I've never heard of that happening.  I think it might be a problem with your install.  Just a guess.

  • User profile image
    Alfred

    Hi Bob. Thanks for the lesson, I'm just wondering if it is really necessary to update the MainPage.xaml file to change the design time data? It only affects the display for the designer? Shouldn't the real "beef" be with the real app stuffs at runtime, starting at App.xaml and App.xaml.cs? Just curious to know.

  • User profile image
    BobTabor

    @Alfred: Great question.  The only reason I think it's necessary is so that you can make sure your data is presented and styled correctly.  Personally, I like that.  But if you're not worried about that or you don't mind running the app a bunch of times to make sure the data is in there correctly, then you can ignore this.

  • User profile image
    swen

    Is there an option to add an image in SoundModel.cs
    Than we have an image as tile

    data.Items.Add(new SoundData
    {
    Title = "This is the title",

    FilePath = basePath + "sound.wav"
    >>>> Image= "assets/images/picture.png" <<<is this possible
    });

  • User profile image
    Jackpump

    The explanation of this video is awesome, thank you so much for this kind of videos!!

  • User profile image
    xia

    First All three classes are public,all are public!
    Second SampleData.xaml xmlns:vm="clr-namespace:SoundBoard.ViewModels" pay attention to the end of 's'

  • User profile image
    satishnair77

    Hi

    I am getting same error too < "SoundModel" does not exist in the namespace "clr-namespace:SoundBoard.ViewModels" >. I tried checking for the plurals (ViewModels) and added public keyword while declaring the classes. The code is identical.

    I get another error like: < SoundBoard.App.ViewModel.get' : not all code paths return a value > in App.xaml.cs. There is a blue line under the 'get' with the same msg.

    Any suggestions on what i could be doing wrong?

    Regards

    Satish

     

  • User profile image
    satishnair77

    Gosh..please ignore my earlier query. It was a simple typo error which i missed. In App.xaml.cs,  I was returning the viewModel inside the IF condition. :)

     

  • User profile image
    vickykorpal

    Hi! Greetings All!

    I am getting 36 errors regarding  "Error 2 The name "SoundModel" does not exist in the namespace "clr-namespace:SoundBoard.ViewModel" " and

    Error 32 'SoundBoard.App.ViewModel()' is a 'method', which is not valid in the given in the Project (SoundBoard)  {This one I got after changing the Data Context}

    I am pretty sure that  I followed @bob. Also if I change the Data Context = (App.ViewModel());

    However No Luck! I am really new to C# and building the apps. This is gonna be my first app to prepare. If someone can please help me out. I will appreciate. I can zip the code if you need it.

     One more question what is the difference in between ViewModel and viewModel? (capital letter "V" and Lower Case v)

  • User profile image
    Marcio Carvalho

    In case someone are still with namespaces errors and tried everything above, as I do:

    I was with 39 errors poiting namespaces and telling that SoundModel doesnt exists in ViewModels.

    I figure out that while I was watching the video and typing the code on App.xaml.cs file I aciddentally put the "static SoundModel ViewModel get" as "private" cause the "auto-fill", and the right is it being "public".

    Maybe some people are having this trouble, and the reason is the same as was mine.

    After put it in public everything is working fine..

    Regards




  • User profile image
    Ansh

    Hello Bob, The error i am getting is that after compiling;in sampledata.xaml it is showing "input string is not in a correct format"
    with blue squizly lines all over FilePath attributes!!
    Kindly help

  • User profile image
    Tainhx

    I fixed error " SoundModel does not exist in namespace clr-namespace:SoundBoard.ViewModels" ... This is my way : after add 3 class (SoundModel,SoundData,SoundGroup) , you have to build solution and after then , add SampleData.xaml in SampleData folder.
    Regards!

Add Your 2 Cents