XAML Playground
about XAML and other Amenities

The “evil” in using “async void”

2013-02-19T01:57:03+01:00 by codeblock

Following a non-NDA session at the MVP Global Summit 2013, the speaker pointed me to an interesting argument that is the usage of “async” with methods returning void. This practice is used many times, more than you expect, and is a potential source of interesting bad issues that can cause unexpected crashes in your apps.

The problem with “async void” versus the opposite “async Task” is that a method declared this way is something like a “fire and forget”, where the fired part is the body of the method and the forget part may be the problem. Let say you have a situation like this:

   1: public void Run()
   2: {
   3:     try
   4:     {
   5:         DoSomething();
   6:     }
   7:     catch(Exception ex)
   8:     {
   9:         DisplayError(ex);
  10:     }
  11: }
  12:  
  13: public async void DoSomething()
  14: {
  15:     throw new Exception("Something gone wrong...");
  16: }

The “DoSomething” method here throws and exception, probably due to an error in its flow. The problem is that, when this method is called, the developer cannot use the “await” because the method is declared as “void”. So, when the exception is thrown, the “Run” method has already exited. The direct result is that the Exception is pushed to the UI Thread and in a Windows Store app this means that the app crashes, without any advice.

This situation is much more common they you expect. consider the Dispatcher.RunAsync method:

   1: Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
   2:     async () =>
   3:     {
   4:         // do something here...
   5:     });

The declaration of the RunAsync method requires a delegate declared as “void”, so when you ask for an async lambda, this is called as in the previous example. An exception in the body of the lambda expression causes exactly and application crash. The soluton is to wrap the RunAsync method is a mathod that returns a Task, so it can be awaited. So please, always avoid using “async void” in your code and you will not drive crazy trying to understand what is going wrong in your app.

Tip: a common error with Dependency Property metadata

2013-01-22T23:54:03+01:00 by Andrea Boschin

Recently someone pointed me to a malfunction on one of my examples. He sent me a repro project and, after I've run the example, I found the problem as he described it to me. But it took me a long time to figure out the reason. The problem was really simple: an instance of a templated control in a page seems to share a property with another instance of the same control in another page... This sounds impossible at first sight but if the property is a Dependency Property the issue is real. Here is the original code:

   1: public static readonly DependencyProperty ParallacticLayersProperty =
   2:     DependencyProperty.Register(
   3:         "ParallacticLayers", 
   4:         typeof(List<FrameworkElement>), 
   5:         typeof(ParallacticGridView), new PropertyMetadata(new List<FrameworkElement>()));

The code above may appear correct but it has an important issue. When a dependency property is used with a collection type, it is important to not initiate the metadata inline with the declaration, because the declaration of a static property only create a single instance that is shared by all the controls that use thid property. The correct example is the following:

   1: public static readonly DependencyProperty ParallacticLayersProperty =
   2:     DependencyProperty.Register(
   3:         "ParallacticLayers", 
   4:         typeof(List<FrameworkElement>), 
   5:         typeof(ParallacticGridView), new PropertyMetadata(null));

The collection datatypes only, make this problem evident, because once the property has been initialized it does not change the value. The observed behavior shows that the collection always adds the items without removing them. This apply to every XAML dialect...

Windows 8 Apps: Change your project to run PlayReady successfully

2012-09-22T23:29:25+01:00 by Andrea Boschin

Today I wasted a lot of time to understand why, the code I wrote to implement PlayReady in a Windows 8 App didn’t work. I have ran the code lot of times, making changes to handle obscure hypothesis, just because my application was failing to load a DRM protected stream. When I finally found the error code issued by the MediaFailed event I went on the right way:

MF_MEDIA_ENGINE_ERR_SRC_NOT_SUPPORTED : HRESULT – 0x800700C1

I convinced myself that the problem wasn’t in the code but probably in the context I was compiling, this because the error 0x800700C1 means “is not a valid Win32 application”.

Comparing the project configuration with the one in the samples, I found the answer in a simple flag in the project configuration:

image

The “prefer 32-bit” flag is a new entry in the projects that target Microsoft .NET Framework 4.5. Its meaning is explained well in this post but in a few words in my case it was forcing the compiler to output to x86 (32-bit) instead of x64. Unfortunately PlayReady only supports 64bit runtime so, when the application ran it failed with the code I reported above.

So, when you’ll have to write applications that take advantage of the PlayReady SDK, please always remember to clear this flag because it is the sole way to have it working.

Draw your Paths with Logo Turtle Graphics

2012-02-20T22:58:55+01:00 by codeblock

Working on a presentation I'm preparing about Silverlight 5.0, I wrote a funny example to demonstrate the new "Vector Printing" feature. My purpose was to put together a bunch of lines to send to the printer, but I was unsatisfied by any result I created by hand.

So, all of a sudden, I remembered the beauty of some figures, I drawed long time ago (perhaps I was 15), using the Logo Turtle Graphics principles and decided to write some code to draw them again. The result is funny. Try yourself to move the sliders in the following application to see the figures.

