C#, MVVM, Silverlight, WPF

Look At AOP : Getting Rid Of INPC Using Aspects

Some of you may know me, and know that I am quite into WPF, some of you may even be into WPF yourselves and come to my blog to see what posts I have here to do with WPF.

Well I just released a new article all about Aspect Orientated Programming (AOP).

I looked at 4 different AOP frameworks:

  • Castle
  • Unity
  • PostSharp
  • LinFu.AOP

Now as a WPF developer I use the INotifyPropertyChanged interface (INPC) an awful lot.

I have to say a lot of the time I am merely checking the new value does not equal current value and then calling the INotifyPropertyChanged interfaces PropertyChanged event. Not all the time mind, there are cases where something different happens in the setter….but 80% of it is bulk standard, check property and call PropertyChanged  changed event.

Which looks like this typically:

public class MainWindowViewModel : INotifyPropertyChanged
{

    private int someProperty;
    public int SomeProperty
    {
       get { return someProperty; }
       set
       {
         if (someProperty != value
         {
        someProperty=value;
        RaisePropertyChanged("SomeProperty");
         }
       }
    }


    public event PropertyChangedEventHandler PropertyChanged;

    protected void RaisePropertyChanged(string propertyName)
    {
       if (PropertyChanged != null)
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

}

 

Now I got thinking if we could use AOP to come up with something like this, where we use some special attribute to mark standard auto properties as INPC, where the AOP framework would fire the INPC PropertyChanged event whenever a property marked up with one of these attributes was set.

public class MainWindowViewModel : INotifyPropertyChanged
{

    [INPCAttribute]
    public virtual string DummyProp1 { get; set; }

    [INPCAttribute]
    public virtual string DummyProp2 { get; set; }


    public event PropertyChangedEventHandler PropertyChanged;

    protected void RaisePropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }Which got me thinking even more, perhaps if an AOP framework could do that, it may even be able to implement the entire INotifyPropertyChanged interface for me to, which would lead to classes like this:

public class DummyModel
{
    [AddINPCAttribute]
    public virtual string DummyModelProp1 { get; set; }

    [AddINPCAttribute]
    public virtual string DummyModelProp2 { get; set; }

    public virtual string DummyModelProp3 { get; set; }
}

 

The good news is that I did manage to achieve these results, and if you are interested you should read the entire article using this link:

http://www.codeproject.com/KB/miscctrl/Aspects.aspx

Please do not forget to vote…….thanks

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

Advertisements
WPF

Bad Image Quality In .NET 4.0 WPF Project

Recently at work we upgraded our .NET 3.5 SP1 app to Visual Studio 2010 .NET4.0 and all we noticed that all of our images were much more blurry after we did the upgrade.

This was quite a big deal to us since we use a lot of images. After a bit of research it turns out that that Microsoft have changed the default Scaling mode that is applied to bitmaps. It seems to be the default in .NET 3.5 was high quality and the .NET 4.0 default seems to be low quality (presumably meant as some some of performance improvement, shame you can see your images clearly though).

This affected ALL our images, I was not best pleased, luckily after a bit more digging, I found that we solve all our issues with a single Style applied to Image types, that is as follows:

<Window.Resources>
    <Style TargetType="{x:Type Image}">
        <Setter Property="RenderOptions.BitmapScalingMode" 
               Value="HighQuality" />
    </Style>
</Window.Resources>

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

And that was all we had to do…Hope this helps you as much as it did us.

Cinch

Silverlight Navigation And CinchV2 for Silverlight

As some of you may recall I recently managed to push out V2 of my MVVM framework Cinch which also included support for Silverlight. Back in the days when I wrote Cinch V1 I included a LOB application, but I did something slightly more fun and interesting for the Cinch V2 codebase and matching articles.

I did get some flack for this, but being thick skinned (and tired) I shrugged it off. Luckily one codeproject (the source of all the Cinch articles) reader, thought it would be neat to show you how to create a Silverlight navigation enabled application using Cinch V2.

I am incredibly happy this has happened, as it is something I did not do, and it is something most Silverlight Applications will need. It makes use of the standard Silverlight navigation support, i.e : Frames / UriMappings etc etc, and of course does this using MVVM and it uses Cinch V2. So massive thanks for Ben Wintringham for taking it apon himself to do this and publish it.

Here is a link to Bens article:

http://www.codeproject.com/KB/silverlight/MvvmSilverlightNavigation.aspx

How to convert a Silverlight Navigation Application to use MVVM using How to convert a Silverlight Navigation Application to use MVVM using Cinch V2.

If you use Cinch V2 for Silverlight (or WPF and have a general interest), I would urge you to read this article of Bens.

Introduction

OpenId With Forms Authentication

As some of you may know from reading some of my past articles/blogs, that I am not really a web man, but I had an idea a while back to create a tool that had to be web based, so I have been busy constructing this ASP .NET MVC web site in my spare time.

It is still ongoing, but one area that the web site needed was login/authentication, which is a pretty usual requirement on web sites. In fact ASP .NET has long supplied forms authentication for this purpose (as well as other options). Thing is, I did not really want to store username/passwords in my database, then I read about this protocol called OpenId, which is a protocol that numerous web sites adhere to already. And if you have an OpenId compliant login, my site could allow its users to use those credentials directly with the OpenId compliant web site validating them. They basically handle the login/authentication process and redirect back to the original calling site (my site).

This may sound quite nuts, but it is quite probable that you yourself are already in possession of an OpenId login, as there are many many OpenId compliant web sites out there. So it seemed to make sense to allow users to simply use their existing login credentials rather than for my site to ask them to create even more credentials.

So this is all well and good, so what is this article all about. Quite simple really, this article will demonstrate how to use OpenId with Forms Authentication (to store the Authentication cookie) within a ASP .NET MVC web site.

So I ended up creating a small demo project and explanatory atricle over at codeproject, which you can read using this link : http://www.codeproject.com/KB/miscctrl/OpenIdFormsAuth.aspx

I hope it helps you out