Thursday, February 19, 2009

If you are looking to learn about Silverlight development, here is your chance.  Our local Microsoft Developer Evangelists are running a free full day event this Saturday featuring a good variety of information. 

To learn more or register, click here.

If you are a beginner or haven't gotten started yet, this is a great opportunity.  I'm not a beginner and my weekends are pretty busy these days.  So initially, I was going to skip this event.  But from my conversation with Dani Diaz last night, it sounds like there will be a decent range of information here, so I think I will attend too. 

I don't know the full agenda but some of the speakers include Dani, Lindsay Rutter, Bill Wolff and Pete Brown.  That's a good line up but to be perfectly honest, Pete Brown is the big draw for me.  I've been reading his blog since I started doing Silverlight development, this guy really knows his stuff.  One thing I've learned over time is that if a good speaker if giving a talk, even on a topic that I think I know fairly well, it is worth attending.  You never know what kind of tidbit you may pick up from them!

Hope to see you there.

Thursday, February 19, 2009 9:42:51 AM (Eastern Standard Time, UTC-05:00)  #    Disclaimer  |  Comments [2]  |  Trackback
 Friday, February 13, 2009

One thing I really hate about doing Silverlight development is trying to search for good information on the Internet.  When I look for a solution to a problem I am having, I always run into a lot of "noise" in the form of outdated posts that aren't relevant anymore.  There are a ton of questions, answers, and sample solutions on the web from developers working with the Silverlight 2 Beta release.  I know this was all helpful information to share at the time, but a lot of it is useless now.  The problem is, sometimes you have to read the post a bit in order to figure out that the developer was on the Beta.  I've gotten to the point where the first thing I do is look at the date on the post before I read on.  What makes this even more complicated, is sometimes the information in these posts is still relevant and very helpful! 

Oh well, at least we are lucky to be developers in an age where so much information is shared among members of the community.  Can you imaging having to read manuals for all your information ;) ?  I don't mean to discourage anyone from posting.  Don't take me too seriously, I'm just ranting.

Friday, February 13, 2009 8:45:17 AM (Eastern Standard Time, UTC-05:00)  #    Disclaimer  |  Comments [4]  |  Trackback
 Tuesday, February 10, 2009

