Winform

Fix: Program does not contain a static ‘Main’ method suitable for an entry point

October 18, 2011 CSharp, Visual Studio, Winform, WPF , , , ,

This does not require a detailed post and just requires a quick solution for the problem-

Understanding the problem… Why? When?

When you convert a Winform application into a WPF application –  In a Winform or a Console application, you need a static Main method that acts as an entry point to your executable.  In a WPF application, you do not require any such static class to be present at the compile time.  You need an App.xaml file that appears something like this

[sourcecode language=’xml’]



[/sourcecode]

Here StartupUri represents the first/launch WPF Window or a Page and the App.xaml.cs is very short and simple

[sourcecode language=’csharp’] using System.Windows;

namespace MyWPFApplication
{
///

/// Interaction logic for App.xaml
///

public partial class App : Application
{

}
}
[/sourcecode]

When you migrate a Winform application to WPF application, you can add this file explicitly to your project to get started. But when you would compile, you will find a compilation issue

Program does not contain a static ‘Main’ method suitable for an entry point

So the compiler treats App.xaml just like any other WPF page instead of a special class which we call it as ApplicationDefinition class.

What is the fix?

 

Check the properties of App.xaml. Change the Build Action to ApplicationDefinition and re-compile.

This also means, you can have any XAML file to be your ApplicationDefinition file.  But it is preferable to name it App.xaml since it is industry standard and adds consistency to all your projects.

 

 

 

Using Injectors with CInject

September 28, 2011 CSharp, Open Source, Visual Studio, Winform , , , , ,

If you don’t know what is CInject, I would recommend you to read this article and get the latest version of CInject from CodePlex website.

There are few injectors that are shipped with CInject

  • LogInjector – Allows injecting logging in any .NET assembly/executable even if you do not have code of the assembly/executable.  LogInjector is built on .NET 4.0 Client Profile and uses log4net for logging purpose
  • PerformanceInjector – Allows logging the time required to execute injected method in the target assemblies
  • ObjectValueInjector – Allows getting any property of arguments passed to the injected method

You can get started with directly using them without writing a single line of code.

 

Quick Guide to using CInject

So, to get started all you would require is to

  • Download the latest stable version of CInject and unzip it to, lets say, C:\CInject
  • Locate the assemblies (DLL) / executables (EXE) that need to be injected with along with their configuration files
  • Locate your injectors assembly (DLL) and their configuration files.
  • Open the downloaded CInject application [C\CInject\Cinject.exe]
  • Choosing the Target Assembly
    • If you are targeting a single assembly (to be injected), click on Assembly > Load Assembly (F3).  As soon as you select the assembly, CInject will try to create a tree of classes and methods in the assembly.
    • If you are targeting multiple assemblies (to be injected), click on Assembly > Select Directory (F2).  CInject will browse through DLLs and EXEs in the directory and create a tree of classes and methods in each assembly or executable
  • Choosing the Injector Assembly
    • Auto Load – If you have copied the injector assemblies into CInject directory [here, C:\CInject], they will be automatically loaded in CInject
    • Manually Load – Click on Assembly > Load Injector (F4).  This will load the injectors
  • Select all the classes/methods (in the blue panel) that you would want to inject. Then, select the LogInjector (in the yellow panel)
CInject-DefaultInjectors
  • You can click on ‘Add selected injectors to selected methods in target assembly’.  This will add some entries in the grid (Assembly, Method, Injector).  However, this does not mean that the assembly has been injected.  If by mistake you have selected a wrong injector or a method, you can remove them by selecting it in the grid and clicking ‘Remove selected row’
  • Select Inject > Run (F5) to inject and proceed ahead to inject the target assemblies.
CInject-RunInject
  • You will be prompted when the injection has been completed.  With CInject 1.4, all the required files for each injector will be automatically copied to the folder of target assembly
  • You can now execute the target assembly. This is the new assembly with selected injectors

LogInjector Configuration

You can alter the log configuration by editing LogInject.log4net.xml based on the Apache log4net configuration.

Currently, LogInjector supports DEBUG, INFO and ERROR modes

  • Debug:   Prints additional information such as calls to the destructor of the method, type of parameter to each method invoked
  • Info: Just logs the name method invoked
  • Error: Logs exceptions if LogInjector fails at any point

Apart from the method information, it logs Data-Time, Thread Id, and Assembly.  You can configure these details in the xml file accompanied with the CInject executables.

In case you would want to use any other version of .NET, you would have to rebuild the code in Visual Studio.

PerformanceInjector Configuration

PerformanceInjector also uses log4net for logging the performance of the methods.  The configuration can be mentioned in loginject.log4net.xml file.

PerformanceInjector supports only INFO level of logging.

ObjectValueInjector Configuration

ObjectValueInjector uses ObjectSearch.xml to define which property needs to be retrieved in the arguments to the injected methods.

If the defined property (in configuration) does not exist in the argument, this injector does not throw any exception.  If the defined property (in configuration) exist in the argument, injector will try to get the value of the property.  If property value is NULL, it will log <null>

ObjectValueInjector uses DEBUG level of logging

Mobile platform – the strongest with .NET

April 13, 2011 ASP.NET, CSharp, Silverlight, Visual Studio, Windows Phone, Winform

Microsoft .NET has recently emerged as one of the most mature and strongest platforms for Mobile Computing.    Until sometime back, Windows Phone 6.5 was the only mobile that could see .NET applications host, but with limited functionalities.  Windows Phone 6.5 though very strong on business computing could not leverage on rich UI that was promised by Windows Presentation Foundation and Silverlight Framework.  This limited its reach to people who wanted a classic business phone and/or some odds like me who were passionate about .NET programming.

