Archive for February, 2009

Building a Silverlight App With Prism Video Series

February 28th, 2009

This will be my next stop:

Building a Prism Application: The Complete Series

 

UPDATE: links below to the channel9msdn where you can download different quality videos:

Four part series:

Silverlight w/MVVM Pattern – Asynchronous Binding when loading a new view

February 28th, 2009

I’ve been struggling on a part of the MVVM pattern implementation : basically ‘how do I update the view on the view’s load to bind to a control (like a list).  This is common senario where you are fetching data for the display.

Basically the binding seems to occur when the page is loaded and when the asynchronous call returns I’ve been unable to get the UI element to pick up the updated list (ie. observable list).  I don’t want to have any code in my view code behind, I’m 99% there…

After much trying and experimentation I found this article with sample code.

I’m going to take this sample, and build on it.. try it out on my sample test application…. keep your fingers crossed  :)   I’ll write back on the steps needed to do it… if it works!

UPDATE:  works perfectly.  This sample shows off a few other key items: 

  1. Dependency Injection + ServiceLocator 
  2. Commanding (SLExtensions) – basic commanding – I would like to use the Caliburn commanding, simply because I like that it handles just about any event… that will be my next stop.

UPDATE 2: the problem I described, I was able to figure it out when using Caliburn.

When the asynchronous call is completed it’s important to:

Previously I was doing this:

public void CompanyLoadingComplete(object sender, Model.EventArguments.CompanyLoadedEventArgs e)
        {
            CompanyList = e.Results;
        }

Then I tried this instead:

public void CompanyLoadingComplete(object sender, Model.EventArguments.CompanyLoadedEventArgs e)
        {
            CompanyList.Clear();
            foreach(var result in e.Results)
            {
                CompanyList.Add(result);
            }
        }

Silverlight: Getting around the ‘z-index problem’

February 26th, 2009

Figured I’d pass this around if someone searches on it:

basically if you have elements on the page that use z-index, ie. I have a yahoo menu that has dropdown selections.  By default the menu items are obscured by the silverlight control.

To fix this problem, you can set parameters on the silverlight object/control:

<param name="windowless" value="true" />

You can also set other properties like ‘transparent background’:

<param name="background" value="transparent" />

(Edited for comment below)

ie.

image

Silverlight MVVM Problem…Question

February 26th, 2009

Well, I’m struggling with a few items with Silverlight, MVVM and databinding.

After reading this good article here on msdn http://msdn.microsoft.com/en-us/magazine/dd458800.aspx

I ran through the sample, looks pretty good, etc…

Few problems though…

1. the ViewModel constructor.  If I use this, I can’t use Blend any longer.  I get a memory corruption message.  I tried it on 2 different projects, same issue.  Not sure what is behind it

public GamesViewModel() :
      this(new GameCatalog())
    {
    }

    public GamesViewModel(IGameCatalog catalog)
    {
      theCatalog = catalog;
      theCatalog.GameLoadingComplete +=
        new EventHandler<GameLoadingEventArgs>(games_GameLoadingComplete);
      theCatalog.GameLoadingError +=
        new EventHandler<GameCatalogErrorEventArgs>(games_GameLoadingError);
    }

2. In my sample, similiar to this one in the article… my ‘Games’ returned are in the thousands – ie. 3000 records.   I only want to show the top 50.   I’ve been unable to get this work and resorted to manually coding up the binding in the code behind of the view – something of course I’m trying to avoid with this model.

ie. here is my viewModel:

 

public ServicesView()
        {
            InitializeComponent();
            Loaded += OnPageLoaded;

            viewModel = Resources["ServicesViewModel"] as ServicesViewModel;
            if (viewModel != null)
            {
                viewModel.LoadComplete += viewModelLoadComplete;
                viewModel.GetServices();
            }
        }

void viewModelLoadComplete(object sender, EventArgs e)
        {
            FilterCriteria.ItemsSource = viewModel.FilteredServiceList;
            ServiceListBox.ItemsSource = viewModel.FilterByCriteria(string.Empty);
            ProgressIndicator.IsActive = false;
            ServiceContent.Visibility = Visibility.Visible;
        }

The two bolded lines…I want to bind using the XAML, but it’s not taking.  Seems to me it’s related to the order in which things are occuring.

ie. here is the main list coming back from the database:

public ObservableCollection<ServiceClient> ServiceClients
        {
            get
            {
                return serviceClients;
            }
        }

That list receives 3000 service clients on load complete.

The ServiceListBox is bound in xaml, this will work, but gets 3000+

<ListBox x:Name="ServiceListBox" Height="320" Width="448" HorizontalAlignment="Left" VerticalAlignment="Bottom" 
                     Grid.Row="1"
                     Grid.ColumnSpan="2"
                     Style="{StaticResource ListBoxStyle}"
                     ItemsSource="{Binding ServiceClients, Source={StaticResource ServicesViewModel}}"
                     ItemTemplate="{StaticResource ServiceClientsTemplate}" Margin="32,0,0,36" d:UseSampleData="True"/>

So, what I tried to do was filter it out:

ViewModel:

