What is a first chance exception anyway?

This kb article is the best explanation of what a first chance exception is I have ever read (http://support.microsoft.com/kb/105675):

“However, if the application is being debugged, the debugger sees all exceptions before the program does. This is the distinction between the first and second chance exception: the debugger gets the first chance to see the exception (hence the name). If the debugger allows the program execution to continue and does not handle the exception, the program will see the exception as usual. If the program does not handle the exception, the debugger gets a second chance to see the exception. In this latter case, the program normally would crash if the debugger were not present.”

How do I catch first chance exceptions?


In visual studio under the debug > exceptions menu and tick the boxes shown in red:

How do you use a converter on a trigger


You can’t.

You can however use a data trigger and set the binding RelativeSource to Self. Data Triggers allow binding and bindings lets you have converters. Yey

Example:

       <Button Content="I change colour depending on my width for some reason">
            <Button.Triggers>
                <DataTrigger
                    Binding="{Binding
                    Path=Width,
                    RelativeSource={RelativeSource Self},
                    Converter={StaticResource isLessThanConverter},
                    ConverterParameter=50}"
                    Value="True">
                    <Setter Property="Button.Background" Value="Red" />
                DataTrigger>
            Button.Triggers>
        Button>

How to make Tooltips display on disabled WPF controls


This is old news but I find lots of people still don't know about this.

By default, WPF does not show tooltips on controls that have been disabled. To fix this, set the attached property ToolTipService.ShowOnDisabled.

E.g.
<Button Content="Example Button" 
 ToolTipService.ShowOnDisabled="True"  
 ToolTip="I’m visible even when the control is not enabled"/>

Extracting data from a resx file using c#

.Net has a built-in reader (ResxResourceReader) and writer (ResxResourceWriter) which makes it really easy to read a resx file. The classes can be found in the System.Resources namespace under the System.Windows.Forms assembly (in System.Windows.Forms.dll)  

Basic Reader Example:

 
            var rsxr = new ResXResourceReader("demo.resx");

            foreach (DictionaryEntry d in rsxr)
            {
                Console.WriteLine(pathRelativeToStartingPath + ",\"" + d.Key + "\",\"" + d.Value + "\"");
            }

            rsxr.Close();           
         
            
Basic Writer Example:


            string txt = "One smart fellow, he felt smart";
            string txt_fr = "Un garçon intelligent, il se sentait à puce";


            ResXResourceWriter rsxw = new ResXResourceWriter("demo.resx");
            rsxw.AddResource("MyText", txt);
            rsxw.Close();

            ResXResourceWriter rsxw_fr = new ResXResourceWriter("demo.fr-FR.resx");
            rsxw_fr.AddResource("MyText", txt_fr);
            rsxw_fr.Close();    
  
            
Of course if you are putting images or media into your resx file you'll additional code to handle the different types that value can be.

A quick way to delete all the bin and obj folders in your solution

Something like this is a cinch with Powershell

1. open the powershell command prompt
2. enter the command get-childitem -path "C:\sourceCode\MySolution" -recurse -include obj,bin | remove-item -force -recurse

Handy for ensuring your solution will build from scratch with no unreferenced/circular referenced assemblies or for deleting dlls that have become locked which sometimes happens. Same principle could easily be applied to the asp.net cache.

How to obtain access to nested types through XAML

Answer: "+" indicates nested type in XAML

For example I want to access the View member on the Shipping type which is nested inside the Setup type e.g.

public struct Setup
{
  public struct Shipping
  {
    public const string View = "This is the value I want to access";
  }
}

The syntax would be CommandParameter="{x:Static demoAlias:Setup+Shipping.View}"

Edits were made which cannot be compiled. Execution cannot continue until the compile errors are fixed

I was getting a really annoying error today, visual studio (vs2008) kept claiming "Edits were made which cannot be compiled. Execution cannot continue until the compile errors are fixed". No didn't change anything though I swore after the 10th time. The code was compiling and running fine - I only got the error when I had a breakpoint set - without the breakpoint everything was hunky-doory.

Reading this connect bug http://connect.microsoft.com/VisualStudio/feedback/details/400456/edit-and-continue-preventing-execution-at-breakpoint-when-no-code-changes-were-made-in-vs2008 it looks like my issue was that I had silverlight and regular wpf/c# code in the same solution (different projects but some shared linked files with conditional #if SILVERLIGHT precompiler directives) and visual studio was somehow confusing the two.

Unloading the silverlight projects fixed the issue. The suggested work around on the connect site was to turn off edit and continue - that worked too.

PropertyChangedEventManager is not marked as serializable

If you ever get the error “PropertyChangedEventManager is not marked as serializable.” This is probably because you are implementing INotifiyPropertyChanged incorrectly.

The solution is to mark your PropertyChangedEventManager as NonSerialized.

#region INotifyPropertyChanged Members

[field: NonSerialized]
public event PropertyChangedEventHandler PropertyChanged;


protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, e);
}
}

#endregion

Team Foundation Error: You are not logged into Windows Live Messenger

I recently started getting an error dialog popping up saying "Team Foundation Error: You are not logged into Windows Live Messenger" when opening VS.NET 2008 Team Explorer.

Apparently windows live can interface with TFS for collaboration purposes but if you're not interested in using that feature here is how to be rid of the annoying popup:

1. Open VS Team Explorer
2. Right-click on 'Team Members' and select 'Personal Settings'
3. Under 'Collaboration' click 'Change'
4. On the opened dialog select 'None' and click 'OK'

Convert from IEnumerable<gobject> to IEnumerable<x>

The easiest way to convert from IEnumerable<object> to a specific class e.g. IEnumerable<DomainObject> is by using the LINQ extension method Cast<T>.

IEnumerable<object> objEnum = GetData();
IEnumerable<DomainObject> domainObjEnum = objEnum.Cast<DomainObject>().ToList();


The same process can be used in reverse.