in

Dé specialist in .NET trainingen en consultancy

Thomas Huijer

oktober 2009 - Posts

  • Compile-time checked region names in Prism

    In a R&D project of mine, I’m using Prism to create a composite WPF application. I can recommend anyone writing WPF or Silverlight applications to take a look at it if you’re unfamiliair with Prism.

    One thing I dislike about it though, is that is uses strings as region names. Meaning that there’s no compile-time checking if your region names in the Shell and the names you’re using when registering the views are actually aligned.

    Here’s a small trick to get compile-time checking for your region names in Prism:

    1. Define an Enum containing the names of all known regions:

          public enum RegionName
          {
              ToolBar,
              MessageList,
              Details
          }

    2. Define a MarkupExtension for each value of the enum. This sounds like a lot of work, but with a base class like the one below, it’s no more than a few lines.

          public class BaseRegionExtension : MarkupExtension
          {
              private RegionName _regionName;
       
              public BaseRegionExtension(RegionName regionName)
              {
                  _regionName = regionName;
              }
       
              public override object ProvideValue(IServiceProvider serviceProvider)
              {
                  return _regionName.ToString();
              }
       
              public RegionName Name
              {
                  get
                  {
                      return _regionName;
                  }
                  set
                  {
                      _regionName = value;
                  }
              }


      Defining a MarkupExtension now becomes really simple:

         public class ToolBarRegionExtension : BaseRegionExtension
          {
              public ToolBarRegionExtension()
                  : base(RegionName.ToolBar)
              {
              }
          }

    3. When registering your view, just call the ToString() of the enum value you want to use:

        _regionManager.RegisterViewWithRegion(RegionName.ToolBar.ToString(), typeof(MainToolbarView));

    4. When defining regions in your Shell Xaml file, use the right MarkupExtension to reference a value in your enum:

      <ToolBarPanel>
           <ItemsControl cal:RegionManager.RegionName="{ui:ToolBarRegion}">           
           </ItemsControl>
      </ToolBarPanel>

    Have fun!

  • No regions when generating code

    Just wished I had found this option about 5 years earlier….

    image

  • Programming with a guitar…

    My long-time friend Mark Miller of DevExpress has created another challenge for himself. A couple of years ago at the PDC he challenged developers to write some code together. The developer would have a full keyboard and mouse and a standard Visual Studio. Mark would have a keyboard too, but he had to program with chopsticks. His Visual Studio would have CodeRush installed though. And yeah, Mark was faster…

    Well, this year Mark has an even greater challenge. He’s not going to use a keyboard at all. He’s going to use a guitar to write code. Yeah…a guitar…to write code…

    Watch Mark in action on his guitar: http://tv.devexpress.com/GuitarCodeInterview.movie

    Posted okt 21 2009, 03:51 by Thomas with no comments
    Filed under: ,