The other day I was working on a Silverlight app when I got this error:  Layout Cycle Detected.  The good news is that Silverlight actually gave me an error message that meant something.  That is unusual for Silverlight.  Of course the message didn't tell me what caused it, or how to fix it!  Luckily, there is this thing called the Internet ;-).  I found some information on David Yack's blog (I don't know him) that helped.

It turns out that Silverlight doesn't like it if you put too many TextBoxes in a ListBox (or possibly any other "repeater" kind of list control).  I'm not sure why it only applies to TextBoxes and not other controls, but here is my understanding of the problem...  Each time you add a TextBox to a ListBox it needs to figure out how much space to give the control, as well as all the other controls around it.  It figures that out, and then sizes all of the controls that don't have specific sizes set, and then lays everything out for you.  But I guess in a list, it needs to do this over and over and over again and Silverlight thinks it has gone into an endless loop, so at a certain point it cuts it off and says "Layout Cycle Detected".  Each time a control gets resized, the controls around it get resized, and that goes on and on until you get to the top level control.  I guess I can understand that it could be a problem, but I think the developers at Microsoft have got to come up with a better way to resolve this.  If I want to put a lot of TextBoxes in a list, I should be allowed to do so!  Also, the limit is supposedly 250 TextBoxes, but I am pretty sure I had less than that.

There are a few ways to get around the problem, as far as I know.  In my case there are a bunch of controls that made up the DataTemplate for my ListBox.  Since they are in a StackPanel, I put a fixed size on the StackPanel.  That prevents the "bubble up" of resizing going all the way to the page level.  That seemed to fix my problem most of the time, but not all of the time.  I could probably have kept playing with the layout and putting fixed sizes on more panels.  Instead, I gave in. 

I didn't want to fight with Silverlight any more so I changed all of my TextBoxes to TextBlocks.  Then I set the the ListBox items up so that you can switch them into "Edit Mode".  The problem here is that the ListBox doesn't have built in support for that.  So here is how I did it.  By the way, this is not dependent upon the "SelectedItem" in the ListBox, so you can have multiple items in EditMode at the same time.

The purpose of my list is to display a list of Services to the user.  My ListBox is bound to a collection: ObservableCollection<ServiceListItem>.  ServiceListItem is just a class that represents my data.  Among it's other properties, the ServiceListItem has a property for IsInEditMode.  It is nothing fancy:

            public bool IsInEditMode
            {
                get { return _isInEditMode; }
                set
                {
                    if (_isInEditMode == value)
                        return;
                    _isInEditMode = value;
                    NotifyPropertyChanged("IsInEditMode");
                }
            }
            private bool _isInEditMode;

In my DataTemplate for the ListBox I have two StackPanels, one for ReadOnly mode and one for Edit Mode.  The ReadOnly template has a button to switch to EditMode.  The EditMode template has two buttons.  The Cancel button reverts the data to it's original state and switched to ReadOnly mode.  The Save button saves the data and switched to ReadOnly Mode.  Here is the one that puts the template into Edit Mode (nothing fancy here):

<Button x:Name="EditService" Click="EditService_Click">
    <Button.Template>
        <ControlTemplate>
            <Image Source="../images/pencil.png" 
                   Height="10" 
                   Width="10" 
                   Cursor="Hand" 
                   VerticalAlignment="Center" 
                   HorizontalAlignment="Center"/>
        </ControlTemplate>
    </Button.Template>
</Button>

The code for the button click is pretty simple too:

        private void EditService_Click(object sender, RoutedEventArgs e)
        {
            ServiceListItem item = (ServiceListItem)((Button)sender).DataContext;
            item.IsInEditMode = true;
        }

Actually, the only cool part of this is how the control figures out which StackPanel to display.  Each one has it's Visibility Property bound to the IsInEditMode property!

                           <StackPanel x:Name="EditPanel"
                                       Visibility="{Binding IsInEditMode, Converter={StaticResource BoolVisibilityConverter}}" 
                                       Orientation="Horizontal" Height="20" >

The trick is the Converter.  We're using a Converter that converts a boolean value into a value from the Visibility Enumeration:

 

    public class BoolVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null || string.IsNullOrEmpty(value.ToString()) || !(bool)value)
                return Visibility.Collapsed;

            return Visibility.Visible;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            // we don't need it to convert back but we could implement that later.
            return value;
        }
    }
Don't forget to add the Converter to your user control's resources:
    <UserControl.Resources>
        <ClientUtilties:BoolVisibilityConverter x:Key="BoolVisibilityConverter" />
        <ClientUtilties:BoolInverseVisibilityConverter x:Key="BoolInverseVisibilityConverter" />
    </UserControl.Resources>
And it all just works!
From this, you should be able to figure out the other end of this, switching back to Read Only Mode. 
I hope this helps.  
Tuesday, February 10, 2009 10:01:36 PM (Eastern Standard Time, UTC-05:00)  #    Disclaimer  |  Comments [1]  |  Trackback

If you read my last post about Ben, you'll know that the winter wasn't going so well.  It seemed like Ben was always sick and in a "bad mood".  Well not anymore!  The little guy that we love is back and crazier than ever.  One day he just changed completely back to his old self.  These days he is sleeping great, eating great, playful as can be and all about having fun!

Ben is loving "real" food.  He really wants to feed himself so we let him do that most of the time.  He eats pasta, scrambled eggs, sweet potatoes and regular potatoes, chicken nuggets, fish sticks, pears, turkey meatloaf, veggie burgers, pizza (crust), peas, broccoli, carrots, mac and cheese and a bunch of other stuff.  He watches what Mommy and Daddy eat and always wants food off of our plates.  Plus, he is taking his bottles more too.  So all that is going well.

Play time is completely different these days.  Benny really seems to "get" the game.  I don't know what goes on in his mind but he is really starting to learn things.  The little gears are really turning in his head.  He loves to play with his Fisher Price "House".  He plays peek-a-boo by lifting the window up and down.  He also loves his "Little People".  And he has car that he can ride on and walk behind.  We are noticing more hand coordination too as he can put balls into a slot or put his blocks together (sometimes).  He's very proud of himself too, especially when he is walking (with our help), standing - yes on his own for a few seconds at a time, and other fun stuff.  Yesterday I said "How big is Benny" and up went his hands.  I had been trying that for a while with him with no result.  But it just clicked and how he does it all the time. 

