Welcome to MSDN Blogs Sign in | Join | Help

Scott McCraw's Blog

These postings are provided "AS IS" with no warranties, and confer no rights.

A Silverlight Calculator

OK so I finally found some time to start playing around with SL2 B1 in earnest this last weekend.  The results? A Silverlight Calculator.  I don’t know why but a calculator has always been my HelloWorld, every new programming language or UI stack I learn always starts with a calculator.  Anyway you can try out the live version of it here and download the full source code here.  You can find some good instructions on how to get up and running on Silverlight here if needed.

A Basic Calculator

Default Calculator Skin

 

A quick overview of the things I explored during this process.

  • The wonderful world of styling controls in Silverlight.  Have a gander at BasicBlack.cs and generic.xaml to see how I set up the styling for my basic calculator skin.  CalculatorAddIn/GlassSkin.xaml shows an example of a new skin over the basic template.
    Glass Style
  • Skinning is also supported, you can rip the entire UI off the calculator and replace it with your own as shown in CalculatorAddIn/SimpleConsole.xaml & .cs.  To support this skinning approach I had to achieve a simple View/Model separation in the code through the use of interfaces (see ICalculatorEngine.cs, ICalculatorCommand.cs and INumericStack.cs).
    Simple Console Skin
  • Support for custom AddIns.  I wanted to let folks write their own calculator functions in addition to creating their own skins.  Checkout App.xaml.cs and CalculatorFrame.xaml.cs for all the code behind loading assemblies on the fly using AssemblyPart.Load().  You can try this on your machine by downloading the ZIP file for the code and selecting the CalculatorAddIn/ClientBin/CalculatorAddIn.dll when the Open Add In dialog opens.
  • IsolatedStorage – interesting concept for remembering what AddIns have been loaded when you come back to the web page.  IsolatedStorageFile.TryIncreaseQuotaTo() is an interesting API to say the least!

Some known issues/ToDos:

·         Resetting the AddIns is a fairly jarring experience right now (since Assemblies cannot be unloaded, a full page refresh is done to cycle the AppDomain).  At a minimum I need to have an Are you sure? MessageBox (but SL has no such thing yet).  Ideally you should be able to turn off the add-ins one at a time and the calculator would just quit using the extension even though they still exist in the domain.

·         No versioning support for the AddIns.  If you want to get a new version of your AddIn loaded you currently have to unload all the AddIns, reset the AppDomain and then reload your addins.

·         Application Errors are either ignored or unhandled right now.  I have to figure out a good model for dealing with these things.

·         Currently only text is supported in the Command Buttons, I need to explore the wonderful world of loading/parsing XAML at runtime and allow commands to have more sophisticated button views.  Styling is currently putting a serious crimp in these efforts, but we’ll see where it goes.

Anything else?  Comments?  Ideas on additional areas/techniques to explore here?

Published Wednesday, March 26, 2008 5:30 PM by smccraw

Comments

# re: A Silverlight Calculator @ Wednesday, March 26, 2008 10:23 PM

Add a Saturn emulator so I can load an HP49 ROM in the thing :)

kfarmer@microsoft.com

# Silverlight Extensibility: The Calculator @ Thursday, March 27, 2008 3:16 AM

Scott is a devlead I work with and he has been experimenting with how to have an extensibility pattern

Brad Abrams

# re: A Silverlight Calculator @ Thursday, March 27, 2008 11:14 AM

Nice-looking calculator. Can't wait to check out the code this weekend. Keep up the great work!

alvinashcraft

# A Silverlight Calculator with pluggable functions and skins @ Thursday, March 27, 2008 11:34 AM

You've been kicked (a good thing) - Trackback from DotNetKicks.com

DotNetKicks.com

# You beat me to it... @ Thursday, March 27, 2008 11:35 AM

Awesome example, and you beat me to it. I actually just started to work up an example of creating a plugin architecture for Silverlight. Thanks!

crpietschmann

# re: A Silverlight Calculator @ Thursday, March 27, 2008 1:19 PM

Scott,

I was just digging through the code to learn it's secrets and noticed a block that seems to be disabled. The bit in LoadCommands about loading all ICalculatorCommand implementors uses a query that can never return a value (as near as i can tell):

where type.IsAbstract == false && type.IsAbstract == true && [...]

is that on purpose to disable that bit?

And while i'm at it.. the prior query looks for types that are sealed (and finds them), but i can't see any "sealed" in the declaration of those types. What's going on there?

thanks,

arne

sdether

# re: A Silverlight Calculator @ Thursday, March 27, 2008 4:57 PM

Yeah the

where type.IsAbstract == false && type.IsAbstract == true

has been fixed.  It was supposed to say type.IsClass==true for the second clause.

As for sealed, that's how static classes show up in reflection (static classes cannot be inherited from or added to, thus they are sealed).

smccraw

Anonymous comments are disabled
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker