Wednesday, October 22, 2008

Trips&Ticks: Do you know how do you need to change properties of local file when it will be loaded, using a Relative Uri?

When I had tried to use Relative URIs for local files I found next trouble - resource wasn't found in my app. Solution for this problem is next actions: we should open properties of local file and change flag named "Copy to Output Directory" from "Do not Copy" to "Copy always".
Easy and logically.

PS: Nice article about URI in WPF - Pack URIs in Windows Presentation Foundation

Wednesday, October 15, 2008

Really multiUI window in WPF project.


Download
binary file - here
Download source files - here


Introduction.

WPF presents new way to implement visual appearance of application. We can implement it in XAML and C# files both or separately. We use Application.LoadComponent( Object, Uri ) method for load XAML files where "Uri" presents way to XAML file. So we can manipulate visual appearance presented in XAML's files load different XAML's files.

Note: We can't use x:Class attribute for root node in XAML's files. In this case compiler automatic generates C# code (in *.g.cs file) and those classes will be conflicted (more precisely - some automatic generated members will be conflicted). Thus we can't use x:Code and describe to events in XAML's files (code for support those actions were implemented in *.g.cs files).

Note: Class have been generated in XAML's file (more precisely - in *.g.cs) is normal class likes class have been generated in C# code and even we can inherit a custom class from it.

My sample.
My sample presents two mode for manager and worker that to show the personal's information. We load different XAML's files against of mode. First we show dialog window that propose to choose behaviour.

Different XAML's contains different class instants for present data:
PeopleEx (People - for worker) - presents data of personnel (less for worker and more for manager).
ManagerInfoPresenter (WorkerInfoPresenter - for worker) - get a information (different for other mode).
ManagerCapacityShow (WorkerCapacityShow - for worker) - show a diagram. It is fake but it is different for other mode.

We can implement different controls with one name in other XAML's files (for example "InformCheck" or "CapacityShow"). We found those controls in XAML's files to use FindName(String) method and we can handle some events of this controls.

CheckBox informer = (CheckBox)FindName( INFORMCHECK_NAME );
informer.Checked += new RoutedEventHandler( OnInformerChecked );
informer.Unchecked += new RoutedEventHandler( OnInformerUnchecked );


We can apply to different class members to use base class (like m_CapacityShow member) or interface (like m_InfoPresenter member).

if( AppMode.Manager == appMode )
{
...
m_InfoPresenter = new ManagerInfoPresenter();
}
else
{
...
m_InfoPresenter = new WorkerInfoPresenter();
}
...
m_CapacityShow = (Image)FindName( CAPACITYSHOW_NAME );


Summary
We can really create one class window for two (or more) behaviour of application. We don't hide any controls or elements we don't load they! It is elegant way to dynamic create application.

Sunday, October 12, 2008

Little deception of List.ForEach(Action action)

I use foreach statement really often. It is useful and more compact than for. (This way is fast in same case but in same case it is slowly for performance. But I want say not about that). List<T> class has ForEach(Action action) method I thought that its implementation (and behaviour) is like foreach statement (I make this conclusion, based on the name of method) but I was confused. I have looked to Net. Reflector and saw next code:

public static void ForEach(T[] array, Action action)
{
if (array == null)
{
throw new ArgumentNullException("array");
}
if (action == null)
{
throw new ArgumentNullException("action");
}
for (int i = 0; i < array.Length; i++)
{
action(array[i]);
}
}

ForEach(Action action) method behaviour likes for statement behaviour. And I can use it like for. For example I can change main collection during execution action delegate.