The code behind the example is really simple. I wrote a basic Turtle class that is able to manage the position of the turtle. It exposes a basic set of methods that emulates the movements that are tipical of a Logo procedure. Forward, Backward, Left and Right and so on.

   1: public class Turtle
   2: {
   3:     // Collection of geometries to draw the figure
   4:     private GeometryGroup Geometries { get; set; }
   5:     // current angle of the turtle
   6:     public double Angle { get; private set; }
   7:     // current position of the turtle
   8:     public Point Position { get; private set; }
   9:     // position of the pen
  10:     public bool IsPenDown { get; private set; }
  11:  
  12:     /// <summary>
  13:     /// Inizializza una nuova istanza della classe <see cref="Turtle"/>.
  14:     /// </summary>
  15:     public Turtle(Path path)
  16:     {
  17:         this.Geometries = new GeometryGroup();
  18:         path.Data = this.Geometries;
  19:         this.Angle = 0.0;
  20:         this.Position = new Point(0, 0);
  21:         this.IsPenDown = true;
  22:     }
  23:  
  24:     // lift up the pen
  25:     public void PenUp()
  26:     {
  27:         this.IsPenDown = false;
  28:     }
  29:  
  30:     // lower the pen
  31:     public void PenDown()
  32:     {
  33:         this.IsPenDown = true;
  34:     }
  35:  
  36:     // move forward
  37:     public void Forward(double size)
  38:     {
  39:         this.Move(size);
  40:     }
  41:  
  42:     //  move backward
  43:     public void Backward(double size)
  44:     {
  45:         this.Move(-size);
  46:     }
  47:  
  48:     // rotate to the left
  49:     public void Left(double angle)
  50:     {
  51:         this.Angle += ToRadians(angle);
  52:     }
  53:  
  54:     // rotate to the right
  55:     public void Right(double angle)
  56:     {
  57:         this.Angle -= ToRadians(angle);
  58:     }
  59:  
  60:     // move the pen of the specified size
  61:     private void Move(double size)
  62:     {
  63:         Point past = this.Position;
  64:  
  65:         this.Position =
  66:             new Point(
  67:                 past.X + Math.Sin(this.Angle) * size,
  68:                 past.Y + Math.Cos(this.Angle) * size);
  69:  
  70:         if (IsPenDown)
  71:         {
  72:             this.Geometries.Children.Add(
  73:                 new LineGeometry
  74:                 {
  75:                     StartPoint = past,
  76:                     EndPoint = this.Position,
  77:                 });
  78:         }
  79:     }
  80:  
  81:     // convert degrees to radians
  82:     public double ToRadians(double angle)
  83:     {
  84:         return angle * Math.PI / 180.0;
  85:     }
  86: }

Then a short C# function does the rest and draws the figure on the basis of a simple mathematical algorithm. It simply repeat a poligon a number of times, rotating the origin point of a short amount of degrees. The sliders change the number of sides of the poligon and the amount of the rotation expressed in fraction of 360°.

   1: private void Draw(Path pathToDraw)
   2: {
   3:     int sides = (int)this.SidesSlider.Value;
   4:     double corner = 360 / (double)sides;
   5:     int repeat = (int)this.RepeatSlider.Value;
   6:     double angle = 360 / (double)repeat;
   7:  
   8:     Turtle turtle = new Turtle(pathToDraw);
   9:  
  10:     for (int j = 0; j < repeat; j++)
  11:     {
  12:         for (int i = 0; i < sides; i++)
  13:         {
  14:             turtle.Forward(50);
  15:             turtle.Left(corner);
  16:         }
  17:  
  18:         turtle.Right(angle);
  19:     }
  20: }

I'm always impressed by the beauty of the simple mathematical drawing. The prints I got from this example have been forwarded to my daughter and some hours after they are completely coloured and becomed a beautiful picture for the door of our refrigerator. I hope you like this funny Vector Printing example.

Categories:   TIPS
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed

A base class for threaded Application Services

2011-02-21T23:26:16+01:00 by Andrea Boschin

Continuing of my series about IApplicationServices I would like to propose a base class I've created to easily develop Application Services that runs a thread. There are lot of cases when you implement IApplicationService to run a parallel task that accomplish some kind of background tasks. I've collected all the redundant code inside a class I called ThreadedService. Extending this class you have a thread automatically started and stopped according with the application lifetime. Here is the class:

public abstract class ThreadedService : IDisposable, IApplicationService, IApplicationLifetimeAware
{
    /// <summary>
    /// Gets or sets the exit event.
    /// </summary>
    /// <value>The exit event.</value>
    private ManualResetEvent ExitEvent { get; set; }
 
    /// <summary>
    /// Gets or sets the wait handles.
    /// </summary>
    /// <value>The wait handles.</value>
    protected WaitHandle[] ExitHandles { get; set; }
 
    /// <summary>
    /// Initializes a new instance of the <see cref="ThreadedObject"/> class.
    /// </summary>
    public ThreadedService()
    {
        this.ExitEvent = new ManualResetEvent(false);
        this.ExitHandles = new WaitHandle[] { this.ExitEvent };
    }
 
    /// <summary>
    /// The main thread body
    /// </summary>
    protected abstract void ThreadProc();
 
    #region IDisposable Members
 
    /// <summary>
    /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
    /// </summary>
    public virtual void Dispose()
    {
        this.StopService();
    }
 
    #endregion
 
    #region IApplicationLifetimeAware
 
    public virtual void Starting()
    { }
 
    public virtual void Started()
    {
        ThreadStart listenThreadStart = new ThreadStart(ThreadProc);
        Thread thread = new Thread(listenThreadStart);
        thread.Start();
    }
 
    public virtual void Exiting()
    {
        this.ExitEvent.Set();
    }
 
