CSLA Silverlight Dynamic Button / Template Page - Can't Work It Out!

CSLA Silverlight Dynamic Button / Template Page - Can't Work It Out!

Old forum URL: forums.lhotka.net/forums/t/6844.aspx


FraggleRocks posted on Thursday, April 23, 2009

I have a 5 editable root lists and a "generic" silverlight page which contains a toolbar, a data-grid and an Add Row Button.  As I construct the generic page I pass a parameter to the constructor telling it which business object it is to provide an editable client for.  In the constructor I create the relevant datagrid columns and also include a delete button column.  This all works fine.  the correct data is displayed and changes are persisted.

The problem I have is that the editable page contains an "Add Row" button, When I have 5 different Silverlight pages and define the CSLA properties for the button in the XAML for each page, I get correct behaviour.  But when I try and dynamically set the CSLA propeties in the page constructor the button does not work.  If I step through with the debugger the properties appear to be set, but have no affect.

What I am trying to achieve is to code only the differences or specialisms of the generic page.  Does anyone know how to solve my "setting button CSLA property setting at runtime" problem, or have comments on a better strategy?   Many Thanks.

RockfordLhotka replied on Thursday, April 23, 2009

This is a little tricky because of the async world in SL.

In .NET there's the IBindingList interface. That doesn't normally exist in SL, but we created a small subset of it - specifically to address this issue.

So if you cast any CSLA list object to Csla.Core.IBindingList you can get at an AddNew() method.

If the list overrides AddNewCore(), then the UI can call IBindingList.AddNew() and a new item gets added to the list. Conceptually this is no different from .NET.

Except the AddNewCore() method in SL is actually there so you can start an async operation to create the new object. When the creation process is complete (whether sync or async) you must call OnCoreAdded() to tell CSLA that the new child has been created.

The whole point of this is to allow the UI to use IBindingList.AddNew() on any list type - as long as that list supports adding new items by overriding AddNewCore() - just like in .NET.

The only wrinkle, as I say, is that we had to support the possibility of async creation because this is SL after all...

If you can create the child in a sync manner, it is pretty easy:

protected override void AddNewCore()
{
  var newItem = new ChildObject();
  OnCoreAdded(this, new DataPortalResult<ChildObject>(newItem, null, null))
}

That's as close as you can get to the .NET model.

But hopefully you can see how this could start an async call, such as to the data portal, to create the new object, in which case you'd just provide the address of OnCoreAdded as the callback handler for the data portal's CreateCompleted event.

Copyright (c) Marimer LLC