0

Spider Solitaire and Augmented Reality

A while ago, I made an implementation of Solitaire and Spider Solitaire using WPF and my Apex MVVM library. I wrote about it on the CodeProject, in an article called Solitaire and Spider Solitaire for WPF (imaginative title indeed).

Anyway, just recently I got a very interesting message from rupam rupam, who has made an augmented reality version of the project! In his application, you use your webcam to play the game physically by picking up cards with gestures. Other gestures, like thumbs up and thumbs down are bound to commands in the game – here’s a screenshot:

SpiderAugmented

The project is called GesCard and as far as I know there isn’t a page showing the code – but there are more links on the YouTube video for the page. Check out the YouTube video with the link here https://www.youtube.com/watch?v=wCOjuPdBooI. Thanks to rupam for getting in touch and sharing this very cool code!

 

0

Funky WPF – Enumerations and the Combo Box

Binding a combo box to an enumeration in WPF is more work than it should be, creating an object data provider etc etc:

<Window.Resources>
    <ObjectDataProvider MethodName="GetValues"
        ObjectType="{x:Type sys:Enum}"
        x:Key="CharacterEnumValues">
        <ObjectDataProvider.MethodParameters>
            <x:Type TypeName="Character" />
        </ObjectDataProvider.MethodParameters>
    </ObjectDataProvider>
</Window.Resources>

Followed by

<ComboBox SelectedItem="{Binding Character}"
ItemsSource="{Binding
Source={StaticResource CharacterValues}} "/>

What a pain! I have just added ‘EnumerationComboBox’ to my Apex library – so now you can do this:

<!-- The combo box, bound to an enumeration. -->
<apexControls:EnumerationComboBox 
SelectedEnumeration="{Binding Character}" />

No need for an ObjectDataProvider, an items source or anything – and if you decorate enum’s with the ‘[Description]’ attribute, it’ll use the description in the combo.

There’s an article/download here for anyone who’s interested:

http://www.codeproject.com/KB/WPF/enumcombobox.aspx

0

MVVM: Asynchronous Commands

The latest cut of the Apex Code (http://apex.codeplex.com/SourceControl/changeset/changes/6701) contains a very cool new feature – Asynchronous Command Objects.

An Asynchronous Command is a ViewModelCommand – the standard object used in Apex for commanding. However, what is different about this function is that it runs Asynchronously.

One of the problems with running a view model command asynchronously is that generally the view model properties cannot be accessed – as they’re created on a different dispatcher. This problem is resolved by using the ‘ReportProgress’ function. Here’s an example:

public class SomeViewModel : ViewModel
{
  public SomeViewModel()
  {
     // Create the command.
     asyncCommand = new AsynchronousCommand(DoAsyncCommand, true);
  }

  private void DoAsyncCommand()
  {
     for(int i = 0; i < 100; i++)
     {
        // Perform some long operation.
        string message = DoSomeLongOperation();

        // Add the message to the View Model - safely!
        asyncCommand.ReportProgress(
          () =>
          {
             messages.Add(message);
          }
        );
     }
  }
  
  private ObservableCollection<string> messages =
    new ObservableCollection<string>();

  public ObservableCollection<string> Messages
  {
     get { return messages; }
  }

  private AsynchronousCommand asyncCommand;

  public AsynchronousCommand AsyncCommand
  {
     get { return asyncCommand; }
  }
}

In this basic mock-up we have a command called ‘AsyncCommand’ (which we could bind a button to for example) which invokes DoAsyncCommand. However, it invokes it Asynchronously. We can also update the ViewModel properties by using ReportProgress – meaning AsyncCommands can seamlessly provide live feedback while they’re working – and we’re keeping well locked in with the MVVM commanding model!

Expect a full article soon on the CodeProject, until then the source is at:

http://apex.codeplex.com/SourceControl/changeset/changes/6701