    public virtual void Exited()
    { }
 
    #endregion
 
    #region IApplicationService
 
    public virtual void StartService(ApplicationServiceContext context)
    { }
 
    public virtual void StopService()
    { }
 
    #endregion
}

As already said the class is able to manage the background thread automatically but require a little level of collaboration when you develop the body of the thread. Infact after overriding the ThreadProc method you have to listen for the ExitEvent to be raised. When this event is set you have to exit from the service as soon as possible.

Saying we have to poll the newtork once every minute you can implement the ThreadProc the way I show it in the figure.

protected override void ThreadProc()
{
    this.Update();
 
    while (WaitHandle.WaitAny(this.ExitHandles, 5000) == WaitHandle.WaitTimeout)
        this.Update();
}

The procedure wait for the ExitEvent for a timeout. During this time the runtime is able to do something else but once the event is set the process is immediately exited. Something very simple but effective.

Categories:   TIPS
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed

Inject ApplicationServices with MEF reloaded: supporting recomposition

2011-02-17T16:35:03+01:00 by Andrea Boschin

In my I've presented a simple solution that allows people to adds instances of ApplicationServices, without the need of adding the markup to the App.xaml file. The solution was interesting as a demonstration of how MEF works, but after some tests and some feedbacks I had I realized it is not applicable in a real world solution because of a subtle problem.

The problem is given by a conflict in the lifetime of the application between the Application_Start event and the StartService method. If you put the code to inizialize the CompositionContainer inside of the Application_Start event you may get an error stating that the composition has already initialized, just because when the StartService method call the CompositionInitializer method, to compose the services, the container is implicitly initialized. Unfortunately the Application_Start method is called after the StartService so here is the reason of the error you get.

For a long while I've thinked this may remain only a beautiful example because I do not discovered a valid solution, but today I convinced myself that a correction to this problem is possible and that it also add the ability of supporting recomposition to the HostService. As far as I know there is not any concern in calling the methods of the service with a delay respect the runtime raises the corrisponding method, almost until the method is not one of the Exit flavour. During the exit we have to free resources, stop threads, and so on and it is important to make these actions as soon as possible to let the runtime able to close the application faster. But the initialization phase may start in a later time, with the sole constraint that the sequence of the methods should be the same.

So this morning I've worked in this direction and in the following box you can see the resulting solution:

   1: public class HostService : IApplicationService, IApplicationLifetimeAware, IPartImportsSatisfiedNotification
   2: {
   3:     /// <summary>
   4:     /// Gets or sets the context.
   5:     /// </summary>
   6:     /// <value>The context.</value>
   7:     public ApplicationServiceContext Context { get; set; }
   8:     /// <summary>
   9:     /// Gets or sets the current.
  10:     /// </summary>
  11:     /// <value>The current.</value>
  12:     public static HostService Current { get; private set; }
  13:  
  14:     /// <summary>
  15:     /// Gets or sets the initialized.
  16:     /// </summary>
  17:     /// <value>The initialized.</value>
  18:     private List<IApplicationService> Initialized { get; set; }
  19:  
  20:     /// <summary>
  21:     /// Gets or sets the services.
  22:     /// </summary>
  23:     /// <value>The services.</value>
  24:     [ImportMany(AllowRecomposition = true, RequiredCreationPolicy = CreationPolicy.Shared)]
  25:     public List<IApplicationService> Services { get; set; }
  26:  
  27:     /// <summary>
  28:     /// Initializes a new instance of the <see cref="HostService"/> class.
  29:     /// </summary>
  30:     public HostService()
  31:     {
  32:         if (HostService.Current != null)
  33:             throw new InvalidOperationException("Service already exists");
  34:  
  35:         HostService.Current = this;
  36:         this.Initialized = new List<IApplicationService>();
  37:     }
  38:  
  39:     /// <summary>
  40:     /// Called by an application immediately after the <see cref="E:System.Windows.Application.Exit"/> event occurs.
  41:     /// </summary>
  42:     public void Exited()
  43:     {
  44:         foreach (IApplicationLifetimeAware service in this.Services.OfType<IApplicationLifetimeAware>())
  45:             service.Exited();
  46:     }
  47:  
  48:     /// <summary>
  49:     /// Called by an application immediately before the <see cref="E:System.Windows.Application.Exit"/> event occurs.
  50:     /// </summary>
  51:     public void Exiting()
  52:     {
  53:         foreach (IApplicationLifetimeAware service in this.Services.OfType<IApplicationLifetimeAware>())
  54:             service.Exiting();
  55:     }
  56:  
  57:     /// <summary>
  58:     /// Called by an application immediately after the <see cref="E:System.Windows.Application.Startup"/> event occurs.
  59:     /// </summary>
  60:     public void Started()
  61:     {
  62:         CompositionInitializer.SatisfyImports(this);
  63:     }
  64:  
  65:     /// <summary>
  66:     /// Called by an application immediately before the <see cref="E:System.Windows.Application.Startup"/> event occurs.
  67:     /// </summary>
  68:     public void Starting()
  69:     {
  70:     }
  71:  
  72:     /// <summary>
  73:     /// Called by an application in order to initialize the application extension service.
  74:     /// </summary>
  75:     /// <param name="context">Provides information about the application state.</param>
  76:     public void StartService(ApplicationServiceContext context)
  77:     {
  78:         this.Context = context;
  79:     }
  80:  
  81:     /// <summary>
  82:     /// Called by an application in order to stop the application extension service.
  83:     /// </summary>
  84:     public void StopService()
  85:     {
  86:         foreach (IApplicationService service in this.Services)
  87:             service.StopService();
  88:     }
  89:  
  90:     /// <summary>
  91:     /// Called when a part's imports have been satisfied and it is safe to use.
  92:     /// </summary>
  93:     public void OnImportsSatisfied()
  94:     {
  95:         // buffer for services that raises exception during initialization
  96:         List<IApplicationService> errors = new List<IApplicationService>();
  97:  
  98:         // call StartService
  99:         foreach (IApplicationService service in this.Services.OfType<IApplicationService>().Except(this.Initialized))
 100:         {
 101:             try
 102:             {
 103:                 service.StartService(this.Context);
 104:             }
 105:             catch
 106:             {
 107:                 errors.Add(service);
 108:             }
 109:         }
 110:  
 111:         // call Starting
 112:         foreach (IApplicationLifetimeAware service in 
 113:             this.Services.OfType<IApplicationLifetimeAware>()
 114:             .Except(this.Initialized.OfType<IApplicationLifetimeAware>())
 115:             .Except(errors.OfType<IApplicationLifetimeAware>()))
 116:         {
 117:             try
 118:             {
 119:                 service.Starting();
 120:             }
 121:             catch
 122:             {
 123:                 errors.Add((IApplicationService)service);
 124:             }
 125:         }
 126:  
 127:         // call Started
 128:         foreach (IApplicationLifetimeAware service in 
 129:             this.Services.OfType<IApplicationLifetimeAware>()
 130:             .Except(this.Initialized.OfType<IApplicationLifetimeAware>())
 131:             .Except(errors.OfType<IApplicationLifetimeAware>()))
 132:         {
 133:             try
 134:             {
 135:                 service.Started();
 136:             }
 137:             catch
 138:             {
 139:                 errors.Add((IApplicationService)service);
 140:             }
 141:         }
 142:  
 143:         // register ad Initialized to avoid duplicated initialization in case of recomposition
 144:         foreach (IApplicationService service in 
 145:             this.Services.OfType<IApplicationService>()
 146:             .Except(this.Initialized)
 147:             .Except(errors))
 148:             this.Initialized.Add(service);
 149:  
 150:         // remove services that raised exceptions
 151:         foreach (IApplicationService service in errors)
 152:             this.Services.Remove(service);
 153:     }
 154: }

