Learning WPF M-V-VM

November 8, 2008

For the last few months I’ve been studying M-V-VM and other WPF UI design patterns.

I have selected M-V-VM as my UI design pattern of choice for the soon to be released Ocean framework and code generation system.

I have read much of what Microsoft Partner Architect John Gossman, members of the WPF Disciples have published and specifically what my great friend and Microsoft & Code Project MVP Josh Smith has written on the UI design patterns.

Without any doubt, the best and clearest example of WPF M-V-VM I’ve seen is in Josh’s latest achievement, Crack.NETIf you want to learn M-V-VM from an expert, download Crack.NET source and study Josh’s masterpiece.

Applied

I’m sitting here at the Silicon Valley Code Camp having just taught a M-V-VM for WPF LOB session.   The applications I presented were all 100% testable.  The main application that has all the unit tests, can be run without ever displaying any UI.  It is amazing the power that M-V-VM delivers with such simplicity.  

I need to make a few videos and write up a blog post and I’ll share the code with you after next weekend.

I’m finishing up Ocean’s UI and it uses M-V-VM.

Below is a slide from the presentation.  It covers the layers found in an WPF LOB application.

WPFLOBMVVM

Raleigh Code Camp

Next weekend, 15 Nov 2008, I’ll be in Raleigh, NC for the annual fall RDU Code Camp.  I’ll be launching Ocean at this code camp and will be doing several other sessions as well as being available in the Open Spaces.  See you there!!

Close

Have a great day,

Just a grain of sand on the worlds beaches.


WPF Sample Series – Solution for the Obsolete BitmapEffect Property and Rendering an OuterGlowBitmapEffect

November 4, 2008

Since the release of .NET 3.5 SP1, I have been getting a new Visual Studio warning in my code where I have assigned a BitmapEffect to a UIElement.  Specifically I was using the OuterGlowBitmapEffect.

Warning 1 ‘Public Property BitmapEffect() As System.Windows.Media.Effects.BitmapEffect’ is obsolete: ‘Avoid using BitmapEffects as they have very poor performance characteristics.  They will be deprecated in a future version.  Consider using the UIElement.Effect property and ShaderEffects where appropriate instead.’

Problem

I needed an outer glow effect in my application so I read up on the new Effects property and the new Effect and ShaderEffects classes.  However, I could not find a replacement for the OuterGlowBitmapEffect that would work with the new UIElement.Effects property.

UIElement.BitmapEffect property is obsolete, making those bitmap effects we have been using obsolete also.

On the surface it seems the new UIElement.Effect property does not have an OuterGlowEffect class we can use to replace the OuterGlowBitmapEffect we have been using.

Solution

Today I was reading a WPF thread and found the solution that I want to share with you.

Outerglow

The simple example has four buttons, two with effects declared in  XAML the other two effects assigned in code.  When using the obsolete BitmapEffect property in XAML, no warnings are given by Visual Studio.  The warnings are only provided when you assign the BitmapEffect property a value in code.

Most of you are familar with the OutGlowBitmapEffect.  There is no new class deriving from Effect or ShaderEffect that has a name with OuterGlow in it.

The solution is to use the new DropShadowEffect and set its ShadowDepth to zero.  Presto, we not have an outer glow effect we need.  You can also play around with the BlurRadius property to get the glow size you want.

Here is the best part of all, your outer glow will now be hardware rendered and not software rendered, giving you a nice performance boost. 

You can read about the hardware performance boost in this post GPU-Accelerated Custom Effects for WPF.

<Window x:Class="Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
  
  <StackPanel>
    
    <Button Margin="11" Content="OuterGlowBitmpEffect - XAML">
      <Button.BitmapEffect>
        <OuterGlowBitmapEffect GlowColor="Blue" GlowSize="5" />
      </Button.BitmapEffect>
    </Button>

    <Button Margin="11" Content="DropShadowEffect - XAML">
      <Button.Effect>
        <DropShadowEffect ShadowDepth="0" Color="Blue" BlurRadius="10" />
      </Button.Effect>
    </Button>

    <Button Margin="11" Content="OuterGlowBitmpEffect - Code" x:Name="btnOuterGlow" />

    <Button Margin="11" Content="DropShadowEffect - Code" x:Name="btnDropShadow" />

  </StackPanel>
  
</Window>

The below line of code, Me.btnOuterGlow.BitmapEffect = objOutGlow gets flagged by Visual Studio with the warning message in the below comment.

Below the comment is the code for assigning the new DropShadowEffect to the button in code.  By setting the ShadowDepth to zero and increasing the BlurRadius to ten, you get the same outer glow effect you had with the now obsolete OuterGlowBitmapEffect .

Imports System.Windows.Media.Effects