ie.

public List<ServiceClient> FilteredServiceClient
        {
            get
            {
                return ServiceClients.OrderBy(c => c.ServiceDescription).Take(50).ToList();
            }
        }

<ListBox x:Name="ServiceListBox" Height="320" Width="448" HorizontalAlignment="Left" VerticalAlignment="Bottom" 
                     Grid.Row="1"
                     Grid.ColumnSpan="2"
                     Style="{StaticResource ListBoxStyle}"
                     ItemsSource="{Binding FilteredServiceClient, Source={StaticResource ServicesViewModel}}"
                     ItemTemplate="{StaticResource ServiceClientsTemplate}" Margin="32,0,0,36" d:UseSampleData="True"/>

However, this data is never bound to the list… (and I’ve tried making this list an observablecollection, same problem)

Silverlight – XAML – Visual Studio 2008 Crashes

February 26th, 2009

 

Update 4: http://www.lhotka.net/weblog/WorkingWithWPFOrSilverlightYouNeedThisPatch.aspx

https://connect.microsoft.com/VisualStudio/Downloads/DownloadDetails.aspx?DownloadID=16827&wa=wsignin1.0

Well… there are others that get this* , and it’s very irritating.

Basically if I leave a ‘xaml’ page open and hit debug, Visual Studio crashes to desktop.  So, before I hit debug, I must close the xaml.

I have errors in my Application log in event viewer:

.NET Runtime version 2.0.50727.3053 – Fatal Execution Engine Error

 

Very irritating to say the least…

The silverlight tools imo are very buggy, so we can hope as the product matures it gets better.  Silverlight is still sweet, just ‘young’ – in it’s infancy still – I think whatever Silverlight 1.0 was… well, Silverlight 2.0 feels like a beta to me.

I still like it though  :)

 

* I’ve search around, I see others getting this problem too, but no real solutions available.  One guy said he uninstalled .net 3.5 sp1 and it stopped, but that isn’t an option for me…

UPDATE: seems to only happen for me right now when I’m using asp.net mvc project in my solution with the Silverlight

UPDATE 2 : Actually I realized that only my asp.net mvc projects were using AnknSVN.  After uninstalling AnknSVN I haven’t had any crashes… yet….  :)    I like AnknSVN so I will look into submitting a bug – however, I’ll have to uninstall it for now

UPDATE 3: (I reinstalled AnknSVN problem solved …. so far)

Here is possible fix: http://blogs.msdn.com/webdevtools/archive/2009/03/03/hotfix-available-for-asp-net-mvc-crashes-with-azure-power-commands-resharper.aspx

Seeing the Light

February 24th, 2009

…the Silverlight…

The more I dig in and experience the Silverlight, the more I start to realize that this is a great technology just in it’s infancy with tons of potential.  The capabilities are endless, everything from as simple as replacing your javascript ajax muddling mess, to  building off-line applications inside Mesh, to plugging in RIA inside your Sharepoint installs, to pulling stock data with Silverlight’s rich networking capability.

What is really cool is it’s all done with my favorite language of choice (C#), or if you like VB.NET, IronRuby, IronPython, etc… you can build Silverlight apps – there is even an Eclipse plugin if your not a Visual Studio user!

What tops is all off was the great decision to use XAML for the views.  There are frameworks emerging, such as the newly release Prism V2 that allows you to create Silverlight/WPF apps using the same modular, dependency injection, event-driven architecture, or you can simply learn the MVVM architecture which is, in and of itself, very flexible and easy to develop with (and I would suspect Commanding will be included in Silverlight 3).

The two-way databinding is a real treat, as well as the fact that you must use asynchronous calls back to a service layer, supporting WCF, and even ADO.NET data services.  The power of ADO.NET data services let’s you take your Entity Framework, Linq to SQL, or even NHibernate Linq to hook up calls to the database.  Silveright consumes these services and creates proxy objects that can be mapped back and forth with your view model objects.

Silverlight even has a Silverlight test tool to run UI tests – which if you use the MVVM design, you’ll be rewarded with an easy to test control.

Silverlight can be a fully blown application, with navigation, page loading, etc… or can sit inside a page as a ‘Silverlight island’ – ie. think of a rich upload control showing upload progress.  You can tap into the underlying DOM from within Silverlight, or access Silverlight controls from your web pages – there is a Html bridge, etc…   It can run in your plain html page, your jsp page, your aspx page, your asp.net mvc view (and even call your controllers if you so wish).  It has crossdomain policies to keep it secure or to allow access to certain resources outside your domain.

The information out there on Silverlight is growing in leaps and bounds.  Some favorites:

Jonas Follesoe – wow – TONS of good stuff here!  Great blog to read as well, he does a fantastic job explaining how Silverlight works- good pictures, etc…

  • Is Silverlight 2 Ready for Business Applications?  This article is a nice overview of Silverlight 2:
    • Offline Storage of Data
    • Access to Local Files and Improved File Uploading
    • Multi Threading, Real-Time Networking, and Cross Domain Services
    • Rich Control Framework for Great User Experiences
    • Visualizations and Animations
    • WPF Portability
    • Linking, Search Engine Optimization, and Printing
    • Printing in Silverlight 2
  • Mesh-enabling the Dive Log Silverlight Application (if this isn’t cool I don’t know what is – lol)

Nikhil Kothari – ie. MVVM explanation, as well as a very cool Silverlight framework: Silverlight.Fx

Vectorlight has a nice set of controls: here is a ‘desktop environment’ showing off some of the controls they have

Silverlight Toolkit – this is a must – very very very cool charting, graphs, Themes, Layout tools, as well as other free controls!

Silverlight File Upload – this one is well done!

SilverlightContrib – free controls!

‘DeepInConcert’ has a TON of other stuff as well – Isolated storage, Dependency Properties, etc…  ie. Silverlight 2.0 Layout – a good primer on Grid, Stackpanel, Canvas, etc…

John Papa’s blog -  also, his great book on the data aspects of Silverlight, ‘Data-Driven Services with Silverlight 2’:  WCF, ADO.NET, DataBinding, REST.  I own this one and I loved it – it’s been a good reference as well

Shawn Wildermuth on his blog, as well as some great MSDN articles:

Prism 2 – a CAB architecture mentioned above – just recently released

Silverlight Unit Test Framework (don’t you just love to see MS create a test framework suite to go with their new products… wow, it’s a coming of age isn’t it – lol)

Rockford Lhotka (CSLA) – several good posts on Silverlight as well as a framework update to CSLA for Silverlight

Some notable open source libraries:

Json.NET for Silverlight

Castle Core, Microkernel, Windsor, DynamicProxy2 – all Silverlight ready (see trunk)

Ninject – Using Ninject with Silverlight

ok… my fingers are hurting now – but this should give a tremendous amount of information on Silverlight.

Silverlight at first is a bit daunting if your a web developer who hasn’t touched my XAML.  But it doesn’t take much to start to get into the groove.  And once in that groove – it’s been a blast learning about it.  Some parts are very much in their infancy (ie. the Silverlight Datagrid by MS… not quite ready to be ‘released’ imo), but it’s a great emerging technology.

Windor WCF Integration…bump!

February 21st, 2009

Mike Hadlow’s blog has a great writeup on using Castle Windsor with WCF.

http://mikehadlow.blogspot.com/2008/11/windsor-wcf-integration.html

Also included is information on using WCF with Windsor/NHibernate:

http://mikehadlow.blogspot.com/2009/02/wcf-windsor-integration-using.html

I followed his advice, pulled down his sample code, and was quickly able to implement in my WCF services.  Thanks Mike!

PropertyChangedEvent using Static Reflection (Expression)

February 15th, 2009

Feel free to correct me if this is a bad way of going about using static reflection… but I had a change to remove a ‘magic string’ and figured I’d use static reflection as a way around it.

I was working on a Silverlight sample of Two-way binding and re-reading some of John Papa’s Data-Driven Services with Silverlight 2 book where he is showing that in order to use two-way binding, you must implement the INotifyPropertyChanged interface.

In his book he shows a way to wrap up the duplicating code into a base class, ie:

image

To use this, the following must be done to the entity:

image

The problem I had with this is the ‘PropertyChangedHandler’ taking in a string that is to be the property name.

I’m sure there is a better way to handle this, but I decided to use some static reflection to hand it the property name:

image

Now, I can use this to replace the above “magic string”:

image

Studio Crashes

February 14th, 2009

Not quite sure what was ultimately causing my issue, but it seemed to start with Silverlight projects.

Basically VS would crash just after the solution was loaded.  Well, then this morning, any time I tried to open an .aspx file, it would just crash VS with no error, etc…  I was stumped.  I tried deleting suo file, resharper cache files, disabled Resharper add-in, reinstalled .net 3.5 sp 1 (all suggestions on MSDN site), but no matter what I would do … it would crash.

After some research I found that there can be issues with the ngen (c:\windows\microsoft.net\framework\v2….\ngen ) cache.  I ran ngen update to see I had a ton of errors (scary).   The suggestion was to remove the ones in question… I went to read about what ngen does, and decided… well, with the amount I had I basically decided I’d just clear the ngen cache completely (with fingers crossed).

I cleared the cache, and now everything is working great.

Data-Driven Services with Silverlight 2

February 7th, 2009

By John Papa.

My new book arrived today and I’ve read through the first 2 chapters.  I’m really impressed.  I picked this book up mostly because geared more toward building ‘data-rich business applications’ with Silverlight.

So far, so good – I will post more later, let’s just say it has inspired me to even write this – as it’s a very good read!

One thing I appreciate about his writing style (so far) is that he doesn’t assume you know the technology, and yet he seems to explain these complex features in a simple and concise way.

Part II…

I didn’t really intend on reading almost all of this in one sitting… but it’s an excellent primer.  Covers WCF, WCFRest, Web Services, ADO.NET Data Services – using a plain POCO, Linq to Sql, Entity Framework.   This is the type of book I was interested in – it shows enough of XAML to get you acquainted – with data templates, binding modes, notification – but focuses mostly on the communication with entities from Silverlight to services.