As you can see the structure of the service remains the same, and also the management of the Exit, Exited and StopService is not changed. The very important change is in the Started method, that is called after the Application_Start event. In this method I only perform the composition. Then, in the OnImportsSatisfied method I start a sequence to emulate the startup of the service, repeating the flow that an ApplicationService usually follow during it initialization. I've also added some code to manage exceptions raised during the initialization. In these cases the services that raised the errors do not follow the remaining parts of the initialization and finally are removed from the Services collection.

In the service I maintain a collection of Initialized services because if the recomposition happen I must not repeat the inizialization of services that were already initialized previously. This is also the reason that make me asking a Shared CreationPolicy, because when the composition container compose the Services property does not have to create new instances but only have to add the new services coming from the recomposition.

I hope this solution to be more reliable than the previous one. As usual I will be glad to have feedback from you to continue improving the code..

Categories:   TIPS
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed

Singleton ApplicationServices explained

2011-02-17T09:44:39+01:00 by Andrea Boschin

My has opened to some unexpected questions about what may seems a strangeness in my code. I was surprised about this questions, just because I believed it was a known pattern, so I decided to give an explanation to ones that does not know the reason I wrote some lines of code in the constructor

   1: public class SampleService : IApplicationService
   2: {
   3:     public static SampleService Current { get; private set; }
   4:  
   5:     /// <summary>
   6:     /// Initializes a new instance of the <see cref="ServiceHost"/> class.
   7:     /// </summary>
   8:     public SampleService()
   9:     {
  10:         if (SampleService.Current != null)
  11:             throw new InvalidOperationException("Service already exists");
  12:  
  13:         SampleService.Current = this;
  14:     }
  15:  
  16:     // omissis...
  17: }

The problem here is that sometimes it is not useful (and sometimes it is definitely bad) to have more than an instance of an ApplicationService running behind the scenes. Given that none can limit the number of instances in the ApplicationLifetimeObjects collection I use a pattern to avoid this problem.

First of all I expose a static property "Current". This property is useful to interact with the running service from any part of the code. Since after the beta there is not an indexer to access the running service this is the most common pattern used for this purpose.

Then in the constructor I check the property "Current" to be null. If I found a value it means someone is trying to put two instances of the service in the markup, so I raise an exception stating that only one instance is allowed. In the case "Current" is empty I save myself to this property using "this".

So nothing is wrong in my previous example. Only few lines of code to implement a Singleton pattern for IApplicationService, with a tecnique that is widely used also inside the framework classes.

Categories:   TIPS
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed

Inject Application Services using MEF

2011-02-06T22:33:47+01:00 by codeblock

