Click here to Skip to main content
15,881,709 members
Articles / Desktop Programming / WPF

Introducing the Model Thread View Thread Pattern

Rate me:
Please Sign up or sign in to vote.
4.93/5 (69 votes)
1 May 2010BSD14 min read 163K   862   172  
Reduce threading code, and increase UI responsiveness with a new pattern extending MVVM.
<UserControl xmlns:my="clr-namespace:System.Windows.Controls.DataVisualization;assembly=System.Windows.Controls.DataVisualization.Toolkit"  xmlns:my2="clr-namespace:System.Windows.Controls.Theming;assembly=System.Windows.Controls.Theming.ExpressionDark"  
             x:Class="DanielVaughan.MtvtExample.MainPage"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:cal="clr-namespace:Microsoft.Practices.Composite.Presentation.Commands;assembly=Microsoft.Practices.Composite.Presentation" 
             mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">
    <UserControl.Resources>
        <Style TargetType="StackPanel">
            <Setter Property="Margin" Value="5" />
        </Style>
        <Style TargetType="TextBlock">
            <Setter Property="Margin" Value="0, 0, 0, 10" />
        </Style>
    </UserControl.Resources>

    <Grid x:Name="LayoutRoot" Background="White">
        <my2:ExpressionDarkTheme>
            <StackPanel Margin="50" Width="426">
                <Image Source="Images/WpfDisciplesBanner.png" Margin="0, 0, 0, 10" />
                <TextBlock FontSize="20">Example: Model Thread View Thread</TextBlock>
                <TextBlock TextWrapping="Wrap">This example demonstrates how a command can be used to raise CanExecute and Execute handlers in a view model using a dedicated model thread.
                    It also shows how a custom ObserableDictionary can raise collection changed events on the subscriber thread.
                    This means that when the collection changes via the model thread, the collection changed event will be raised on the UI thread, and
                    this prevents cross thread access exceptions. Likewise, property changes, if not subscribed to on the UI thread are raised on the model thread.</TextBlock>
                <StackPanel Orientation="Horizontal">
                    <Button Content="Fire view model command" 
                            cal:Click.Command="{Binding TestCommand}" 
                            Width="169" Height="32"  />
                    <TextBlock Text="{Binding Message}" TextWrapping="Wrap" Margin="5, 0, 0, 0" />
                </StackPanel>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="Last string added: "/>
                    <TextBlock Text="{Binding TestString}"/>
                    <TextBlock Text=" | UI Thread ID: "/>
                    <TextBlock x:Name="TextBlock_UIThreadId" />
                    <TextBlock Text=" | View Model Thread ID: "/>
                    <TextBlock Text="{Binding VMThreadId}" />
                </StackPanel>
                <ListBox ItemsSource="{Binding ExampleCollection}" Height="200"/>
            </StackPanel>
        </my2:ExpressionDarkTheme>
    </Grid>
</UserControl>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The BSD License


Written By
Engineer
Switzerland Switzerland
Daniel is a former senior engineer in Technology and Research at the Office of the CTO at Microsoft, working on next generation systems.

Previously Daniel was a nine-time Microsoft MVP and co-founder of Outcoder, a Swiss software and consulting company.

Daniel is the author of Windows Phone 8 Unleashed and Windows Phone 7.5 Unleashed, both published by SAMS.

Daniel is the developer behind several acclaimed mobile apps including Surfy Browser for Android and Windows Phone. Daniel is the creator of a number of popular open-source projects, most notably Codon.

Would you like Daniel to bring value to your organisation? Please contact

Blog | Twitter


Xamarin Experts
Windows 10 Experts

Comments and Discussions