Live blogging a XAML project with CodePlex - Now with WPF

OK, we have th game basics, we have enforced the game rules, now we need to make it playable.  In order to do this (according to the original requirements) we need to

  1. Add a little pizzaz to the notifications
  2. Handle it when the player looses a letter

So I found that in order to proceed with this project, I needed to define a few terms.

  • XAML - it's basically HTML.
  • WPF - It's ASP.NET, or Cold Fusion, or JSP.  Control that render XAML, like the ASP.NET controls render HTML.

So where does this leave us?  A whole, totally new library to learn, after having already learned HTML since 1992 when it had twelve tags.  Oh, and you windows programmers?  You too.  This replaces EVERYTHING - Windows "ActiveX" and web.  It's global!  Yay.  Replaces Flash even - WPF and XAML run Silverlight.  Rah.  I can't even hide my excitement.

So why am I upset?  Well, I like HTML.  It does what I need.  But I understand that it doesn't fill 100% of what people want, and XAML with WPF probably will.  Problem is when you make a tool that meets everyone's needs, it becomes unusable.  As that say the docs are 'tl;dr;: 'too long; didn't read'.  There are hundreds of options - in HTML there are a few dozen.  Some say that constrains creativity, I think it enhances it.  To each their own.

Polymorphic animation.

Anyway, it's a technology that the biggest software company in the world has embarrassed so I should learn it right?  Right.  There are at least two ways to put text in the viewable area - the letterBox, which we are using for the Game Piece, and the textblock.  The TextBlock is what is needed for the Wow! animation.

One of the nice things about XAML is that it is polymorphic.  Polymorphic is the property of being able to be context sensitive.  If you have a fruit class, and there is a Fruit.Peel method, a polymorphic Peel method will know that you take the skin from a banana, take the seeds out of a pomegranate and don't bother with a grape unless you are making pie.  (No really.  My stepmom just made a grape pie last month.  It was yummy.)

I am pontificating and this is supposed to be a liveblog of a project.  Let it be said that you can apply an animation to an object, and they are truly polymorphic.  Therefore, we have a textblock, and we want to apply two animations to it.  The animations will automagically treat the textblock properly, thus showing us their polymorphic nature.

Enough of that

I want a flying 'Wow'.  To do this, I need to animate two properties: the scale of the block, and the opacity of the text.

There are two ways to implement an animation in WPF.  You can use a storyboard in XAML, which is more of a Microsoft Expression thing to do, or you can apply the methods in code, which is what I want to do.  This is a VB blog, after all.

To animate the scale, I have to add an empty ScaleTransform object to the TextBlock.

<TextBlock x:Name="wowTextblock" Opacity="0" Foreground="Green" HorizontalAlignment="Center" VerticalAlignment="Center" Text="Wow!" Visibility="visible">
    <TextBlock.RenderTransform>
        <ScaleTransform></ScaleTransform>
    </TextBlock.RenderTransform>
</TextBlock>


This isn't necessary with the Opacity animation because the Opacity is already a property of the TextBlock.  I know that Width and Height are too, but scaling them isn't.  I suppose I can get behind that.  Anyway.

To animate the block, I am going to make two DoubleAnimation objects, and then BeginAnimation them one after another.  The Opacity I can directly apply, but the Scale I have to go get from the XAML and then set a few properties for.  I packed the whole thing into a subroutine I called ShowWow.  I will replace the old MessageBox.Show with a call to the new ShowWow method.  Here is the code:

    Public Sub ShowWow()
        'Make an animation for the scale
        Dim wowAnimation As New DoubleAnimation(0, 100, New Duration(New TimeSpan(0, 0, 3)))
        'Make another for the opacity
        Dim wowOpacity As New DoubleAnimation(1.0, 0.0, New Duration(New TimeSpan(0, 0, 3)))
        'Go grab the ScaleTransformation from the XAML and se tthe properties
        Dim wowTransform As ScaleTransform = DirectCast(wowTextblock.RenderTransform, ScaleTransform)
        wowTransform.CenterX = 11
        wowTransform.CenterY = 7
        wowTransform.ScaleX = 1
        wowTransform.ScaleY = 1
        'Then run all of the animations
        wowTransform.BeginAnimation(ScaleTransform.ScaleXProperty, wowAnimation)
        wowTransform.BeginAnimation(ScaleTransform.ScaleYProperty, wowAnimation)
        wowTextblock.BeginAnimation(TextBlock.OpacityProperty, wowOpacity)
    End Sub

