Solving development problems  |  About this blog

Archive for the ‘Tools’ Category

Three essential plugins for Expression Blend

When it comes to vector graphics in Windows applications XAML is the most advanced format to choose from. XAML is great because developer/designer has full control over the graphics. Hidden reference lines, or garbage shapes, or objects can be relatively easily detected and removed from the code as compared to those formats that entirely rely on GUI editor.

A XAML document can be created with plain Notepad but there are great apps like Visual Studio, Expression Blend and Kaxaml that simplify editing. What is missing in those apps is lack of XAML exporting support, like saving .xaml to .png file. Luckily there are plugins to do just that.

xbprint is printing plugin for Expression Blend 3 hosted on CodePlex. Installation of the utility must be perfomed manually by extracting files to the Addins folder in Expression Blend directory. But once the plugin is installed it is easy to use. Set margins, alignment, paper size and click Print.

xbprint screenshot

xbraster is xaml to raster image converting plugin, a close relative to xbprint. Installation and use is similar. xbraster can convert XAML to PNG, JPEG, TIFF, GIF or bitmap.

xbraster screenshot

xbsprite is a sprite image generating plugin. Input xaml files are rasterized and combined in one image with optional CSS and HTML output for use on a web page.

xbsprite screenshot

For advanced and professional use of XAML xbprint, xbraster and xbsprite are must-have plugins.

EDIT: xbprint, xbraster and xbsprite can run directly from desktop – no need to have
Expression Blend installed.

Written by developer

July 5th, 2010 at 10:49 am

Auto-incement assembly build version in Visual Studio 2008

When building a project with Visual Studio an output assembly with embedded version is generated. Version indicates how an application is advancing in development.

Developer can change output assembly version for each project by opening project’s properties (right click, Properties), clicking Assembly Information… button and editing text in fields (see marked area on the image below).

VS2008 window for changing assembly version

VS2008 window for changing assembly version

Alternative is to open Properties/AssemblyInfo.cs or .vb and change AssemblyVersion value in a text editor.

Version can be automatically incremented on each build. To enable counter open Properties/AssemblyInfo.cs and set:

//Change AssemblyVersion's value from 1.0.0.0 to 1.0.* and comment AssemblyFileVersion
[assembly: AssemblyVersion("1.0.*")]
//[assembly: AssemblyFileVersion("1.0.0.0")]

Auto-increment symbol (‘*’) cannot be set in GUI (like screenshot above) but only in code.

Written by developer

January 18th, 2010 at 5:12 pm

Create sceenshot for part of the screen – quickly

If you have Microsoft Office 2007 installed and Microsoft Office OneNote 2007 and if he is active in your SystemTray then just click on Windows key + S and select desirable area of your screen.

You can set options of this program by right clicking on it and choose to save it only in clipboard.

Written by Avivo

December 2nd, 2009 at 9:43 am

Posted in Tools

C# sizeof – Gets a size of a custom class programatically

In .NET, sizeof() works only for basic variable types, like int, double, etc. If you try to use sizeof() for a managed class then compiler reports error:

Cannot take the address of, get the size of, or declare a pointer to a managed type

Alternative to sizeof() would be System.Runtime.InteropServices.Marshal.SizeOf() but this one works with unmanaged types (not a native .NET class).

So far, a decent solution would be to serialize object into binary stream and get the stream size. Source code:

public static long SizeOf(object obj)
{
long size = 0;

try
{
System.IO.MemoryStream stream = new System.IO.MemoryStream();
BinaryFormatter objFormatter = new BinaryFormatter();
objFormatter.Serialize(stream, obj);
size = stream.Length;
}
catch (Exception ex)
{
}

return size;
}

A non-programatic alternative is to use .NET profiler to retrieve object size.

Reference: http://social.msdn.microsoft.com/forums/en-US/clr/thread/b871dee4-6eb5-4dca-be79-b9589a79f5e9/

Written by Avivo

October 5th, 2009 at 2:06 pm

Silverlight 2.0 – Invoke method with Reflection