For an example I'm writing for my next presentation at the end of February, I’m trying to build an application skeleton that have lot of extensibility points and my goal is to make almost everything pluggable. An interesting thing I did today is to inject in the application a number of ApplicationServices. The application services are an useful tool to have application-wide services available during the entire lifetime, and they are very easy to create and install. First of all you have to implement the IApplicationService interface and if you want a more detailed control on application lifecycle you can also implement the IApplicationLifetimeAware interface. These interfaces give you a number of methods that notify when the application is started of stopped. Here is an example of a service and its installation:

   1: public class SampleService : IApplicationService
   2: {
   3:     public static SampleService Current { get; private set; }
   4:  
   5:     /// <summary>
   6:     /// Initializes a new instance of the <see cref="ServiceHost"/> class.
   7:     /// </summary>
   8:     public SampleService()
   9:     {
  10:         if (SampleService.Current != null)
  11:             throw new InvalidOperationException("Service already exists");
  12:  
  13:         SampleService.Current = this;
  14:     }
  15:  
  16:     /// <summary>
  17:     /// Called by an application in order to initialize the application extension service.
  18:     /// </summary>
  19:     /// <param name="context">Provides information about the application state.</param>
  20:     public void StartService(ApplicationServiceContext context)
  21:     {
  22:         // initialize here the service
  23:     }
  24:  
  25:     /// <summary>
  26:     /// Called by an application in order to stop the application extension service.
  27:     /// </summary>
  28:     public void StopService()
  29:     {
  30:         // release resources here
  31:     }
  32: }
  33:  
  34: // -----------
  35:  
  36: <Application.ApplicationLifetimeObjects>
  37:     <local:SampleService />
  38: </Application.ApplicationLifetimeObjects>

Today I've asked myself how can I inject an application service with MEF. If I have many modules I plug to my skeleton, let say they are customization for different customers, I suppose I can also have one or more application services injected by these modules for the purpose of the customization. To solve this pluggability problem I’ve created a simple HostService that implement both IApplicationService and IApplicationLifetimeAware, then I added a property, imported with MEF, that is a collection of IApplicationService. So when I load the ApplicationService in the App.xaml the constructor perform the Composition and I have a bunch of services loaded into the host service.

   1: public class HostService : IApplicationService, IApplicationLifetimeAware
   2: {
   3:     public static HostService Current { get; private set; }
   4:  
   5:     /// <summary>
   6:     /// Gets or sets the services.
   7:     /// </summary>
   8:     /// <value>The services.</value>
   9:     [ImportMany]
  10:     public IEnumerable<IApplicationService> Services { get; set; }
  11:  
  12:     /// <summary>
  13:     /// Initializes a new instance of the <see cref="ServiceHost"/> class.
  14:     /// </summary>
  15:     public HostService()
  16:     {
  17:         if (HostService.Current != null)
  18:             throw new InvalidOperationException("Service already exists");
  19:  
  20:         HostService.Current = this;
  21:         CompositionInitializer.SatisfyImports(this);
  22:     }
  23:  
  24:     /// <summary>
  25:     /// Called by an application immediately after the <see cref="E:System.Windows.Application.Exit"/> event occurs.
  26:     /// </summary>
  27:     public void Exited()
  28:     {
  29:         foreach (IApplicationLifetimeAware service in this.Services.OfType<IApplicationLifetimeAware>())
  30:             service.Exited();
  31:     }
  32:  
  33:     /// <summary>
  34:     /// Called by an application immediately before the <see cref="E:System.Windows.Application.Exit"/> event occurs.
  35:     /// </summary>
  36:     public void Exiting()
  37:     {
  38:         foreach (IApplicationLifetimeAware service in this.Services.OfType<IApplicationLifetimeAware>())
  39:             service.Exiting();
  40:     }
  41:  
  42:     /// <summary>
  43:     /// Called by an application immediately after the <see cref="E:System.Windows.Application.Startup"/> event occurs.
  44:     /// </summary>
  45:     public void Started()
  46:     {
  47:         foreach (IApplicationLifetimeAware service in this.Services.OfType<IApplicationLifetimeAware>())
  48:             service.Started();
  49:     }
  50:  
  51:     /// <summary>
  52:     /// Called by an application immediately before the <see cref="E:System.Windows.Application.Startup"/> event occurs.
  53:     /// </summary>
  54:     public void Starting()
  55:     {
  56:         foreach (IApplicationLifetimeAware service in this.Services.OfType<IApplicationLifetimeAware>())
  57:             service.Starting();
  58:     }
  59:  
  60:     /// <summary>
  61:     /// Called by an application in order to initialize the application extension service.
  62:     /// </summary>
  63:     /// <param name="context">Provides information about the application state.</param>
  64:     public void StartService(ApplicationServiceContext context)
  65:     {
  66:         foreach (IApplicationService service in this.Services)
  67:             service.StartService(context);
  68:     }
  69:  
  70:     /// <summary>
  71:     /// Called by an application in order to stop the application extension service.
  72:     /// </summary>
  73:     public void StopService()
  74:     {
  75:         foreach (IApplicationService service in this.Services)
  76:             service.StopService();
  77:     }
  78: }

As you can see in the code, for every method exposed by the interfaces I make a loop and call the corresponding method on each service. The services have to be exported ad IApplicationService, but if they implements also the other interface the host call the relative methods. To export a service you can simple decorate it with an Export attribute.

   1: [Export(typeof(IApplicationService))]
   2: public SampleService : IApplicationService
   3: {
   4:     // implement the service
   5: }