Of course, you can download the whole thing from the codeplex site for the project.  Next time, we'll handle missed letters, and then call it a project.  Have fun!  I hope this is being some help to those working in WPF.

Live blogging a XAML project with CodePlex, Part Duex

And we are back at the Letters Are Falling project, which has it's own Codeplex URL now ... heh.  If you remember from the last post, we need to

  1. Randomize the letter.
  2. Randomize the start position right to left.
  3. Make it more fun when you win.
  4. Handle it when you lose.

I am going to start with the first two.  For now, I am going to keep this pretty declaritive, and try and use the WPF objects 'correctly' later once I get the bugs worked out of the logic.

Let's add two functions: one for the left randomization and one for the letter randomization.  The left randomization needs to set the left margin anywhere from zero to the width of the window minus the width of the letterBox.  The letter randomization needs to pick from a selection of letters, which we will later make configurable.

Now, certain other things have to happen to make this a game.  It has to restart when you get the right letter, keep score, things I forgot in the original requirements.  In order to do this, I have to set up the game to reset the letterbox, and not have all of the code in the form load event.  To do this, I have built a new function to set up the letterbox, which calls the two new randomization functions.  Then I can change my keypress event to make some gametime decisions, including a Quit function.

The VB file now looks like this:

   1:  Class gameBoard
   2:      Public WithEvents gameClock As New DispatcherTimer
   3:      Private letterBoxTop As Double = 0
   4:      Private letterBoxLeft As Double = 0
   5:      Private scoreCount As Integer = 0
   6:      Private Sub gameBoard_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
   7:          SetupLetterBox()
   8:          gameClock.Interval = New TimeSpan(0, 0, 1)
   9:          gameClock.Start()
  10:      End Sub
  11:      Private Sub SetupLetterBox()
  12:          letterBoxLeft = GetLeftMargin()
  13:          letterBoxTop = 0 - letterBox.Height
  14:          letterBox.Margin = New Thickness(letterBoxLeft, letterBoxTop, 0, 0)
  15:          Dim currentLetter As String = GetLetter()
  16:          letterBox.Content = currentLetter
  17:      End Sub
  18:      Private Sub LetterPress(ByVal sender As System.Object, ByVal e As System.Windows.Input.KeyEventArgs) Handles MyBase.KeyDown
  19:          If e.Key.ToString.ToUpper = letterBox.Content.ToString.ToUpper Then
  20:              gameClock.Stop()
  21:              scoreCount = scoreCount + 1
  22:              MessageBox.Show(String.Format("You got it!!{0}Your score is {1}!!", vbCrLf, scoreCount))
  23:              SetupLetterBox()
  24:              gameClock.Start()
  25:          ElseIf e.Key = Key.Escape Then
  26:              MessageBox.Show(String.Format("Goodbye!{0}Your score is {1}.", vbCrLf, scoreCount))
  27:              MyBase.Close()
  28:          End If
  29:      End Sub
  30:      Private Sub ClockCheck() Handles gameClock.Tick
  31:          letterBoxTop = letterBoxTop + 10
  32:          letterBox.Margin = New Thickness(letterBoxLeft, letterBoxTop, 0, 0)
  33:      End Sub
  34:      Private Function GetLeftMargin() As Integer
  35:          Dim result As Integer = 0
  36:          result = (New Random().Next) Mod CInt(gameBoard.Width - letterBox.Width)
  37:          Return result
  38:      End Function
  39:      Private Function GetLetter() As String
  40:          Dim result As String
  41:          Dim lettersList As String() = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}
  42:          Dim listIndex As Integer = (New Random().Next) Mod lettersList.Length
  43:          result = lettersList(listIndex).ToString
  44:          Return result
  45:      End Function
  46:  End Class

In the next edition of Live Blogging a XAML project, we will get into a little more of the XAML itself, including getting rid of the messagebox, and adding a little pizzaz!  Until next time...

Live blogging a XAML project with CodePlex

So, I am writing a game for my son.  It's a Raining Letters type game.  You know, where a letter falls from the top of the screen and the player has to find it and push it on the keyboard before it gets to the bottom of the screen?  Adam has a little hand held game that works this way, but he is ALWAYS on his PC these days so I thought this would be better and a good way for me to learn XAML in the Windows world.  Teach him the ABCs now and teach him the XAML later, right?

