Posts

Showing posts from 2011

.Net Validate a URL

         internal   bool  IsValidURL( string  value)         {              Uri  validUri;              return   Uri .TryCreate(value,  UriKind .Absolute,  out  validUri);         }          [ TestMethod ]          public   void  IsValidUrl()         {              // Act              var  actual = Subject.IsValidURL( @"http://www.google.com" );              // Assert              Assert .IsTrue(actual);         }         [ TestMethod ]          public   void  IsNotValidUrlWithoutHttp()         {              // Act              var  actual = Subject.IsValidURL( @"www.google.com" );              // Assert              Assert .IsFalse(actual);         }         [ TestMethod ]          public   void  UrlStartingWithHttpsIsValid()         {              // Act              var  actual = Subject.IsValidURL( @"https://www.google.com" );              // Assert              Assert .IsTrue(actual);         }

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 ).

WPF C# Detect CTRL + A (PreviewKeyDown)

XAML: < Window  x : Class ="LassoSelection.Controls.MainWindow"  xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"          xmlns : x ="http://schemas.microsoft.com/winfx/2006/xaml"  Title ="WPF C# Detect CTRL + A"            Height ="350"            Width ="525"            PreviewKeyDown ="WindowKeyDown">      < Grid >      Grid > Window > Code Behind: using  System.Windows; using  System.Windows.Input; public   partial   class   MainWindow {      public  MainWindow()     {         InitializeComponent();     }      private   void  WindowKeyDown( object  sender,  KeyEventArgs  e)     {          if  (e.Key ==  Key .A &&  Keyboard .Modifiers ==  ModifierKeys .Control)         {              MessageBox .Show( "CTRL + A Pressed!" );         }     } }   References: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.previewkeydown.aspx

WPF Get ListBoxItem from ListBox.SelectedItem using ItemContainerGenerator.ContainerFromItem

var  listBoxItem = listBox.ItemContainerGenerator.ContainerFromItem(myListBox.SelectedItem)  as   ListBoxItem ; if  (listBoxItem !=  null )  {      listBoxItem.Background =  SystemColors .HighlightBrush; }

Detecting drop (Drag and Drop between a WPF and a Non-WPF application)

Image
When you want to enable drag & drop between a WPF and a Non-WPF application where Drag is originated from the WPF application and Drop happens on the Non-WPF application, detecting when the Drop happened could be a challenge. You can achieve this 2 ways: Approach 1: DragDrop.DoDragDrop is a blocking call, thus when this call returns you can be sure that the drop happened. The drop could have happened in the same application or the other application, you would always know on the application that started the drag. Here is the DragDrop.DoDragDrop method signature: Code Sample: .... .... DragDrop .DoDragDrop(_draggedElt, data,  DragDropEffects .All); // Drop happened { } Approach 2: Before calling DragDrop .DoDragDrop attach the following event handler to the drag source:   var  queryhandler =  new   QueryContinueDragEventHandler (DragSourceQueryContinueDrag); _dragSource.QueryContinueDrag += queryhandler; While Drag is happening, the following event han

[Attached Property] WPF TextBox clears Text when user presses Escape key

To add the behavior to the WPF TextBox, use the following XAML. < Window  x : Class ="WPF.MainWindow"            xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"          xmlns : x ="http://schemas.microsoft.com/winfx/2006/xaml"            Title ="WPF TextBox MVVM DataBinding Example"  Height ="500"  Width ="500"          xmlns : Behaviors ="clr-namespace:Behaviors">      < Grid >          < TextBox  Behaviors : TextBoxBehavior.EscapeClearsText ="True" />      > > using  System.Windows; using  System.Windows.Controls; using  System.Windows.Input; namespace   Behaviors {      public   class   TextBoxBehavior     {         #region  Attached Property EscapeClearsText          public   static   readonly   DependencyProperty  EscapeClearsTextProperty            =  DependencyProperty .RegisterAttached( "EscapeClearsText" ,  typeof ( bool ),  typeof ( TextBo