When deciding whether to create a rich graphical web application with lots of animations either with Silverlight or ASP .NET, Silverlight definitely wins. But when deciding whether put a database in first place then server side scripts (i.e. ASP .NET) have the advantage.

Anyway, if you are accessing database from Silverlight you’ve probably came across WCF or other  web services. Let’s say you have 100 tables and want to use WCF. Each table has Select, Insert, Update and Delete methods generated like InsertUser(User item). Visual Studio 2008 can automatically generated WCF proxy classes in Silverlight which are asynchronous. Suddenly, handling application logic and database becomes complex for development.

Those methods have similarities - Select, Insert, Update and Delete are repeated but for different table names. Idea is to use System.Reflection to simply and minimize development time. What the Reflection classes do is they use run time variables (i.e. string, int, etc.) to dynamically invoke methods according to situation in run time (cannot be determined in design time while developing).

Let’s say you have a method InsertCustomer(Customer item) in WCF and Visual Studio generates InsertCustomerAsync(Customer item) for Silverlight. Application can generate “InsertCustomerAsync” as string on demand. Then a method invoke by string is needed:

using System.Reflection;

//MyWebService is WCF proxy class and Customer is data entity

MethodInfo methodInfo = typeof(MyWebService).GetMethod(“InsertCustomerAsync”, new Type[] { typeof(Customer) });

methodInfo.Invoke(con, BindingFlags.Public | BindingFlags.Instance, null, new object[] { this.item, }, null);

Then a “completed” event needs to be caught:

MethodInfo handlerInfo = typeof(ReflectionDemo<T>).GetMethod(“SaveHandler”, BindingFlags.NonPublic | BindingFlags.Instance);
EventInfo eventInfo = typeof(MyWebService).GetEvent(eventName);
eventInfo.AddEventHandler(con, Delegate.CreateDelegate(eventInfo.EventHandlerType, this, handlerInfo));

Putting all code together:

public class ReflectionDemo<T> where T : new()
{
private T item;

public bool IsUpdate
{
get;
set;
}

public void Save()
{
try
{
if (this.item != null)
{
//Get names
string methodName = (this.IsUpdate ? “Update” : “Insert”) + typeof(T).Name + “Async”; //e.g. UpdateCustomerAsync
string eventName = (this.IsUpdate ? “Update” : “Insert”) + typeof(T).Name + “Completed”; //e.g. UpdateCustomerCompleted
string eventArgsName = (this.IsUpdate ? “Update” : “Insert”) + typeof(T).Name + “CompletedEventArgs”; //e.g. UpdateCustomerCompletedEventArgs

//Create a connection
MyWebService con = new MyWebService();

//Attach an event handler in run time retrieved event (e.g. something like “con.UpdateCustomerCompleted += …” at design time)
MethodInfo handlerInfo = typeof(ReflectionDemo<T>).GetMethod(“SaveHandler”, BindingFlags.NonPublic | BindingFlags.Instance);
EventInfo eventInfo = typeof(MyWebService).GetEvent(eventName);
eventInfo.AddEventHandler(con, Delegate.CreateDelegate(eventInfo.EventHandlerType, this, handlerInfo));

//Invoke method in run time (e.g. something like “con.UpdateCustomerAsync(this.item);” at design time)
MethodInfo methodInfo = typeof(MyWebService).GetMethod(methodName, new Type[] { typeof(T) }); //From overloaded methods get the one with single parameter
methodInfo.Invoke(con, BindingFlags.Public | BindingFlags.Instance, null, new object[] { this.item, }, null); //Invoke the method immediately
}
}
catch (Exception ex)
{
//Exception may be thrown in various situations (e.g. method cannot be found)
//Insert breakpoint here while debugging
}
}

protected void SaveHandler(object sender, EventArgs args)
{

//Handle event logic here
//Sample how to cast to a derived EventArgs with known properties:

Type type = args.GetType();
Exception ex = type.GetProperty(“Error”).GetValue(args, null) as Exception ?? null;
Customer item = (Customer)type.GetProperty(“Customer”).GetValue(args, null);
}

Written by Avivo

July 3rd, 2009 at 2:45 pm