Ben is really looking forward to turning 1 year old.  He had a lot of fun with his cousin Grace at her Birthday party.  She's older by 10 days!  Check out the picture of them together, it is pretty funny because Grace is big for her age, while Ben is pretty small.

OK, here are some of my favorite pictures from the last few weeks.  Oh, by the way, Benny got his first haircut too.

DSC03884 DSC03915 DSC03922 DSC03967 DSC03976 DSC03982 DSC03983

 | 
Tuesday, February 10, 2009 8:35:26 PM (Eastern Standard Time, UTC-05:00)  #    Disclaimer  |  Comments [0]  |  Trackback
 Tuesday, January 27, 2009

If you are working with LINQ to SQL, you may be familiar with this exception message:

"Cannot add an entity with a key that is already in use."

This typically comes up when you are working with LINQ to SQL, as I am, in a "disconnected " situation.  In my case, I am working with a Silverlight Application, so I get some data with one DataContext, then pass it out to my Silverlight application (this is the same with any other web application), make changes to it, pass it back to the server where it gets updated using a different instance of my DataContext.

The interesting thing in my case is that I thought I fixed the problem but I still got the exception.  I'll explain why.  But first, there are several ways you can resolve this issue.  How you choose to fix this will have to do with how your database is set up initially.  I am working with an existing database and there are limits to how I can change the db.  So I have my all of the properties in my LINQ to SQL Entities set as "UpdateCheck = UpdateCheck.Never".  Next I'll "Attach" my entity to my DataContext  but before I do that, I'll "Detach" it  My Detach() method "resets" all of the EntitySets and EntityRefs connected to my entity so that LINQ to SQL doesn't think I am trying to insert them into the database.  Sorry, I'm not going into details on this process in this blog post but you can easily find details on the web.  Now I can attach my disconnected entity to my DataContext like this: 

            using (ABCDataContext db = new ABCDataContext())
            {
                foo.Detach();
                db.Foos.Attach(foo, true);
                db.SubmitChanges();
            }

This should work fine.  But it wasn't working in my case.  Why?  It is a typical case of how various steps I took to resolve the a different problem ended up causing new problems.  I was debugging and trying a bunch of stuff because my calls to Attach were throwing exceptions.  There is an overload to the Attach() method that would look like this -- db.Attach(newEntity, originalEntity)  -- so that LINQ to SQL can compare the two items.  I had been playing around with it but when I commented out my code, I forgot to comment out one line, so it looked like this:

            using (ABCDataContext db = new ABCDataContext())
            {
                foo.Detach();
                Foo oldFoo = db.Foos.SingleOrDefault(f => f.FooId == foo.FooId);  // NOT REMOVING THIS LINE CAUSED THE EXCEPTION!
                //db.Foos.Attach(foo, oldFoo);
                db.Foos.Attach(foo, true);
                db.SubmitChanges();
            }

When I run this, I get the exception "Cannot add an entity with a key that is already in use".  For some reason, even though I wasn't working with "oldFoo", LINQ to SQL didn't like the fact that I requested it from the database.  So I commented out that line (Foo oldFoo = db.Foos.SingleOrDefault...) and everything just started working fine. 

It was a pain but it is over now.  Now it is time to solve more problems...

Tuesday, January 27, 2009 10:10:34 AM (Eastern Standard Time, UTC-05:00)  #    Disclaimer  |  Comments [0]  |  Trackback
 Saturday, January 24, 2009

It's been a while since my last entry of The Ben Chronicles.  As I look through my collection of photos I see why.  I have taken very few photos of Ben since Thanksgiving.  There's a few reasons for that.  One is that my camera is on the fritz.  But hopefully some new rechargeable batteries will solve the problem.  The bigger issue is that Ben was sick a lot.  In December I think he was sick more days than he was healthy.  First a stomach bug (no details but that was fun), then  a cold, than another cold, than another, etc.  Most of these weren't anything major but one time he gave us a scare when his hole throat puffed out.  Turns out it was just really bad swollen glands due to all of the colds he has had.  Anyway, he was sick during Rebecca's entire winter break which was really a bummer.  Each time he goes back to daycare and then a few days later he is home sick again.  But he seems to be pretty healthy these days (or should I say, today).  People warned me that the first year at school or daycare is when kids get sick the most. 

