Friday, April 27, 2012

Implementing the theme concept in C# client application

- Shanthi karthika
Its a common practice to include images either from the project resource files or from the settings file in any C# project. This heads to do changes in code for any change in an image. To overcome this and  to load the images at the runtime Theme Concept can be used.
The theme concept is a way of segregating the images to a class library and loading the images at the runtime. The images are to be changed only in the dll and so the source code will not be disturbed.
Below, I have created a demo project in which theme concept has been implemented. This sample can be used to know the way how theme concept can be used.

Sample Demo For Theme:
Following steps can be done to implement Theme.

Step 1:
In Visual Studio Create a windows application. For instance, here I name it is as CssStorage.

Step2:
Create an interface class inside the CssStorage and name it as InterfaceImages. Implement all the images in the interface which are needed for the application.
namespace CssStorage
{
public interface InterfaceImages
{
Bitmap AddButton { get; }
Bitmap DeleteButton { get; }
}

Compile CssStorage which will create a dll, for instance here it creates CssStorage.exe.

Step3:
Create a class library, for instance name it as CssTheme. Add a project resource file which contains the images that are needed for the application.

Step4:
Add the CssStorage.exe to CssTheme and make a reference to it.



Step 5:
Create a class in CssTheme for instance name it as Images, which implements the images for which interface have been build.

using CssStorage;

namespace CssTheme
{
 public class Images : InterfaceImages
 {
  public Bitmap AddButton
  {
   get { return Properties.Resource.AddButtonImage; }
  }
 public Bitmap DeleteButton
 {
  get { return Properties.Resource.DeleteButtonImage; }
 }
}
}


Step 6:
Compile CssTheme and extract the dll. Add the dll to the CssStorage. Now, in CssStorage make a call to the assembly to retrieve the path of CssTheme. Now call the images through the assembly.
Hashtable mAssemblies = new Hashtable();
Interface lCommand;
Assembly lAssembly = null;
String filename= “CssTheme”;

string themePath = Assembly.GetExecutingAssembly().Location.Replace(Assembly.GetExecutingAssembly().ManifestModule.ScopeName, "");
// Retrives the path where the CssTheme.dll Exists.

if (mAssemblies != null && mAssemblies.ContainsKey(fileName + ".dll") == false)
{
 lAssembly = Assembly.LoadFile(Path.Combine(themePath, fileName +     ".dll"));
 mAssemblies.Add(fileName + ".dll", lAssembly);
}

// retrived Images are casted to interface.
lCommand = default(InterfaceImages);
lCommand = (InterfaceImages)createObject(fileName + ".Images");

private object createObject(string lObjectType) //to retrive Images from the CssTheme
{
try
{
 object lObject = null; int lAssemblyCount = 0;
 lAssemblyCount = mAssemblies.Count;
 lAssembly = Assembly.GetCallingAssembly();
 lObject = lAssembly.CreateInstance(lObjectType);
 if (lObject == null)
 {
  lAssembly = Assembly.GetExecutingAssembly();
  lObject = lAssembly.CreateInstance(lObjectType);
 }
 if (lObject == null)
 {
  IDictionaryEnumerator enumerator = mAssemblies.GetEnumerator();
  while (enumerator.MoveNext())
  {
   lAssembly = (Assembly)enumerator.Value;
   lObject = lAssembly.CreateInstance(lObjectType);
   if (lObject != null)
   {
    break;
   }
  }
}
return lObject;
}
catch (DllNotFoundException ex)
{
throw ex;
}
catch (Exception ex)
{
throw ex;
}
}

Now the images will be retrieved and it can be used where ever needed by calling the Interface Object Icommand

uxAddPictureBox.Image = lCommand.AddButton;
uxDeletePictureBox.Image = lCommand.DeleteButton;

Step 7:
Compile the project. The images will be loaded at the runtime.

Implement the ThemeConcept and get new experience of loading images at runtime from a class library. 










12 comments:

raid data recovery linux said...

This is great contribution on this awesome screen subject. I must say thanks for the giving time to discuss and present it here for us.

Anonymous said...

C# implement custom themes to .NET application

Richard Majece said...

By the way, here you can read more about paper outline. It was useful for me so I even managed to get a high grade

Assignment Helper said...

Looking for college essay help in this world of inflation? Any success? No? Why not try to Crowd Content professional writers, offering essays written by experienced and certified native writers at highly affordable rates. Try our services today and be amazed how you ace in your academics.

Anonymous said...

This blog is complete of proper statistics approximately massive statistics engineer. The call for of these engineering in various form of organizations will increase each day. Here you could get all details about their position in business and earnings. It is without a doubt a large statistics idea is quite new. These amazon records blockchain entwickler engineer popular in the global of technology. If every person want to hire those engineer they can see their previous paintings.

zarkazijar said...

It gladdens my heart to have surf through blog, you got so much of good contents, so important and with helpful information shared, really appreciate your effort in putting up this amazing reads, thank you. bmu cut off mark for biology

WORKER said...

I love the valuable info provided in your article. I will bookmark your blog and often check back here. toefl speaking topics with answers pdf

Anonymous said...

Hey guys!!!
gezocht programmeur to a global outsourcing company, we promise to love, care and generously reward such a person

Daily Word said...

This is the best blog anyone can find authentic information on. Your blog has a whole lot of valid information. I will come back for more. Best Gifts For High School Graduates Going To College

Unknown said...

I have decided to be one of your daily visitors after discovering the vital information you published on your blog. Keep posting valid content like this. You might be interested in little known beaches near me 

Anonymous said...

퍼스트카지노 | 우리카지노 | 카지노사이트 쿠폰 - 쿠쿠카지노

Anonymous said...

Hello to all, it’s actually a pleasant for me to pay a quick visit this web page, it comprises important Information. Also read on what is collage dorm party