Csla/Caliburn.Micro sample - the short story

Csla/Caliburn.Micro sample - the short story

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


tiago posted on Tuesday, January 25, 2011

For those out there that want to do MVVM using Csla and Caliburn.Micro, here is a WPF sample. I guess it should be very easy to port it to Silverlight.

The stereotypes used are ReadOnly List and ReadOnly Object.

Put in your project the usual suspects:

Notes - No need for System.Windows.Interactivity.dll on this sample.

 

DefaultBootstrapper.cs

using Caliburn.Micro;
using CaliburnMicroWpf.ViewModels;

namespace CaliburnMicroWpf
{
    public class DefaultBootstrapper : Bootstrapper<DocListViewModel> {}
}

App.xaml

<Application x:Class="CaliburnMicroWpf.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
             xmlns:local="clr-namespace:CaliburnMicroWpf">

    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary>
                    <local:DefaultBootstrapper x:Key="bootstrapper" />
                </ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

 

Views\DocListView.xaml

<Window x:Class="CaliburnMicroWpf.Views.DocListView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid>
        <DataGrid 
            AutoGenerateColumns="False" 
            EnableRowVirtualization="True" 
            Height="310" 
            HorizontalAlignment="Left" 
            Margin="12,14,0,0" 
            Name="DocList" 
            RowDetailsVisibilityMode="VisibleWhenSelected" 
            VerticalAlignment="Top" 
            Width="806">
            <DataGrid.Columns>
                <DataGridTextColumn x:Name="DocID" Binding="{Binding Path=Model.DocID}" Header="Doc ID" IsReadOnly="True" Width="SizeToHeader" />
                <DataGridTextColumn x:Name="DocRef" Binding="{Binding Path=Model.DocRef}" Header="Doc Ref" Width="SizeToHeader" />
                <DataGridTextColumn x:Name="DocDate" Binding="{Binding Path=Model.DocDate}" Header="Doc Date" Width="SizeToHeader" />
                <DataGridTextColumn x:Name="DocClassName" Binding="{Binding Path=Model.DocClassName}" Header="Doc Class Name" Width="SizeToHeader" />
                <DataGridTextColumn x:Name="DocTypeName" Binding="{Binding Path=Model.DocTypeName}" Header="Doc Type Name" Width="SizeToHeader" />
                <DataGridTextColumn x:Name="DocStatusName" Binding="{Binding Path=Model.DocStatusName}" Header="Doc Status Name" Width="SizeToHeader" />
                <DataGridTextColumn x:Name="RecipientName" Binding="{Binding Path=Model.RecipientName}" Header="Recipient Name" Width="SizeToHeader" />
                <DataGridTextColumn x:Name="SenderName" Binding="{Binding Path=Model.SenderName}" Header="Sender Name" Width="SizeToHeader" />
                <DataGridTextColumn x:Name="Subject" Binding="{Binding Path=.Model.Subject}" Header="Subject" Width="SizeToHeader" />
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window> 

Notes - Replace Name="DocList"  with your ObsevableCollection<T> name and use your own properties on the DataGridTextColumn. Note also Caliburn-Micro has no bind conventions for DataGridtextColumn so you must write all the Binding statements.

 

ViewModels\DocListViewModel.cs (read only list)

using System.Collections.ObjectModel;
using System.Linq;
using Csla.Xaml;
using DocStoreBO;

namespace CaliburnMicroWpf.ViewModels
{
    public class DocListViewModel : ViewModel<DocList>
    {
        public DocListViewModel()
        {
            ManageObjectLifetime = true;
            DoRefresh("GetDocList");
        }

        public ObservableCollection<DocInfoViewModelDocList
        {
            get
            {
                var result = new ObservableCollection<DocInfoViewModel>();
                if (Model != null)
                    foreach (DocInfo item in Model.OrderBy(c => c.DocID))
                        result.Add(new DocInfoViewModel(item));
                return result;
            }
        }
    }
}

 Notes - Replace DoRefresh("GetDocList"); with  your factory method. Note the ObservableCollection is a collection of ViewModel objects and not a collection of Csla objects. Those go into the Model property.

 

ViewModels\DocInfoViewModel.cs (read only object)

using Csla.Xaml;
using DocStoreBO;

namespace CaliburnMicroWpf.ViewModels
{
    public class DocInfoViewModel : ViewModel<DocInfo>
    {
        public DocInfoViewModel(DocInfo docInfo)
        {
            ManageObjectLifetime = false;
            Model = docInfo;
        }
    }
}

 A final word to Jonny Bekkum that told me this was possible; thank you Jonny.

acheema replied on Monday, January 20, 2014

@Tiago Freitas Leal , please can you give an example how to call BeginRefresh("GetDocList"), and after async, returns how the result will bind to GUI.

Thanks.

Copyright (c) Marimer LLC