Anyway, I started to chat about it on Twitter, and I decided that 1) all of the things I learned would get lost quickly and 2) I would drive everyone absolutely nuts.  So I decided to write an epic blog entry about what I learn.

I am doing this the way I write most of my prototypical applications, and starting very simple and adding in functionality.  I started, then, with a WPF project in VB.  It comes with a window; I added a label in the designer and a timer in the VB file.  On load, I set the Margin of the label to a new thickness with a left of a random number and the top up above the top of the screen.  On the timer click , I add 10 to the top thickness.  When the user clicks the right letter on the keyboard, they win.  Simple.  I'll make it better as I go.

Whoops.  Timer didn't work.  WPF components are compartmentalized.  Didn't know that.  Found a blog entry that helps me out, points me to DispatcherTimer instead.  Gotcha, I can do that.  It is in the System.Windows.Threading class, for which I need to add a reference.  Uses a 'Tick' event rather than an 'Elapsed' event.  that makes me think the windows guys and the Framework guys at Microsoft need to get together and have a beer.

Anyway, I have a working game!  Only does one letter, and not a lot of fun yet, but it's a start.  Here is the XAML:

<Window x:Class="gameBoard"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="The Letters Are Falling!" Height="480" Width="640" Name="gameBoard">
    <Grid>
        <Label Name="letterBox" FontSize="100" HorizontalAlignment="Left" Width="71" Height="110" VerticalAlignment="Top">A</Label>
    </Grid>
</Window>

Here is the VB code:

Class gameBoard
    Public WithEvents gameClock As New DispatcherTimer
    Public letterBoxTop As Integer = 0
    Private Sub gameBoard_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
        letterBoxTop = 0 - letterBox.Height
        letterBox.Margin = New Thickness(100, letterBoxTop, 0, 0)
        gameClock.Interval = New TimeSpan(0, 0, 1)
        gameClock.Start()
    End Sub
    Public Sub LetterPress(ByVal sender As System.Object, ByVal e As System.Windows.Input.KeyEventArgs) Handles MyBase.KeyDown
        If e.Key.ToString = "A" Then
            gameClock.Stop()
            MessageBox.Show("you win!")
        End If
    End Sub
    Public Sub ClockCheck() Handles gameClock.Tick
        letterBoxTop = letterBoxTop + 10
        letterBox.Margin = New Thickness(100, letterBoxTop, 0, 0)
    End Sub
End Class

Now I have a proof of concept, right?  I just need to dig in and get a few features installed.

  1. Randomize the letter.
  2. Randomize the start position right to left.
  3. Make it more fun when you win.
  4. Handle it when you lose.

I think I need to work on this tomorrow.  It's a little late and I have a three year old that will be up at 7.  I want to check this into CodePlex.  I have an account there but have never used it.  Wonder how it works.

Go sign in and Create a Project.

The Letters Are Falling! has been created

Congratulations, you've successfully created your project, theLettersAreFalling, on CodePlex.com.

Great!   Now what?  OK, I apparently have 30 days to publish.   Well heck I want to NOW.  Oh, I have a link.  Let's see what's there.

Ah, I need to do a few things.  Edit the homepage.  Wikitext, gotcha.  Done.  Upload the source.  I need to download and install TotriceSVN for Subversion.  Mmmmm .... done.  Give them a few bucks via PayPal.  Reboot.  Grrr ... do that in a little bit.  Freakin windows menu system.

Alright.  Back from reboot.  Right click on the Folder and Import from the TortiseSVN menu.  Put int the URL, username and password CodePlex gave me ... easy.  Wow.  I need to use this for the book code.  Alright.  Moving on.

Pick a license - easy.  Same as the book code - share and share alike!  Oooooh ... not so easy - CC licenses aren't in CodePlex.  I remember reading about this.  Booooooooo.  Damn, now I have to research for a bit.  Let's go with Stallman - he is smarter than me.  Copyleft - the way this stuff should be, right?  I select the GPL and move on.  Done.

Clicking Publish this Project.

Hey, now.  Look at that.

Alright, I am using this for evrything from now on.  Man, are ALL of these repositories this good?  I shoulda been doing this a while ago.  VB for Dummies 2008 code going up ASAP.  Everyone get a codeplex account!  New favorite software.

Anyway, tomorrow we do the RAD rotation, and add features to the POC.  Isn't this fun?

Have a good evening!

Hanselman has it pegged when it comes to language

