Jamil Rzayev’s Blog

practice solutions for everyday

Styles Dictionary

Posted by Jamil Rzayev on December 5, 2008

Each time I build rich user window on WPF I think I have to write the same lines of code of style concept for each buttton or textbox. And the following question comes in mind: Why I don’t place all my styles in one XAML file to dynamically load them in future?

For example, if I have five buttons and each of them must have 40 in Width, 21 in Height, GradientColorBrush  as BackgroundColor, WhiteSmoke color as Foreground and Margin equals to 5. I simply can write for each button the following lines of code:

threebuttonstylecode

It is easy to understand that writing for each cotrol separate style code will blow our XAML file and you as a developer willl have to spend much more time to cover all UI code in future.

WPF allows us to create special XAML file for filling it with styles and load them as we need.

Such type of file is named ResourceDictionary. It is basic XAML file with following header:

styledictionaryheadercode

Then we write special lines of code for each style. using Style tag into that file between ResourceDictionary tags. For each important property which I want to assign a value I use Setter tag with Property and Value attributes.

That is all I need to do. For the moment I make some changes in window xaml code by using in each control declaration Style poperty and assign to it name of style. i only use Style tag and any additional attrubutes are deleted.

Style={DynamicResource ButtonStyle}”

At last to make it work correctly I put code to App.xaml file to load styles at application startup. I put file with styles in separate folder Styles.

That technique requires a bit more time to be accustomed with dictionaries but in everyday practice it reduces great time of development

Posted in WPF | 1 Comment »

Common solution for cross-thread problem on WinForms

Posted by Jamil Rzayev on December 4, 2008

Last two months I write on C# for my new project. Several days ago I needed to implement asynchronous invokation of EventHandler at button click. by using new instance of EventHandler I wrote needed code with lambda expression for AsyncCallBack. But, unfortunately, my code crashed. 

After first application running I received following message after button clicking:

crossthreaderrormsgpic1

 

At MSDN forum I found solution for that exception and they were used BackgroundWorker. 

http://msdn.microsoft.com/en-us/library/ms171728.aspx

But if you have many different methods which must change some properties of controls on your form that technique is not acceptable. I realized that I would be nice to provide only name of appropriate method in my form’s class and to run it asynchronously.

My idea is acceptable only for C# 3.0 because I use extension methods. 

For example, we can create new class named ExtHelper and implement one single method named SafeCall.

Static class to implement extension method

Static class to implement extension method

Now I can use the method SafeCall in my AsyncCallback delegate to invoke needed methods.
For example, I have methods named Do, Do2.
usingextensionmethod

That mehods carry out simple gui-based operations but they distinctly show the solution’s principle.

Posted in C# | Tagged: , , | Leave a Comment »

Intro

Posted by Jamil Rzayev on December 2, 2008

Welcome. My name is Jamil Rzayev.

This blog is about all in programming world. As I like .Net technology I will start with it.

Posted in C# | Tagged: | Leave a Comment »