Once installed, this very simple service let you easily inject new services without the need of recompile the software. Simply add the assembly where it resides to the catalog and the services will be installed seamless.

Magic…

Categories:   TIPS
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed

Remove Transformation from shapes in Blend

2011-01-24T13:49:54+01:00 by Andrea Boschin

Recently I'm working hard with Expression Blend to create some interfaces starting from some Illustrator mockups, made by a designer and also creating the user interface of some Windows Phone 7 applications I developed. One thing I've found annoying is that once you create a shape and you rotate it in the designer, Blend uses a CompositeTransform to render the shape in the XAML markup. If you are creating lot of elements (in my case they were the ticks of a gauge) you can get lot of transformations that the plugin or the phone have to render. And to me (particularly from my developer side), this is no a good thing since many times a rotated rectangle might be represented as a Path instead of using the CompositeTransform.

After realizing that creating the shape by hand is not an option I searched hard the options in the menu items because in my mind it was obvious to have a "Make transform static" option. But it simply does not exists (or at least I was unable to find it). So for a long time I decided to close my eyes and accept the Transformations. This until I found a tricky way to convince Blend to perform this operation as a side effect of a sequence of common operations. Here is the process I found:

First of all create the Shape. In this example I will use a Rectangle:

Capture1

Then I apply the Transformation I need. For this example I simply rotate the shape with the mouse of an amout of degrees:

Capture2

Now select the shape and find the option in the context menu (or in the Objects Tree) to make the shape a Path (Path -> Convert To Path). Obviously, to remove the transformation, you have to make the rectangle a Path. Once you have done this you will have this in the Object tree:

Capture3

As now Blend have performed the conversion but the CompositeTrasform is still in place. To convince Blend we have another couple of steps. Now Draw another shape (a rectangle) over the shape to convert. See in figure the result:

Capture4

Then Convert also this Rectangle to a Path. After this step the Objects Tree will show two paths one on top of the other. In the figure I've named the upper Shape to "Mask":

Capture5

Finally, and it is the tricky thing you have to deselect both the Paths (e.g. selecting the LayoutRoot element) then select again them starting from "Mask" then (using CTRL) the shape to convert. It is important to be aware that in the conversion the order matter. If you select the shapes in the inverse order you will not reach the required result. Then finally from the context menu select Combine -> Intersect. The resulting shape is this:

Capture6

The type of selection makes evident that now the Shape ha no rotation but we can see at the markup and the result is reported here:

   1: <Grid x:Name="LayoutRoot" Background="White">
   2:     <Path x:Name="Mask" 
   3:         Data="M29.362633,1.5 L101.14476,29.362633 L73.282127,101.14476 L1.5,73.282127 z" 
   4:         Fill="#FFFFD200" 
   5:         Margin="228.678,148.678,308.678,228.678" 
   6:         Stretch="Fill" Stroke="Black" StrokeThickness="3" UseLayoutRounding="False"/>
   7: </Grid>

Now you are ready to put the shape wherever you need it. I've experimented the trick in many ways and until today I never found a case where it does not works. The sole things you have to keep in mind is the right order of the steps, and that you can convert only a single shape at a time. So for my Gauge I had to make the conversion many times.

As a suggestion I think it would be beautiful to have an additional menu item that does the trick automatically... If someone knows a better way please feel free to write me and I will be very grateful.

Technorati Tags: ,,
Categories:   TIPS
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed

Simplify localization with a T4 template

2010-11-25T23:07:04+01:00 by Andrea Boschin

If you evere tryied to localize a Silverlight application, you have found for sure that it is not a straighforward task. Like all the other .NET tecnologies the Silverlight projects has resource files and you can create satellite assemblies to contains culture specific strings. But the problem is that it is not so easy to bring this string to the interface.

When you create a resx file Visual Studio generates a code file with a class ready to be used to read the strings. You can also choose to make this class public, so it is possible to use it across different projects. Unfortunately this class has an internal constructon so there is no way to add an instance to the App.xaml resources and use DataBinding to reference the strings in the user interface.

Someone suggest to manually change the ctor of the class but to me it is very frustrating to correct this simple bug every time I add a new resource. So it is required to create a proxy class, that wraps the static one. Every property will call the correspondent static property and then the proxy class will be used for the DataBinding. When you are developing a simple application this is a simple task, but what about when you have an huge number of strings?

With this in mind I've worked to a T4 template that it is able to read a resx file and automatically produces the proxy class. If you do not know it, the T4 template is a very powerful tool that exists since Visual Studio 2008. With it you can write code to generate code inside Visual Studio, and it is exactly what I'm searching for.