Class Window1

    Private Sub Window1_Loaded(ByVal sender As Object, _
            ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded

        Dim objOuterGlow As New OuterGlowBitmapEffect
        objOuterGlow.GlowSize = 5
        objOuterGlow.GlowColor = Colors.Red
        Me.btnOuterGlow.BitmapEffect = objOuterGlow

        'Public Property BitmapEffect() As System.Windows.Media.Effects.BitmapEffect
        ' is obsolete.  Avoid using BitmapEffects as they have very poor performance 
        'characteristics.  They will be deprecated in a future version.  

        'Consider using the UIElement.Effect property and ShaderEffects where appropriate 
        'instead.

        Dim objDropShadow As New DropShadowEffect
        objDropShadow.ShadowDepth = 0
        objDropShadow.BlurRadius = 10
        objDropShadow.Color = Colors.Red
        Me.btnDropShadow.Effect = objDropShadow

    End Sub
End Class

Download

After downloading the below package, you must rename it from .doc to .zip.  This is a requirement of WordPress.com

Download Source (17 KB)

Close

Have a great day,

Just a grain of sand on the worlds beaches.


Support our Marines Overseas

November 2, 2008

marinesega

Many of you know I served in the Marine Corps after graduating from high school.  The training and experience of the Corps molded me for life.  I was blessed to have seen so much while in.  From carrying a rifle though the jungles in the Pacific to crewing a KC-130 on flights around the globe to molding Marine Recruits on the grinder at MCRD Parris Island, South Carolina.

The Marine Corps is an environment, a lifestyle where words like honor, commitment, loyalty, dedication, courage and faithfulness are core values that are lived out each day.  I love the Corps and miss it everyday.

Today I received an email from a retired Marine friend of mine, Gene Hetzel.  He passed along some information on supporting our Marines overseas this Christmas.

I emailed the unit Chaplain, Lt. Cullen and he sent me the following letter I want to share with you.

How to Support our Marines

Greetings from Iraq! We are the men of 1st Reconnaissance Battalion, United States Marines Corps. As we draw near to the holiday season, we are seeking support once again for our Marines and Sailors. As you can imagine, the holidays are difficult times to be away from family members. Our supporters play a big role in keeping the men’s morale high and impacting the effectiveness of the greater mission. Movies, Greeting Cards for the Marines and Sailors to send home, though seemingly insignificant, have a great affect on morale.

Below, please find a list of recommended items, but by no means consider this list exhaustive. We also included our Battalion’s web site. Visit the site regularly to stay abreast of things happening with 1st Recon.

Thank you for your support, thoughts, and prayers. We are very mindful of you and all that you do for the Marines and Sailors of 1st Reconnaissance Battalion.

Web Site: http://www.i-mef.usmc.mil/div/1reconbn/default.asp

Care Package Mailing Address:

LT DAVID J. CULLEN

1st RECON BN H&S CO

UNIT 40535

FPO AP 96426-0535

Suggested Care Items

  • Packets of sugar
  • Packets of creamer
  • Chocolate powder
  • Oatmeal
  • Beef jerky
  • Sunflower seeds
  • Protein bars
  • Hand warmers
  • Trial size deodorant
  • Socks
  • Phone cards 
  • DVD movies 
  • Toothbrushes
  • Toothpaste
  • Chap sticks
  • Greeting cards 
  • Travel size lotion
  • Baby wipes

Close

Thank you for your support of our Marines overseas.  As I was writing this the following verse came to mind:

Cast thy bread upon the waters: for thou shalt find it after many days. — Ecclesiastes 11: 1 (KJV)

Have a great day,

Just a grain of sand on the worlds beaches.


WPF CheckListBox – Two Versions XAML Only & Custom Control

November 1, 2008

Article and Code

Read the WPF CheckListBox article here

Background

In October of 2007 I wrote an article on Code Project entitled, A WPF Problem Solved Two Very Different Ways – Using XAML Only – Using A Custom Control.

I was at work yesterday and was replying to a forum post requesting information on a WPF CheckListBox.  I actually had trouble finding my article on my blog.  After finding it, I realize the title I selected was not a good “long term” blog post title.

At the time I was showing off the power of WPF and created two solutions to a problem Josh Smith was writing about in his article, The WPF Thought Process.

However, a year goes by, the title no longer makes sense and does not aid search engines in returning my article or blog post that is REALLY about WPF CheckListBoxes!

Moral of the Story

Name blog posts with titles that a year later will enable Internet users to find your awesome content.

So, this is a repeat blog post with the correct title to help others find the poorly named article.

Have a great day,

Just a grain of sand on the worlds beaches.

 


Follow

Get every new post delivered to your Inbox.

Join 167 other followers