Posts

Showing posts from 2012

Catching Exceptions at Top Level in WPF

Add a handler for the DispatcherUnhandledException to the App.xaml file: <Application x:Class="Main.App"              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"              DispatcherUnhandledException="Application_DispatcherUnhandledException"              StartupUri="MainApplicationWindow.xaml">     <Application.Resources>             </Application.Resources> </Application> Add the handler for it to the App.xaml.cs file: private void Application_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)         {             // Do what you want here. The exception can be accessed via the event args e.         }

Debugging Unit Tests

To enable debugging and stepping through unit test code when using NUnit: In the project containing the unit tests add a reference to nunit-console-runner.dll. Add a file to the project called NUnitConsoleRunner, for example. using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Utilities_Tests {     public class NUnitConsoleRunner     {         [STAThread]         public static void Main(string[] args)         {             NUnit.ConsoleRunner.Runner.Main(args);         }     } } Under the properties for the project:    - Application - set the output type to Windows Application.   - Debug - set the command line arguments to the name of the executable produced, e.g. Utilities_Tests.exe, and set the working directory to where it lives. Set the project to be the start up project. When you hit F5 or the green arrow the unit tests will run and will stop at any break points.

Visual Studio 2010 Intellisense not working correctly

My Visual Studio 2010 intellisense was giving a drop down of options as I typed, but the selected option was not highlighted and I had to hit space to highlight it. Turns out it had got into 'low impact' mode. Clicking Ctl + Alt + Spacebar toggles between low and high impact intellisense modes.

Creating random number between two values

If we have a high value and a low value and want to create a random number between the two: randomNumber = (high - low) * random.NextDouble() + low;

Checking we're on the UI thread in WPF

Use the Dispatcher.CheckAccess() method. Note; it doesn't appear in intellisense. e.g. if(!Dispatcher.CheckAccess()) {        // Invoke. } else {        // Normal code }

Custom WPF Events

This shows an example of adding a custom WPF event to a control, called ImagingControl. public delegate void CameraConnectedEventHandler(object sender, CameraConnectedRoutedEventArgs e); public static readonly RoutedEvent CameraConnectedEvent = System.Windows.EventManager.RegisterRoutedEvent( "CameraConnected", RoutingStrategy.Bubble, typeof(CameraConnectedEventHandler), typeof(ImagingControl)); public event CameraConnectedEventHandler CameraConnected { add { this.AddHandler(CameraConnectedEvent, value); } remove { this.RemoveHandler(CameraConnectedEvent, value); } } public class CameraConnectedRoutedEventArgs : RoutedEventArgs { public CameraConnectedRoutedEventArgs(RoutedEvent routedEvent, bool bIsConnected) { RoutedEvent = routedEvent; IsConnected = bIsConnected; } public bool IsConnected { get; set; } } The event is raised in this lin
I started getting warning messages like ' Could not find schema information for the element 'supportedRuntime'' after adding an app.config file to my project. It seems that Microsoft have not completely specified the 'startup' element in the relevant .xsd file. To fix this: Navigate to the file ' C:\Program Files\Microsoft Visual Studio 10.0\Xml\Schemas\DotNetConfig.xsd'. If the startup element looks like this: <: xs:element name="startup" vs:help="configuration/startup"> Change it to: < xs:element name="startup" vs:help="configuration/startup"> < xs:complexType> < xs:choice minOccurs="1" maxOccurs="1"> < xs:element name="requiredRuntime" vs:help="configuration/startup/requiredRun

Using Cognex 7.2 with .NET 4

Cognex 7.2 can be used with .NET 4, but it needs a bit of setting up… Right click on the project in Solution Explorer > Add > New Item. Select General > Application Configuration File. On the project properties set the Target framework to ‘.NET Framework 4’. Note – not the client profile version. It should warn you about reloading the project. Click OK and this will update the app.config file with the .NET version. To the app.config file add the attribute: useLegacyV2RuntimeActivationPolicy = " true ". It should now look like this: xml version = " 1.0 " ?> < configuration > < startup useLegacyV2RuntimeActivationPolicy = " true " >< supportedRuntime version = " v4.0 " sku = " .NETFramework,Version=v4.0 " /> startup > configuration > It should now compile OK.

Bitmap manipulation

Explanation of bitmap format and manipulation of pixels. http://www.bobpowell.net/lockingbits.htm