Author Archive
PRISM Tutorials
by Andrew Shough on Aug.12, 2010, under Silverlight, Using PRISM in Silverlight 4
I recently worked on a Silverlight project that used the PRISM framework, and discovered that, while there are numerous resources out there on how to use it, I had to trawl through search results, various blogs, and help files to find the information I needed on doing various things with PRISM & Unity.
Which brings me to this post. I have decided to write a series of tutorials on how to use PRISM and Unity in Silverlight to develop a MVVM application. These will be written assuming that you, the reader, have some Silverlight knowledge, programming experience in C#, and know how to use Visual Studio. I will post code examples and source code with every tutorial so that you may view the complete working example.
You are welcome to contact me if you need any help with regards to anything discussed in these tutorials.
So what are you going to need:
- Visual Studio 2010 (C# Express version will do).
- Silverlight 4 SDK.
- Silverlight 4 ToolKit for Visual Sudio 2010 – Current release is April 2010.
- PRISM from Microsoft’s Patterns & Practices – Current release is August 2010 CTP.
You can get PRISM from Codeplex, and all the Silverlight bits can be downloaded from silverlight.net.
The PRISM download is a self-extracting install of the source code. Just follow the readme on how to compile the code into the required assemblies.
Please note that while PRISM is for both Silverlight and WPF, I will be focusing on Silverlight for now. WPF is not that different apart from some minor differences here and there.
So the first tutorial on setting up the project and creating a shell will follow shortly.
Catch you on the flipside.
Asynchronous Tasks in Silverlight
by Andrew Shough on Jul.15, 2010, under Silverlight
Anyone who has developed in Silverlight knows that you can’t block the UI, silverlight actually throws an exception if you block the UI. So most tasks need to be done asynchronously, but for some strange reason I can’t find this type of functionality built into the silverlight framework, one would think that Microsoft would supply a way of doing this without having to write your own threading, something like the following:
public void LoadData()
{
int result = 0;
Task.Execute( () => result = SomeMethod(), (e) => taskComplete(e, result));
}
public int SomeMethod()
{
return 2 * (100 / 5) + 1000;
}
public void taskComplete(Exception e, int result)
{
if(e != null)
// do something with the exception.
else
// do something with the result.
}
But since I couldn’t find one in the silverlight framework, I wrote one for my own use:
public class Task<T>
{
public Task(Func<T> function, Action<T> completed)
{
_func = function;
_completed = completed;
}
private Func<T> _func;
private Action<T> _completed;
private Dispatcher _dispatcher;
public void Execute()
{
Thread thread = new Thread(Start);
thread.Start();
}
private void Start()
{
var result = _func();
_dispatcher.BeginInvoke(_completed, result);
}
}
public class Task
{
public Task(Dispatcher dispatcher, Action action, Action<Exception> completed)
{
_action = action;
_completed = completed;
_dispatcher = dispatcher;
}
public Task(Action action, Action<Exception> completed)
{
_action = action;
_completed = completed;
}
private Dispatcher _dispatcher;
private Action _action;
private Action<Exception> _completed;
private Exception _exception;
public void Execute()
{
Thread thread = new Thread(Start);
thread.Start();
}
public static void Execute(Dispatcher dispatcher, Action action, Action<Exception> completed)
{
new Task(dispatcher, action, completed).Execute();
}
public static void Execute(Action action, Action<Exception> completed)
{
new Task(action, completed).Execute();
}
public static void Execute<T>(Func<T> action, Action<T> completed)
{
new Task<T>(action, completed).Execute();
}
private void Start()
{
try
{
_action();
}
catch (Exception ex)
{
_exception = ex;
}
_dispatcher.BeginInvoke(_completed, _exception);
}
}
All you need to do to use it, is the same as I did right at the top:
public void LoadData()
{
int result = 0;
Task.Execute( () => result = SomeMethod(), (e) => taskComplete(e, result));
}
public int SomeMethod()
{
return 2 * (100 / 5) + 1000;
}
public void taskComplete(Exception e, int result)
{
if(e != null)
// do something with the exception.
else
// do something with the result.
}
or you can do it without the complete method:
public void LoadData()
{
int result = 0;
Task.Execute(
() => result = SomeMethod(),
(e) =>
{
if(e != null) // do something with the error.
else // do something with the result.
});
}
public int SomeMethod()
{
return 2 * (100 / 5) + 1000;
}
Hope this helps someone with their silverlight UI development.