I get a lot of crap for being a VB programmer.  People who think it is cool to be smarter than someone else like to point out that they code in C# (though it was Java last year and C++ the year before) and I keep writing programs that work in whatever language best suits.  Why?  I learned how to program, not how to program in X language.  I recommend it for everyone - I recommend it in the book, and Scott Hanselman says it better than I ever could.

When programming (that is, expressing your intent to the computer) you should select a language that matches up with the program you're trying to solve. Every language is, in a way, a Domain Specific Language.

Regardless of what your language of choice is, you might be someone who says all languages eventually become, or try to become Lisp, or you might think Visual Basic is the best or that PHP is God's Language, you should learn a new language every year. I code, and have coded, for many years in C#, and before that C++, but it's important for me in my personal development to remember why I learned Haskell and Lisp (I suck at Lisp) and Smalltalk, and why I return to them occasionally for a visit.

This year, I'm learning Ruby. Does that mean my team is moving to Ruby?  Probably not, but it does mean I'm learning Ruby this year because I believe in sharpening the saw. You might be too busy sawing to sharpen, but I'd encourage you - no matter what brand or type of saw you use - to remember that there are other folks out there cutting wood successfully with a different kind of saw. Maybe they know something that you don't.

The man is a genius.  Pay attention.  Learn a new language every year.  If VB is your language this year, may I suggest a good book.  For the record, I am learning Ruby too ...

Just finished watching the PDC Keynote

Weird not seeing Bill there.

Anyway, everything old is new again, again.  Microsoft Azure is Hailstorm with a little more structure around the standards of XML Web services.  Even has an SDK.  With the advent of REST and the strong strides made in reliability, security and transaction support it made sense to try it again.  However, as Steve Wallace pointed out on Twitter, you still have issues of 1)Trust and 2) Not Built Here Syndrome.  This will be a hard sell.

What am I talking about?  Azure is the new Software as a Service offering from Microsoft.  It is a 'cloud computing' (read: bunch of machines that use the Internet) environment where applications can easily be hosted with managed scalability.  You write an app, you put it on Azure, and then subscribers can get to it.  Like XBox marketplace, but for enterprise consumers.

Credit to Microsoft for doing what they said they were gonna do.  I remember at TechEd 2002 when Bill said that Software as a Service was the future and that they were going for it all the way home and back, I thought "nah".  But I was wrong.  They made a plan, and stuck to it, and here we are.

PDC attendees can register for Azure at azure.com after noon PDT.

Paul Vick at PDC

I can't make it to PDC this year due to a project commmittment, but if you are there be sure to drop by and say "hi" to Paul Vick.  He is doing a talk on the future of VB this year, and it is sure to be something I should be at ... oh well.  Anyway, I guess I'll just have to live with the video.  This is the description from microsoftpdc.com

Hear language architect Paul Vick and specification lead Lucian Wischik discuss the future direction of the Visual Basic language. Learn about the new capabilities of the next version of the language, including additional LINQ features, syntax simplifications, and a host of other improvements. Also gain insight into possible future features, including meta-programming and scripting.

If you are at PDC, be sure to check him out, cheer loudly, and report back here with the scoop!  Here is the channel 9 link:

http://channel9.msdn.com/pdc2008/TL12/

I'm sure it will be a great presentation!

Detail

In Jujutsu, details matter. The Bansenshukai Ninjutsu jujutsu curriculum is made up of a 30 part kata of maneuvers from American Jujutsu in the Crawford system. It's the basic stuff you see in an MMA fight, really. Mount reversal, guard break, side mount, key lock, arm bar, arm bar, juji gatame ... you get the idea.

What astounds me is how much the details matter. Fo instance, in step 4 of the drill Tori is in side mount and Uke breaks an arm bar and grabs for a scarf choke. Tori goes to gaidon and get a vertical arm bar. Uke's arm is laying against the back shoulder and chest of Tori, and Tori has both hands to get the lock. In BSSKN, we use the palm grip (palms together, thumbs interlocked, fingers folded over back of hand) and tonight Sensei reminded me to use the knife edge of the inside of the forearm to get the lock. Just put that bone right on the top of the tricep.

That makes all the difference.

It is simpler to just grab the elbow joint with the hands. In fact, you can just hit it with a fist and break the elbow joint if you want. But putting that edge there just puts Uke in an inordinate amount of pain right away, and who doesn't want to end a fight faster?

So next time you train, work out the details. It's not just a fight, it is a set of techniques, and blending them will take time. Focus on the techniques now so them come naturally later.

Dates and nothingness