With integration of technologies stacks such as Microsoft .NET 3.5 Framework, Silverlight, WPF, Prism, Comet, REST Services and WCF with RIA, a new dimension to application programming was invented.   Having a rich UI along with a high-performance application was no more a dream – no more trade-offs!  And to add as a catalyst, Windows Phone 7 was launched in October, 2010.  So that gave a perfect solution in any platform – Winforms, RIA, Mobile or Web!  The definition of  “Developer” and “Designer” became precise and exclusive.  In true sense, a developer could focus on artifacts that could make his application scalable, maintainable, extensible (….. -ables) and better performing; while a designer could focus on the aesthetics and appeal (UED – User Experience Design) of the application.

What strikes more is.. the power of .NET and how it, today, emerges as a technology that has brought the difference.  Not just applications on Microsoft Operating Systems are built on .NET, but applications on varied platforms today preferred to be developed on .NET.

DotGnu and Mono Project have opened doors for developing .NET applications on Unix and Linux; while MonoDroid (now Mono for Android) gives more options to port your applications on Android platform.  With these companies now focusing on supporting .NET applications of open-source operating systems, imagine the market share Microsoft will draw after few years!  Learning Objective – C might appear a task in your ‘wish list’, but if you are good at .NET programming sky is the limit.  iPhone, iPod and Wii applications can be built with MonoTouch. Whether you want to build Xbox games or Windows Phone apps/games, XNA framework is an easy-to-implement framework.

So if you are willing to learn Mobile development, believe me .NET is the most strongest contender in the league. 

And yet, we are still to reach the crest – .NET getting used in almost every other platform we touch, use, imagine and work in daily lives.  Many more versions of mature .NET with many new features would make the journey a worth experience!

Certificate Signing in ClickOnce

January 11, 2010 CSharp, Visual Studio, Winform , ,

Why do we need to sign the ClickOnce Deployment using a certificate?

Signing the deployment is just another way to ensure that the local version agrees with the version on server.  This prevents from installing any unauthorized stuff – hence helps detect tampering by others.

Signing with a test certificate, will prompt “unknown publisher” when it installs; Signing with a Verisign Certificate will show your company name when it installs.

The purpose of a certificate is just to ensure that you are who you say you are. Signing with a test certificate can be used when you don’t want to purchase the real deal.

WPF vs WPF XBAP vs Silverlight

September 7, 2009 Silverlight, Winform

Many have answered this question and many have asked me the same… So after research from various sites including http://silverlight.net and http://blogs.msdn.com/ this is what I conclude:

 

  WPF  WPF XBAP Silverlight Ajax.Net
Client –
OS
Windows XP SP2 & Vista Windows XP SP2 & Vista Windows XP SP2, Vista, Mac, Linux Any OS
Client –
.NET FX
.NET 3.0 .NET 3.0 Silverlight Plugin Not required
Client – Browser 

NA Internet Explorer 6+ FireFox, Mac Safari, Internet Explorer Any Web Browser
Deployment 

Downloadable Installer or ClickOnce Runs in Internet Explorer secure sandbox One-time install of Silverlight plug-in Web Page
When to use Programs that need access to Windows desktop files. Intranet applications for Windows-oriented companies. Rich Internet Applications for public-facing web sites General-purpose public-facing web sites 

 

Disadvantages Typically works on MS-OS only Mostly internal sites for companies that use MS-OS. Works on any browser but has limited functionalities compared to WPF, WPF XBAP Lacks Richness in UI
         

Guess this will be helpful in analysing situations where to use what!

Winform Tip#10: Getting File Properties

July 25, 2009 CSharp, Visual Studio, Winform , , ,

Instantiate a FileInfo object with the full path as constructor arg. Then simply call FileInfo.Extension and you will get just the extension of the file.

 

FileInfo finfo = new FileInfo(strFileName);
Console.WriteLine(finfo.Extension);

Similarly, you can get more properties

Winform Tip#9: ColorPicker

July 24, 2009 CSharp, Visual Studio, Winform , , ,

Choose a textbox over which we will test this experiment:
[sourcecode language=’csharp’] ColorDialog colorDialog1 = new ColorDialog();

//fontDialog1.ShowColor = true;

if(colorDialog1.ShowDialog() != DialogResult.Cancel )
{
txtMessage.ForeColor = colorDialog1.Color;
}
txtMessage.Text = “Test for ColorPicker”;

[/sourcecode]

Another tip in this: Similar is for FontDialog. Try using it 🙂

Winform Tip#8: Clearing cache Webbrowser

July 20, 2009 CSharp, Winform , , ,

Check out: http://support.microsoft.com/support/kb/articles/q326/2/01.asp

It is indeed a good read.

Winform Tip#7:Browser within Winform

July 5, 2009 CSharp, Winform , , ,

WebBrowser control can be used inside a form to display HTML contents and listen to events such as NavigateComplete or BeforeNavigate

Use of ActiveX wrappers over old DLLs is not prefered.

Winform tip#6:DataGrid vs DataGridView

July 1, 2009 CSharp, Winform , , ,

DataGridView is an enhanced version of DataGrid.  Some of the features lacking in DataGrid are:

  1. Multiple column types
  2. Multiple ways to display data
  3. Multiple ways to customize the display of data
  4. Multiple options for changing cell, row, column, and header appearance and behavior

Follow on Feedly