Posts

Showing posts from August, 2011

WPF: How to Deep Copy WPF object (e.g. UIElement) ?

         ///  Generic Method to perform Deep-Copy of a WPF element (e.g. UIElement)          public   static  T DeepCopy (T element)         {              var  xaml =  XamlWriter .Save(element);              var  xamlString =  new   StringReader (xaml);              var  xmlTextReader =  new   XmlTextReader (xamlString);              var  deepCopyObject = (T) XamlReader .Load(xmlTextReader);              return  deepCopyObject;         }          ///  Test showing that the deep copied object does not have the same reference as the object being copied         [ TestMethod ]          public   void  DeepCopy()         {              // Arrange              Shape  shape =  new   Rectangle  {Fill =  new   SolidColorBrush ( Colors .Red)};              // Act              var  deepCopyShape =  MyClass .DeepCopy(shape);              // Assert              Assert .AreNotSame(shape, deepCopyShape);         }  

C# Get Currently logged in windows user

using  Microsoft.VisualStudio.TestTools.UnitTesting; using  System.Security.Principal;  /* Namespace needed to get currently logged in user */ namespace  SecurityModules {     [ TestClass ]      public   class   Security     {         [ TestMethod ]          public   void  GetCurrentUser()         {              // Act              var  currentlyLoggedInUser =  WindowsIdentity .GetCurrent().Name ;              // Assert              Assert .IsFalse( string .IsNullOrEmpty(currentlyLoggedInUser));         }     } }

Unit Testing - Deploy Files & Folders (Microsoft Visual Studio)

Image
Scenario:  I have a test that needs a file deployed inside a folder (e.g. - DeploymentItems\ControlsMetaData.xml) You can use the DeploymentItem attribute with the 2 parameters overload and specify the directory (folder) you would like the test file to be deployed in. [ TestMethod ,  DeploymentItem ( "DeploymentItems\\ControlsMetaData.xml" ,  "DeploymentItems" )] public   void  DeployFileAndContainingFolder() {      // Arrange                    // Act       var  xmlFiles =                  UXStandardsViewModel .GetFilesByExtension(                      Path .Combine(TestContext.TestDeploymentDir,  "DeploymentItems" ),  ".xml" );      // Assert       Assert .AreEqual(1, xmlFiles.Count()); } The test above, gets all files with extension “.xml” at “DeploymentItems\ControlsMetaData.xml” The folder above is the Test output directory.    

.Net C# Multi-Threading in C#

http://www.albahari.com/threading/

WPF XAML Escape Sequence {}

Example: < TextBox Text =" {} pack://application:,,,/Controls;component/WatermarkTextBox/WatermarkTextBoxSample.xaml" />   {} is the escape sequence for string use in XAML. Reference: http://msdn.microsoft.com/en-us/library/ms744986.aspx

The assembly used when compiling might be different than that used when loading and the type is missing.

Image
Why the error? My WPF application UIXStandards has reference to a class library Controls which in turn has a reference to the Smt.Windows.Controls library. In Visual Studio when I add reference to Controls library from my UIXStandards   application, Visual Studio does not copy any references needed for the Controls library (e.g . ) Smt.Windows.Controls to UIXStandards application. Solution: To solve the problem, within Visual Studio when you refer to a class library ( Controls ) also add references its dependencies ( Smt.Windows.Controls ).