Sounds like a metaphysical post, but it isn't. In working with an object for which I needed a last change date, I set the DateTime variable value to Nothing in the constructor.

Dim lastReset as DateTime = Nothing 

But when I check for IsNothing() later in the code the check always fails due to the fact that the Nothing value for the DateTime (a value type in .NET) is actually MinValue. To make sure that it wasn't Nothing I would avtually have to compare to MinValue:

If lastReset <> Date.Minvalue Then DoSomething() 
 
 

Well, I was sure that .NET 2.0 fixed this with Generics and I was right. This is what happens when you do to much designing and not enough coding, I guess. The new Nullable object type can be Of DateTime, so that this now works:

Dim lastReset as Nullable(Of DateTime) = Nothing 
'… 
If IsNothing(LastReset) Then DoSomething() 
 
 

From a pragmatic programming perspective, this isn't a big deal. From a solid code perspective it is a rather large deal. If I say Nothing, I want NOTHING in there. I don't want the MinValue in there. I want Nothing in there. Then I want to see if the value is still nothing later.

Generate a managed class for WMI instrumentation

So,I need a list of services from a remote machine for an application I am building for Wendys.  I wrote a boatload of System.Management code to do it, liike something like this:

   1:  Public Function ListServices() As Dictionary(Of String, String)
   2:    co = New ConnectionOptions()
   3:    co.Username = My.Settings.Username
   4:    co.Password = My.Settings.Password
   5:    co.Impersonation = ImpersonationLevel.Impersonate
   6:    scope = New ManagementScope("\\" + IPNumber + "\root\cimv2", co)
   7:    Dim result As New Dictionary(Of String, String)
   8:    Dim mc As New ManagementClass("Win32_Service")
   9:    Dim oc As ManagementObjectCollection
  10:    Dim query As New ObjectQuery
  11:    query.QueryString = "SELECT * FROM Win32_Service"
  12:    Dim mos As New ManagementObjectSearcher(scope, query)
  13:    oc = mos.Get
  14:    For Each item As ManagementObject In oc
  15:      result.Add(item.Item("DisplayName"), item.Item("State"))
  16:    Next
  17:    Return result
  18:  End Function 
  19:   

 

Then what did I learn?  Lo and behold, there is a Tool To Do This.  I hate it when that happens!  Just pass in the WMI namespace and it will generate all the class code you can eat.  (Win32_Battery made me 2800 lines of code).  Check it out here on MSDN.

Coding for Patterns

With the deployment of the gold release of ASP.NET MVC, the topic of patterns are hot and heavy again, and I am getting reader emails about “What pattern do I use for this or that”.  I answer when I can, but it needs to be understood that The Gang Of Four were writing for a pretty broad kind of software development, and a lot of their thoughts only peripherally apply to the world of business programming.

Most of us write programs that accept data from some source – usually a user – and then deliver it to some source – usually a report.  This is not what The GoF had in mind when they wrote Design Patterns.  In fact, you’ll notice that even Model View Controller – the pattern behind the ASP.NET MVC – is Martin Fowler’s; it isn’t even in Design Patterns.

Speaking of Fowler, he is a great author to check out when making decisions about a pattern for the data-driven applications usually built by VB.NET programmers.  I strongly recommend his Patterns of Enterprise Application Architecture.  The Object-relational structural patterns, especially, define a lot of the thought that needs to go into object-oriented applications persisting to a relational database.

What patterns do I use?  I believe in single abstraction of the domain.  I think that you need significant separation between the database and the domain and application layer, but to wrap ever single little thing in interfaces is a waste of time for 80% of stovepipe applications.  In general, using Fowler’s terminology, I use a lazy mapper model.  The domain objects (like person or organization) call data objects that relate directly to tables in order to gather or persist information.  If it is a really big application, I will go ahead and go to that next level of abstraction and implement that separated interface that I generally do not like.

A final note – design patterns are not ‘philosophical constructs of programming.’  They are real, important, significant and weighty contributors to software design.  If you are not designing your software, you should be and it is tough if not impossible to design without an approach and the approach is defined by the pattern.  Please don’t allow the hackers on a team to direct the flow away from a sensible approach.

Bill Sempf

Husband. Father. Pentester. Secure software composer. Brewer. Lockpicker. Ninja. Insurrectionist. Lumberjack. All words that have been used to describe me recently. I help people write more secure software.

PageList

profile for Bill Sempf on Stack Exchange, a network of free, community-driven Q&A sites

MonthList