Note: If your WPF UserControl is hosted in MFC Window, you may want to clean-up the ViewModel that is DataContext of your UserControl. Since Unloaded event is not called reliably for the UserControl when the hosting MFC Window closes, there is a need for another solution. XAML: < UserControl x : Class = "Views.MyUserControl" xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns : x = "http://schemas.microsoft.com/winfx/2006/xaml" xmlns : mc = "http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns : d = "http://schemas.microsoft.com/expression/blend/2008" mc : Ignorable = "d" d : DesignHeight = "300" d : DesignWidth = "300" > < Grid > </ Grid > </ UserControl > Code Behind: using System; using ViewModels; name
Dispatcher provides services for managing the queue of work items for a thread. Sometimes to execute a task on a background thread. Any task executed by calling Dispatcher.BeginInvoke(...) in unit test does not get executed until you do some wire-up. This is how you would unit test code with Dispatcher. using System; using System.Windows.Threading; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace UnitTesting.Tests { [ TestClass ] public class DispatcherUtilTest { private ClassThatUsesDispatcher _subject; [ TestInitialize ] public void TestInitialize() { _subject = new ClassThatUsesDispatcher (); } [ TestMethod ] public void UseDispatcher() { // Arrange var backgroundWorkDone = false ; _subject.BackgroungWorkAction = () => { backgroundWorkDone = true ; }; // Act _subject.DoWork(
/// 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); }
Comments
Post a Comment