Here is the template:

   1: <#@ template debug="false" hostspecific="true" language="C#" #>
   2: <#@ output extension=".cs"  encoding="UTF8" #>
   3: <#@ assembly name="System.Core" #>
   4: <#@ assembly name="System.Xml" #>
   5: <#@ assembly name="System.Xml.Linq" #>
   6: <#@ import namespace="System.IO" #>
   7: <#@ import namespace="System.Text.RegularExpressions" #>
   8: <#@ import namespace="System.Linq" #>
   9: <#@ import namespace="System.Xml" #>
  10: <#@ import namespace="System.Xml.Linq" #>
  11: //------------------------------------------------------------------------------
  12: // <auto-generated>
  13: //     This code was generated by a tool.
  14: //
  15: //     Changes to this file may cause incorrect behavior and will be lost if
  16: //     the code is regenerated.
  17: // </auto-generated>
  18: //------------------------------------------------------------------------------
  19:  
  20: <#
  21: string appName = "ResXProxy Generator Template";
  22: string version = "1.0.3977.0";
  23: string ns = (string)System.Runtime.Remoting.Messaging.CallContext.LogicalGetData("NamespaceHint");
  24: string resxFileName = Path.ChangeExtension(Host.TemplateFile, ".resx");
  25: string resxClassName = Path.GetFileNameWithoutExtension(Host.TemplateFile);
  26: string proxyClassName = string.Format("{0}ResourceProxy", resxClassName);
  27: XDocument document = XDocument.Parse(File.ReadAllText(resxFileName));
  28: #>
  29: namespace <#=ns#>
  30: {
  31:     using System.Globalization;
  32:     using System.Windows.Markup;
  33:  
  34:     /// <summary>
  35:     /// Represent a proxy class for "<#= resxClassName #>" resources
  36:     /// </summary>
  37:     [System.Diagnostics.DebuggerStepThroughAttribute()]
  38:     [System.CodeDom.Compiler.GeneratedCode("<#= appName #>", "<#= version #>")]
  39:     public class <#= proxyClassName #>
  40:     {
  41:         /// <summary>
  42:         /// Initializes the "<#= proxyClassName #>" class
  43:         /// </summary>
  44:         public <#= proxyClassName #>()
  45:         {}
  46:     
  47:         /// <summary>
  48:         /// Gets the current culture
  49:         /// </summary>
  50:         [System.CodeDom.Compiler.GeneratedCode("<#= appName #>", "<#= version #>")]
  51:         public CultureInfo CurrentCulture 
  52:         { 
  53:             get { return CultureInfo.CurrentCulture; } 
  54:         }
  55:  
  56:         /// <summary>
  57:         /// Gets the current UI Culture
  58:         /// </summary>
  59:         [System.CodeDom.Compiler.GeneratedCode("<#= appName #>", "<#= version #>")]
  60:         public CultureInfo CurrentUICulture 
  61:         {
  62:             get { return CultureInfo.CurrentUICulture; } 
  63:         }
  64:         
  65:         /// <summary>
  66:         /// Gets the current Xml Language property
  67:         /// </summary>
  68:         [System.CodeDom.Compiler.GeneratedCode("<#= appName #>", "<#= version #>")]
  69:         public XmlLanguage Language 
  70:         { 
  71:             get { return XmlLanguage.GetLanguage(CultureInfo.CurrentUICulture.Name); } 
  72:         }
  73: <# foreach(var item in document.Element("root").Elements("data")) 
  74:    { 
  75:         string name = EscapeName(item);
  76:     
  77:         if (item.Attributes("type").Count() == 0)
  78:         {
  79: #>
  80:  
  81:         /// <summary>
  82: <# if (item.Elements("comment").Count() == 1) { #>
  83:         /// <remarks><#= item.Element("comment").Value #></remarks>
  84: <# } #>
  85:         /// Gets the "<#= name #>" Property
  86:         /// </summary>
  87:         [System.CodeDom.Compiler.GeneratedCode("<#= appName #>", "<#= version #>")]
  88:         public string <#= name #> 
  89:         { 
  90:             get { return <#= resxClassName + "." + name #>; } 
  91:         }
  92: <# 
  93:         }
  94: }
  95: #>
  96:     }
  97: }<#+
  98: public string EscapeName(XElement item)
  99: {
 100:     string name = item.Attribute("name").Value;
 101:     return Regex.Replace(name, "[^a-zA-Z0-9_]{1,1}", "_");
 102: }
 103: #>

Using the template is really easy. First of all you have to configure the Silverlight application to be localizable ( you can find how to do)  and then create the required resx files. Finally you have to add the template using the same name of the main resource file but using the .tt extension. E.g. if your resx is called AppString.resx you have to call the template AppStrings.tt. Nothing else. Once you have added the template it automatically generates a class called AppStringResourceProxy you can easily reference from a XAML file and bind to the UI. Remember that every time you change the resx file you have to select "Run custom tool" from the context menu of the T4 template to generate the code again.

The generated class does not contains only the string properties but also expose a Language property. You can bind this property to the corresponding Language property of the page (the UserControl) and override the default culture that is usually set to "en-US". Doing this you will have also the Dates and numeric values converted to the right culture.

   1: <UserControl Language="{Binding Language, Source={StaticResource AppString}}" />

The tamplate works fine for Silverlight and Windows Phone. I've never tryied but I think it works also with WPF. Hope this helps.

Download Sample: Silverlightplayground.Localization.zip (96,3 KB)

WP7 Fast Tips: Load (decode) and Save (encode) Jpeg files

2010-10-15T11:40:05+01:00 by Andrea Boschin

Notice: I've added the new category "WP7 Fast Tips" to collect fast tips I found while developing my Windows Phone Applications. You Don't expect only great tips in this category but also simple things I need fo remember or I found with some difficulties :)


Working on an application I hope to publish soon on the marketplace, I have the need to save an image from a WriteableBitmap instance into a Jpeg file. It was not an easy search but finally I found a couple of methods, added as Extension Methods to the WriteableBitmap class itself. If you need to encode a jpeg to a byte array you can use this code:

   1: public byte[] EncodeToJpeg(WriteableBitmap wb)
   2: {
   3:     using(MemoryStream stream = new MemoryStream())
   4:     {
   5:         // this is Extensions.SaveJpeg() method
   6:         wb.SaveJpeg(stream, wb.PixelWidth, wb.PixelHeight, 0, 85);
   7:         return stream.ToArray();
   8:     }
   9: }

