-
Creating a single executable from multiple .NET assemblies
Problem You have a .NET application that uses multiple assemblies but you want it as a single executable. Solution Use ILMerge to merge the assemblies into one executable. Solution (with WPF support) Embed the required assemblies as embedded resources in your project and extend the assembly resolving to check the embedded resources (see second link…
-
WPF Text Rendering Optimization (Text of WPF application is blurry or pixelated)
Situation You’re building a WPF application. Problem Some text is rendered blurry or pixelated. Solution Modify the text render options corresponding to your needs. Properties you can set on any UIElement: TextOptions.TextFormattingMode TextOptions.TextRenderingMode TextOptions.TextHintingMode A settings that has worked for me very well is TextOptions.TextFormattingMode=”Display”. These settings are inherited by all sub-elements so it’s okay…
-
Mocking a generic method (that uses TResult) with Microsoft Moles
Situation There is a generic method that uses TResult which you want to mock using Microsoft Moles. Problem You can’t just assign a delegate like on a non-generic method. Solution Create a delegate that behaves like the mocked method should. Define the concrete expected parameter types. Instantiate a new MolesDelegates.Func with the corresponding type-parameters and…
-
Disable anti-aliasing in WPF
Situation You’re creating some kind of control that needs to be sharply visible. Problem WPF uses anti-aliasing on default so the control is rendered “soft”. Solution Deactivate anti-aliasing for the whole WPF application, the control or the specific part that needs to be sharp by setting the EdgeModeProperty to aliased. Sample /// <summary> /// Interaction…
-
“InitializeComponent() does not exist in the current context” after copying XAML files
Situation You copied XAML files in Visual Studio. Problem Visual Studio now can’t compile anymore, reporting that the InitializeComponent() method in the code-behind of your XAML files does not exist in the current context. Solution The build action of your XAML files has probably changed to “Resource”. Change it (back) to “Page”. Sources http://stackoverflow.com/questions/954861/why-can-visual-studio-not-find-my-wpf-initializecomponent-method
-
Localizing WPF controls with ResX files
Situation You want to localize a WPF control. Problem Books on WPF recommend to use Microsofts LocBaml.exe, which is actually just a sample that is, of course, not integrated anywhere. Solution Localize the WPF control using ResX files. create the ResX files set the scope of the ResX files to public add the Properties namespace…
-
Using a CLR-namespace of another assembly in XAML
Situation You want to use a CLR namespace of another assembly in your XAML. Problem A simple xmlns:custom=”clr-namespace:MyOtherAssembly.MyDesiredNamespace” won’t do the trick. Solution Identify the assembly within the namespace string. Example xmlns:custom=”clr-namespace:MyDesiredNamespace;assembly=MyOtherAssembly” Sources http://msdn.microsoft.com/en-us/library/ms747086.aspx
-
Setting the culture on WPF bindings
Situation You work on WPF bindings, for example a binding to a DateTime object. Problem The DateTime object is displayed in en-US culture format. Solution Set the current culture on the root element, which is most probably the outer most Grid element, of your XAML. For example: Set the culture in the corresponding ViewModel constructor…
-
“QTAgent32.exe reagiert nicht mehr” appears when running unit-tests
Situation You are writing unit-tests and maybe are refactoring the implementation along the way. Problem “All of a sudden” QTAgent32.exe crashes when you run the tests. Solution A unit-test (or changed implementation) most probably caused a stack overflow. Attach the debugger to QTAgent32.exe (use the “Debug” button on the crash-window), so you can see the…
-
WPF localization
Sources ResX: http://compositeextensions.codeplex.com/discussions/52910?ProjectName=compositeextensions MarkupExtension: http://www.wpftutorial.net/LocalizeMarkupExtension.html
-
Get PublicKeyToken of an assembly reference from within Visual Studio
Situation You need the public key token of an assembly reference. E.g. to set the InternalsVisibleTo() attribute for a Unit-Test. Problem There is no standard mechanism embedded in Visual Studio (2010). Solution Create a “external tool” entry that calls sn.exe and redirects the output to the output window. Dialog: [Tools] – [External tools…] – [Add]…
-
XmlSerializer throws FileNotFoundException when debugging
Situation You are using a XmlSerializer. Problem The constructor XmlSerializer(Type) throws a FileNotFoundException when debugging. Solution Pre-Compile the Serializer. See http://msdn.microsoft.com/en-us/library/ee704594.aspx for details. Alternative Configure Visual Studio not to break on System.IO.FileNotFoundException thrown by the CLR through opening the dialog [Debug] – [Exceptions] and unchecking the checkbox for “Common Language Runtime Exceptions” – “System.IO” – “System.IO.FileNotFoundException” Sources…
-
The invocation of the constructor on type X that matches the specified binding constraints threw an exception
Situation You want to edit a WPF application using Visual Studio. Problem Visual Studio throws a XamlParseException that says ‘The invocation of the constructor on type X that matches the specified binding constraints threw an exception’. Solution Open the dialog [Debug] – [Exceptions] (CTRL+D, E) and check the ‘Thrown’ checkbox for ‘Common Language Runtime Exceptions’.…
-
Using Microsoft Moles and dynamics (DLR)
Situation You want to use the ‘dynamic’ type in your unit-test. Problem An InvalidOperationException that says ‘Dynamic operations can only be performed in homogenous AppDomain.’ is thrown. Solution Changed the attribute ‘enabled’ of the node ‘legacyCasPolicy’ to ‘false’ in the following configuration file: C:Program Files (x86)Microsoft Visual Studio 10.0Common7IDEPrivateAssembliesMicrosoft.Moles.VsHost.x86.exe.config Sources http://weblogs.asp.net/mjarguello/archive/2011/04/26/using-moles-with-dlr.aspx http://www.cameronfletcher.com/post/InvalidOperationException-Dynamic-operations-can-only-be-performed-in-homogenous-AppDomain.aspx http://social.msdn.microsoft.com/Forums/en/pex/thread/0a931fc1-9049-482a-bf2d-5499ccf65b82 http://stackoverflow.com/questions/4230909/odd-exception-in-mvc-3-project
-
MessageBox in a unit-test
Situation You want to unit-test a method which uses a MessageBox. Problem The MessageBox will pop up and stop the execution of all tests until the user chooses an action (e.g. presses “OK”). Solution One way around this issue is mocking the MessageBox class to just return the expected DialogResult, which can be done using…
-
FxCop ignores GlobalSuppressions
Problem FxCop ignores the GlobalSuppressions file. Solution Open the build properties of the project ([Menu] – [Project] – [Properties] – [Build]) Add “CODE_ANALYSIS” to field [Conditional compilation symbols]. Hint: This field may be different between Debug and Release mode.
-
Using reflection to unit-test private methods
Situation There is a class called “MyClass” which has a private method called “MyPrivateMethod” (which expects a single string parameter) that you want to unit-test. Problem You can’t access private methods from the test class by just calling them. Solution Use reflection to invoke the method. typeof(MyClass).GetMethod(“MyPrivateMethod”, BindingFlags.NonPublic | BindingFlags.Instance) .Invoke(null, new object[] { “MyStringParameter” }); Hint: Static methods require the BindingFlag…