December 19, 2014

Visual Studio Project Type GUIDs Cheet Sheet

Every VS projects have a project type and some of the projects contains more than one type. Each project type is identified by a unique GUID. This GUID is present in the project file for class libraries project while projects like Web Site, GUID is present in the solution file.

Developer require this GUID in order to change the existing project type like suppose you want to change the type from Class Library to Web Application then you need to change its Project Type GUID in order to do so.

The purpose of this post is to list down all the possible Project Type GUIDs which will help developers around the world to convert their projects to desireable choice.

List of Project Type GUIDs are:

1.By Description

  • ASP.NET MVC 1.0 {603C0E0B-DB56-11DC-BE95-000D561079B0}
  • ASP.NET MVC 2.0 {F85E285D-A4E0-4152-9332-AB1D724D3325}
  • ASP.NET MVC 3.0 {E53F8FEA-EAE0-44A6-8774-FFD645390401}
  • ASP.NET MVC 4.0 {E3E379DF-F4C6-4180-9B81-6769533ABE47}
  • C#         {FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}
  • C++         {8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}
  • Database {A9ACE9BB-CECE-4E62-9AA4-C7E7C5BD2124}
  • F#         {F2A71F9B-5D33-465A-A702-920D77279786}
  • J#         {E6FDF86B-F3D1-11D4-8576-0002A516ECE8}
  • Deployment Setup {978C614F-708E-4E1A-B201-565925725DBA}
  • Database (other project types) {4F174C21-8C12-11D0-8340-0000F80270F8}
  • Deployment Cab {3EA9E505-35AC-4774-B492-AD1749C4943A}
  • Deployment Merge Module {06A35CCD-C46D-44D5-987B-CF40FF872267}
  • Deployment Smart Device Cab {AB322303-2255-48EF-A496-5904EB18DA55}
  • Distributed System {F135691A-BF7E-435D-8960-F99683D2D49C}
  • Dynamics 2012 AX C# in AOT {BF6F8E12-879D-49E7-ADF0-5503146B24B8}
  • Legacy (2003) Smart Device (C#) {20D4826A-C6FA-45DB-90F4-C717570B9F32}
  • Legacy (2003) Smart Device (VB.NET) {CB4CE8C6-1BDB-4DC7-A4D3-65A1999772F8}
  • Model-View-Controller v2 (MVC2) {F85E285D-A4E0-4152-9332-AB1D724D3325}
  • Model-View-Controller v3 (MVC3) {E53F8FEA-EAE0-44A6-8774-FFD645390401}
  • Model-View-Controller v4 (MVC4) {E3E379DF-F4C6-4180-9B81-6769533ABE47}
  • Mono for Android {EFBA0AD7-5A72-4C68-AF49-83D382785DCF}
  • MonoTouch {6BC8ED88-2882-458C-8E55-DFD12B67127B}
  • MonoTouch Binding {F5B4F3BC-B597-4E2B-B552-EF5D8A32436F}
  • Portable Class Library {786C830F-07A1-408B-BD7F-6EE04809D6DB}
  • SharePoint (C#) {593B0543-81F6-4436-BA1E-4747859CAAE2}
  • SharePoint (VB.NET) {EC05E597-79D4-47f3-ADA0-324C4F7C7484}
  • SharePoint Workflow {F8810EC1-6754-47FC-A15F-DFABD2E3FA90}
  • Silverlight {A1591282-1198-4647-A2B1-27E5FF5F6F3B}
  • Smart Device (C#) {4D628B5B-2FBC-4AA6-8C16-197242AEB884}
  • Smart Device (VB.NET) {68B1623D-7FB9-47D8-8664-7ECEA3297D4F}
  • Solution Folder {2150E333-8FDC-42A3-9474-1A3956D46DE8}
  • Test {3AC096D0-A1C2-E12C-1390-A8335801FDAB}
  • VB.NET {F184B08F-C81C-45F6-A57F-5ABD9991F28F}
  • Visual Database Tools {C252FEB5-A946-4202-B1D4-9916A0590387}
  • Visual Studio Tools for Applications (VSTA) {A860303F-1F3F-4691-B57E-529FC101A107}
  • Visual Studio Tools for Office (VSTO) {BAA0C2D2-18E2-41B9-852F-F413020CAA33}
  • Web Application {349C5851-65DF-11DA-9384-00065B846F21}
  • Web Site {E24C65DC-7377-472B-9ABA-BC803B73C61A}
  • Windows (C#) {FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}
  • Windows (VB.NET) {F184B08F-C81C-45F6-A57F-5ABD9991F28F}
  • Windows (Visual C++) {8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}
  • Windows Communication Foundation (WCF) {3D9AD99F-2412-4246-B90B-4EAA41C64699}
  • Windows Phone 8/8.1 Blank/Hub/Webview App {76F1466A-8B6D-4E39-A767-685A06062A39}
  • Windows Phone 8/8.1 App (C#) {C089C8C0-30E0-4E22-80C0-CE093F111A43}
  • Windows Phone 8/8.1 App (VB.NET) {DB03555F-0C8B-43BE-9FF9-57896B3C5E56}
  • Windows Presentation Foundation (WPF) {60DC8134-EBA5-43B8-BCC9-BB4BC16C2548}
  • Windows Store (Metro) Apps & Components {BC8A1FFA-BEE3-4634-8014-F334798102B3}
  • Workflow (C#) {14822709-B5A1-4724-98CA-57A101D1B079}
  • Workflow (VB.NET) {D59BE175-2ED0-4C54-BE3D-CDAA9F3214C8}
  • Workflow Foundation {32F31D43-81CC-4C15-9DE6-3FC5453562B6}
  • Xamarin.Android {EFBA0AD7-5A72-4C68-AF49-83D382785DCF}
  • Xamarin.iOS {6BC8ED88-2882-458C-8E55-DFD12B67127B}
  • XNA (Windows) {6D335F3A-9D43-41b4-9D22-F6F17C4BE596}
  • XNA (XBox) {2DF5C3F4-5A5F-47a9-8E94-23B4456F55E2}
  • XNA (Zune) {D399B71A-8929-442a-A9AC-8BEC78BB2433}

2. By GUID

  • Deployment Merge Module {06A35CCD-C46D-44D5-987B-CF40FF872267}
  • Workflow (C#) {14822709-B5A1-4724-98CA-57A101D1B079}
  • Legacy (2003) Smart Device (C#) {20D4826A-C6FA-45DB-90F4-C717570B9F32}
  • Solution Folder {2150E333-8FDC-42A3-9474-1A3956D46DE8}
  • XNA (XBox) {2DF5C3F4-5A5F-47a9-8E94-23B4456F55E2}
  • Workflow Foundation {32F31D43-81CC-4C15-9DE6-3FC5453562B6}
  • Web Application {349C5851-65DF-11DA-9384-00065B846F21}
  • Test {3AC096D0-A1C2-E12C-1390-A8335801FDAB}
  • Windows Communication Foundation (WCF) {3D9AD99F-2412-4246-B90B-4EAA41C64699}
  • Deployment Cab {3EA9E505-35AC-4774-B492-AD1749C4943A}
  • Smart Device (C#) {4D628B5B-2FBC-4AA6-8C16-197242AEB884}
  • Database (other project types) {4F174C21-8C12-11D0-8340-0000F80270F8}
  • SharePoint (C#) {593B0543-81F6-4436-BA1E-4747859CAAE2}
  • ASP.NET MVC 1.0 {603C0E0B-DB56-11DC-BE95-000D561079B0}
  • Windows Presentation Foundation (WPF) {60DC8134-EBA5-43B8-BCC9-BB4BC16C2548}
  • Smart Device (VB.NET) {68B1623D-7FB9-47D8-8664-7ECEA3297D4F}
  • MonoTouch {6BC8ED88-2882-458C-8E55-DFD12B67127B}
  • XNA (Windows) {6D335F3A-9D43-41b4-9D22-F6F17C4BE596}
  • Windows Phone 8/8.1 Blank/Hub/Webview App {76F1466A-8B6D-4E39-A767-685A06062A39}
  • Portable Class Library {786C830F-07A1-408B-BD7F-6EE04809D6DB}
  • C++ {8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}
  • Deployment Setup {978C614F-708E-4E1A-B201-565925725DBA}
  • Silverlight {A1591282-1198-4647-A2B1-27E5FF5F6F3B}
  • Visual Studio Tools for Applications (VSTA) {A860303F-1F3F-4691-B57E-529FC101A107}
  • Database {A9ACE9BB-CECE-4E62-9AA4-C7E7C5BD2124}
  • Deployment Smart Device Cab {AB322303-2255-48EF-A496-5904EB18DA55}
  • Visual Studio Tools for Office (VSTO) {BAA0C2D2-18E2-41B9-852F-F413020CAA33}
  • Windows Store Apps (Metro Apps) {BC8A1FFA-BEE3-4634-8014-F334798102B3}
  • C# in Dynamics 2012 AX AOT {BF6F8E12-879D-49E7-ADF0-5503146B24B8}
  • Windows Phone 8/8.1 App (C#)
  •   {C089C8C0-30E0-4E22-80C0-CE093F111A43}
  • Visual Database Tools {C252FEB5-A946-4202-B1D4-9916A0590387}
  • Legacy (2003) Smart Device (VB.NET) {CB4CE8C6-1BDB-4DC7-A4D3-65A1999772F8}
  • XNA (Zune) {D399B71A-8929-442a-A9AC-8BEC78BB2433}
  • Workflow (VB.NET) {D59BE175-2ED0-4C54-BE3D-CDAA9F3214C8}
  • Windows Phone 8/8.1 App (VB.NET)
  •   {DB03555F-0C8B-43BE-9FF9-57896B3C5E56}
  • Web Site {E24C65DC-7377-472B-9ABA-BC803B73C61A}
  • ASP.NET MVC 4.0 {E3E379DF-F4C6-4180-9B81-6769533ABE47}
  • ASP.NET MVC 3.0 {E53F8FEA-EAE0-44A6-8774-FFD645390401}
  • J# {E6FDF86B-F3D1-11D4-8576-0002A516ECE8}
  • SharePoint (VB.NET) {EC05E597-79D4-47f3-ADA0-324C4F7C7484}
  • Xamarin.Android / Mono for Android {EFBA0AD7-5A72-4C68-AF49-83D382785DCF}
  • Distributed System {F135691A-BF7E-435D-8960-F99683D2D49C}
  • VB.NET {F184B08F-C81C-45F6-A57F-5ABD9991F28F}
  • F# {F2A71F9B-5D33-465A-A702-920D77279786}
  • MonoTouch Binding {F5B4F3BC-B597-4E2B-B552-EF5D8A32436F}
  • ASP.NET MVC 2.0 {F85E285D-A4E0-4152-9332-AB1D724D3325}
  • SharePoint Workflow {F8810EC1-6754-47FC-A15F-DFABD2E3FA90}
  • C# {FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}

December 18, 2014

Typical Traps In WPF

In WPF, few traps are present where developers required lots of time to resolve issues. In this post I'm going to list down the common errors arise in development and their solution so that developer would resolve them quickly.

1. Layout Section


Q1. Scrollbar is not active or visible
Ans. If your control is within a vertical stackpanel, it gives the control infinite height to layout. Consider replacing the stackpanel by a dockpanel.

Q2. I created a data template and set HorizontalAlignment to Stretch but the item is not stretched
Ans. Set the HorizontalContentAlignment on the list to Stretch.

2. DataBinding


Q1. I changed a value, but the binding is not reflecting my changes
Ans. Check the following conditions:
         a. Check the output window in VisualStudio, if there are any binding errors.
         b. Does your data support INotifyPropertyChanged?
         c. Just firing a PropertyChanged event without changing the data does not work. Because the binding checks if oldvalue != newvalue

3. Performance


Q1. My list of items takes too long to render
Ans. Your list is not virtualized. This means, all items will be generated, even if they are not visible. To avoid this check the following points:
         a. ScrollViewer.CanContentScrol must be set to False
         b. Grouping must be disabled
         c. You replaced the ItemsPanel by one that does not support virtualization
         d. You are using a too complex data template.

Q2. Animations cause a high CPU load
Ans. WPF cannot use hardware acceleration and does software rendering. This can be because of the following points:
         a. You have set AllowTransparency to True on your window.
         b. You are using legacy BitmapEffects instead of fast pixel shaders (Effects).
         c. Your graphics adapter or driver does not support DirectX

4. Custom Controls

Q1. I created a custom control, but the template it not showing
Ans. Check the following conditions:
         a. Check if you have overriden the metadata of the DefaultStyleKeyProperty and set it to your type.
         b. Check if your template is surrounded by a style and both have the right TargetType
         c. Check if the resource dictionary that contains the default style is loaded

Q2. I use {TemplateBinding} in my ControlTemplate, but is not working
Ans. Check the following conditions:
         a. In most cases you have to replace {TemplateBinding Property} by {Binding Property RelativeSource={RelativeSource TemplatedParent}}
         b. You can only use TemplateBinding within the content of your control template. It will not work anywhere else!
         c. If you want to access a parent property in the trigger, you have to use a normal binding, with relative source Self.
         d. TemplateBinding works only within the VisualTree of the template. You cannot use it on items that are only in the logical tree. Neighter on Freezables or to do two-way binding.

December 10, 2014

Extremely Helpful Extension Methods in C#

Developers consumed lots of time before C# 3.0 to write methods but after C# 3.0, Microsoft introduces the terminology called "Extension Method" that is saving lots of developer time. Now they have to called these methods to done the matters quickly.

As per Microsoft's correspondence, Extension Methods allow you to create new functionality for existing types without modifying the actual type itself. Extension methods should contains these attributes: 

a. The class has to be static.
b. The method has to be static.
c. The method's first parameter in the signature must have the "this" declared.

You can create your own extension methods which will helps you throughout project and you can easily return the desire result by passing the method name. 

Under below are list of powerful extension methods that will save your lots of development time.

As per rule (a), class has to be static so create static class and then place all the custom extension methods there; I created the custom Extension Method class as:


public static class CustomExtensionMethods
{

}

As per rule (b, c), following custom extension method can reduce lots of your development time efforts.

1. Calculate Age:
    To calculate how old someone is:


public static int CalculateAge(this DateTime dateTime)
{
    var age = DateTime.Now.Year - dateTime.Year;

    if (DateTime.Now < dateTime.AddYears(age))
    {
        age--;
    }

    return age;
}

2. Has<T>()/Is<T>()/Add<T>()/Remove<T>()
   When you required an enumerated type are like flags instead of full items then use the following method:


public static bool Has<T>(this System.Enum type, T value)
{
    try
    {
        return (((int)(object)type & (int)(object)value) == (int)(object)value);
    }
    catch
    {
        return false;
    }
}

public static bool Is<T>(this System.Enum type, T value)
{
    try
    {
        return (int)(object)type == (int)(object)value;
    }
    catch
    {
        return false;
    }
}

public static T Add<T>(this System.Enum type, T value)
{
    try
    {
        return (T)(object)(((int)(object)type | (int)(object)value));
    }
    catch (Exception ex)
    {
        throw new ArgumentException(
            string.Format("Could not append value from enumerated type '{0}'.",
                typeof(T).Name), ex);
    }
}

public static T Remove<T>(this System.Enum type, T value)
{
    try
    {
        return (T)(object)(((int)(object)type & ~(int)(object)value));
    }
    catch (Exception ex)
    {
        throw new ArgumentException(
            string.Format("Could not remove value from enumerated type '{0}'.",
                typeof(T).Name), ex);
    }
}

3. ToReadableTime()
   This method is useful when you required to display a message like 'One second ago'.


public static string ToReadableTime(this DateTime value)
{
    var timeSpan = new TimeSpan(DateTime.UtcNow.Ticks - value.Ticks);
    double totalSecond = timeSpan.TotalSeconds;

    if (totalSecond < 60)
    {
        return timeSpan.Seconds == 1 ? "one second ago" : timeSpan.Seconds + " seconds ago";
    }

    if (totalSecond < 120)
    {
        return "a minute ago";
    }

    if (totalSecond < 2700) // 45 * 60
    {
        return timeSpan.Minutes + " minutes ago";
    }

    if (totalSecond < 5400) // 90 * 60
    {
        return "an hour ago";
    }

    if (totalSecond < 86400) // 24 * 60 * 60
    {
        return timeSpan.Hours + " hours ago";
    }

    if (totalSecond < 172800) // 48 * 60 * 60
    {
        return "yesterday";
    }

    if (totalSecond < 2592000) // 30 * 24 * 60 * 60
    {
        return timeSpan.Days + " days ago";
    }

    if (totalSecond < 31104000) // 12 * 30 * 24 * 60 * 60
    {
        int months = Convert.ToInt32(Math.Floor((double)timeSpan.Days / 30));
        return months <= 1 ? "one month ago" : months + " months ago";
    }

    var years = Convert.ToInt32(Math.Floor((double)timeSpan.Days / 365));
    return years <= 1 ? "one year ago" : years + " years ago";
}

4. WorkingDay()/IsWeekend()/NextWorkday()
   When you want to display like 'Today is working day, weekend or determine the next working day' then:


public static bool WorkingDay(this DateTime date)
{
    return date.DayOfWeek != DayOfWeek.Saturday && date.DayOfWeek != DayOfWeek.Sunday;
}

public static bool IsWeekend(this DateTime date)
{
    return date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek == DayOfWeek.Sunday;
}

public static DateTime NextWorkday(this DateTime date)
{
    var nextDay = date;

    while (!nextDay.WorkingDay())
    {
        nextDay = nextDay.AddDays(1);
    }

    return nextDay;
}

5. Next()
   Determine the Next date by passing in a DayOfWeek (i.e. From this date, when is the next Tuesday?)


public static DateTime Next(this DateTime current, DayOfWeek dayOfWeek) 
{ 
    int offsetDays = dayOfWeek - current.DayOfWeek; 

    if (offsetDays <= 0) 
    { 
       offsetDays += 7; 
    } 

    DateTime result = current.AddDays(offsetDays); 
    return result; 
}

6. str.ToStream()/stream.ToString()/CopyTo()
   If you want to take a large string and convert it to a Stream or vice-versa then use these:


public static Stream ToStream(this string str)
{
    byte[] byteArray = Encoding.UTF8.GetBytes(str);
    return new MemoryStream(byteArray);
}

public static string ToString(this Stream stream)
{
    var reader = new StreamReader(stream);
    return reader.ReadToEnd();
}

public static void CopyTo(this Stream fromStream, Stream toStream)
{
    if (fromStream == null)
    {
        throw new ArgumentNullException("fromStream");
    }

    if (toStream == null)
    {
        throw new ArgumentNullException("toStream");
    }
    
    var bytes = new byte[8092];
    int dataRead;

    while ((dataRead = fromStream.Read(bytes, 0, bytes.Length)) > 0)
    {
        toStream.Write(bytes, 0, dataRead);
    }
}

7. Between()   
   Check to see if a date is between two dates. 


public static bool Between(this DateTime dateTime, DateTime rangeStart, DateTime rangeEnd)
{
    return dateTime.Ticks >= rangeStart.Ticks && dateTime.Ticks <= rangeEnd.Ticks;
}

8.RemoveLast() / RemoveLastCharacter() / RemoveFirst() / RemoveFirstCharacter()
   These tricky method save lot of time in the heat of string manipulation. 


public static string RemoveLastCharacter(this String str)
{
    return str.Substring(0, str.Length - 1);
}

public static string RemoveLast(this String str, int number)
{
    return instr.Substring(0, instr.Length - number);
}

public static string RemoveFirstCharacter(this String instr)
{
    return str.Substring(1);
}

public static string RemoveFirst(this String str, int number)
{
    return str.Substring(number);
}

9. ToFileSize
   If you want to read the file of size then this method is easier to read the file size.


public static string ToFileSize(this long size)
{
    if (size < 1024) { return (size).ToString("F0") + " bytes"; }
    if (size < Math.Pow(1024, 2)) { return (size / 1024).ToString("F0") + "KB"; }
    if (size < Math.Pow(1024, 3)) { return (size / Math.Pow(1024, 2)).ToString("F0") + "MB"; }
    if (size < Math.Pow(1024, 4)) { return (size / Math.Pow(1024, 3)).ToString("F0") + "GB"; }
    if (size < Math.Pow(1024, 5)) { return (size / Math.Pow(1024, 4)).ToString("F0") + "TB"; }
    if (size < Math.Pow(1024, 6)) { return (size / Math.Pow(1024, 5)).ToString("F0") + "PB"; }

    return (size / Math.Pow(1024, 6)).ToString("F0") + "EB";
}

10. ToXmlDocument()/ToXDocument()
    This extension method is very handy and will save you a load of time when you need to convert an XmlDocument into an XDocument and vice-versa:


public static XmlDocument ToXmlDocument(this XDocument xDocument)
{
    var xmlDocument = new XmlDocument();

    using (var xmlReader = xDocument.CreateReader())
    {
        xmlDocument.Load(xmlReader);
    }

    return xmlDocument;
}

public static XDocument ToXDocument(this XmlDocument xmlDocument)
{
    using (var nodeReader = new XmlNodeReader(xmlDocument))
    {
        nodeReader.MoveToContent();
        return XDocument.Load(nodeReader);
    }
}

public static XmlDocument ToXmlDocument(this XElement xElement)
{
    var stringBuilder = new StringBuilder();
    var xmlWriterSettings = new XmlWriterSettings {OmitXmlDeclaration = true, Indent = false};

    using (var xmlWriter = XmlWriter.Create(stringBuilder, xmlWriterSettings))
    {
        xElement.WriteTo(xmlWriter);
    }

    var xmlDocument = new XmlDocument();
    xmlDocument.LoadXml(stringBuilder.ToString());
    return xmlDocument;
}

public static Stream ToMemoryStream(this XmlDocument xmlDocument)
{
    var memoryStream = new MemoryStream();
    xmlDocument.Save(memoryStream);
    memoryStream.Flush();//Adjust this if you want read your data 
    memoryStream.Position = 0;
    return memoryStream;
}

Hope these custom extension methods will helps you to reduce your development time and efforts. Stay tuned!