The parameters of the SaveJpeg method require a stream to write to, the size of the bitmap to generate and the quality from 0 to 100. The fourth parameter, called orientation, currently is not used and you can set it to 0 (zero).

The counterpart of the SaveJpeg methos is LoadJpeg that you can use to take a Jpeg file and load it into a WriteableBitmap instance. A very useful feature. If you need to write the Jpeg to the MediaLibrary probably the better is to pass the generated stream directly to the Save method.

Tags:  
Categories:   TIPS | Windows Phone 7
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed

A generic enum converter to map values...

2010-09-03T16:38:49+01:00 by Andrea Boschin

Many times I have to deal with enumerated values with databinding, and often this requires  the writing of a converter to map the enum to something else. As an example you can have a Status enum and you need to transform it to a color (or a brush) in the UI, or you have a series of values from the enumerator and need to change it to an image when it is presented to the user.

Finally these days I found a generic solution to these problems that let me create a mapping from a value (an enumerator, a series of integers, of also a boolean) to an instance of another object. To give you an example think at having this enumerator:

   1: public enum MessageType
   2: {
   3:     Normal,
   4:     Urgent,
   5:     Critical
   6: }

We want to map the values to a series of colors having White for Normal, Orange for Urgent and Red for Critical. Since usually you can override IValueConverter and write a specific converter that handles this enum I found useful to create a parametric value converter that lets me write the code below:

   1: <UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
   2:              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
   3:              xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
   4:              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
   5:              xmlns:cv="clr-namespace:SilverlightPlayground.Converters;assembly=SilverlightPlayground"
   6:              mc:Ignorable="d" x:Class="SilverlightPlayground.MyApplication.Shell" d:DesignWidth="456">
   7:     <UserControl.Resources>
   8:         <cv:EnumConverter x:Key="mapTypeToBrush">
   9:             <cv:EnumConverter.Items>
  10:                 <SolidColorBrush Color="#00000000" />
  11:                 <SolidColorBrush Color="#FFFF9900" />
  12:                 <SolidColorBrush Color="#FFFF0000" />
  13:             </cv:EnumConverter.Items>
  14:         </cv:EnumConverter>
  15:     </UserControl.Resources>
  16:     <Grid x:Name="LayoutRoot">
  17:         <Rectangle Fill="{Binding ReveivedMessageType, Converter={StaticResource mapTypeToBrush}}" Width="100" Height="100" />
  18:     </Grid>
  19: </UserControl>

The beautiful of this converter is that with a single converter you can create a series of mappings in the App.xaml resources and share them across all the applications and centralize the conversion of application wide enums in a single position. The code of the converter is really simple and straightforward:

   1: public class EnumConverter : IValueConverter
   2: {
   3:     private List<object> items;
   4:  
   5:     public List<object> Items 
   6:     { 
   7:         get
   8:         {
   9:             if (items == null)
  10:                 items = new List<object>();
  11:  
  12:             return items;
  13:         }
  14:     }
  15:  
  16:     public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  17:     {
  18:         if (value == null)
  19:             throw new ArgumentNullException("value");
  20:         else if (value is bool)
  21:             return this.Items.ElementAtOrDefault(System.Convert.ToByte(value));
  22:         else if (value is byte)
  23:             return this.Items.ElementAtOrDefault(System.Convert.ToByte(value));
  24:         else if (value is short)
  25:             return this.Items.ElementAtOrDefault(System.Convert.ToInt16(value));
  26:         else if (value is int)
  27:             return this.Items.ElementAtOrDefault(System.Convert.ToInt32(value));
  28:         else if (value is long)
  29:             return this.Items.ElementAtOrDefault(System.Convert.ToInt32(value));
  30:         else if (value is Enum)
  31:             return this.Items.ElementAtOrDefault(System.Convert.ToInt32(value));
  32:  
  33:         throw new InvalidOperationException(string.Format("Invalid input value of type '{0}'", value.GetType()));
  34:     }
  35:  
  36:     public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  37:     {
  38:         if (value == null)
  39:             throw new ArgumentNullException("value");
  40:  
  41:         return this.Items.Where(b => b.Equals(value)).Select((a, b) => b);
  42:     }
  43: }

The converter has a list of items it use to make the conversion of integer values. It maps the integer to the index of the list and returns the n-th element found in the array. I didn't handled the out of bounds exception just because the errore returned to the user by the converter is already good to understand what it happen. The converter itself can handle both the directions if the Equals method is correctly overrided.

Finally, I  supported boolean values so it is possible to pass to the converter true/false and having it select the items from a list of two elements. This let me also map the Visibility to boolean values (that is a pretty frequent situation):

   1: <cv:EnumConverter x:Key="bool2Visibility">
   2:     <cv:EnumConverter.Items>
   3:         <Visibility>Collapsed</Visibility>
   4:         <Visibility>Visible</Visibility>
   5:     </cv:EnumConverter.Items>
   6: </cv:EnumConverter>

As you can see the trick is really simple but as usual simple things are the most beautiful and useful. I hope someone can judge this tip good to spare time. It has been very great for me.