When he is feeling good, Ben is a bundle of fun.  He's really curious and he is really stubborn.  He loves to crawl around and play with stuff.  He loves being tickled and laughs a lot!  He's not so interested in baby food any more and wants to eat "real" food.  He really likes to feed himself.  I think all of those sick days made him a little more needy because he wants to be held more than ever.  Or maybe it is just a phase he is going through.  He isn't gaining a lot of weight these days, and all those sick days didn't help, but he still seems to be looking a lot bigger.  As he gets close to 1 year old, he really is changing into less of a baby and more of a little boy.  But we still have a long way to go.

 

DSC03868 DSC03873 DSC03875 DSC03877 Baby Smash2

 | 
Saturday, January 24, 2009 1:15:39 PM (Eastern Standard Time, UTC-05:00)  #    Disclaimer  |  Comments [0]  |  Trackback
 Thursday, January 22, 2009

I've already blogged about how cool Live Mesh is, you can read my previous post here.  Well the other day I had a great idea to synchronize my Internet favorites for all of my machines in Mesh.  It was really easy to do.  Of course I am not the first person to have this idea and there are lots of blog posts out there already about how to do it.  Here is the basic idea... Just find the folder on your computer where you already save your favorites.  Add that folder to your Mesh.  Make sure your other machines are synced to that folder too (you can do that when you add the folder to your Mesh, or you can go to your Mesh Desktop and set it there too).  Then on your other computers, you can specify where you want the new folder to go.  Just set it to replace (actually you can merge the two of them) your existing Favorites folder on each machine.  If you have used Mesh at all you will see this is very easy to do.

Some other people have already taken the time to explain it in detail here, here, and here.

 

Technorati Tags: ,
Thursday, January 22, 2009 11:12:02 PM (Eastern Standard Time, UTC-05:00)  #    Disclaimer  |  Comments [0]  |  Trackback
 Sunday, January 18, 2009

As an ASP.Net developer, one of my favorite tools is Firebug.  Generally, my default browser is IE, but when I develop web sites I always debug and test with Firefox with the Firebug Add-on to make designing the pages easier. If you are a web developer and you aren't familiar with Firebug you must check it out.  It basically allows you to inspect your page in the browser at runtime.  With Firebug you can see how your ASP.Net Code gets rendered into HTML, and how the CSS is applied.  Often it was not as I expected!  You can even modify the html, CSS and JavaScript at runtime.  This is a great help because you can tweak the page in the browser to look the way you want and then go back and apply the changes to your source code.

Now I am developing a Silverlight Application.  The learning curve is steep, working with XAML I am starting from scratch.  It isn't like HTML at all and it is not so easy to make controls look like you want the to.  And it was even harder because I didn't have Firebug to help.  Searching the Internet, I found Silverlight Spy.  This is a cool, free tool that helps in many ways with Silverlight Development.

With Silverlight Spy you can inspect your XAP package, monitor events, network activity and performance and more.  But the best part is that I can inspect the elements within my XAML at runtime and even make changes.  This is pretty important because I find that the XAML design experience in Visual Studio is less than perfect.  I've already used Silverlight Spy to help me solve a bunch of problems.  The tool isn't perfect, but it is BETA software and it is free, so I am not complaining.  Many, many thanks to the developers.

 

Sunday, January 18, 2009 9:20:44 PM (Eastern Standard Time, UTC-05:00)  #    Disclaimer  |  Comments [1]  |  Trackback
 Tuesday, January 13, 2009

Sometimes strange things happen.  It happened to me, I figured it was a fluke.  But last week it happened to my co-worker too.  Weird right?  Everything was working great with my VS Solution including a Silverlight App.  It isn't new and I've debugged it many times.  But for some reason, no matter what I did, the debugger seems to be ignoring my breakpoints.  At first I thought that maybe the code wasn't getting called, but it sure seems like it was getting called.  If you find yourself in this predicament, here's all you need to do.   Go to the Property Pages of your Web App (the application that hosts the Silverlight).  You get there by right clicking on the Web Application in the Solution Explorer and select Properties.  Then choose the "Web" tab and you'll see the one below.  If debugging doesn't work, the "Silverlight" checkbox is probably not checked!  I'm not sure how this setting gets reset, but at least it is easy enough to fix.

Enable Silverlight Debug

 

Technorati Tags: ,
Tuesday, January 13, 2009 8:35:02 PM (Eastern Standard Time, UTC-05:00)  #    Disclaimer  |  Comments [0]  |  Trackback