Return to frontpage

 

 

.Net code snippets

 

 

WPF code snippets

 

 

 

 

 

 

.NET serialization.

 

 

To serialize content to disk, the most used formats are binary and XML. Below you can find Save and Load methods for both formats. In the examples below we serialize an example class by the name "MyObject" to the specified filename.

 

IMPORTANT: The class being serialized must have the Serializable attribute to participate in binary or XML serialization.

 

 

 

Binary serialization.

 

You will need to reference System.Runtime.Serialization and System.Runtime.Serialization.Formatters.Binary for the serialization methods and System.IO for file handling.

 

 

 

 

 

 

Any property of the serialized class can be excluded by using the [field: NonSerialized] attribute on that property, so for example if we want to exclude an image from the serialization we would....

 

XML serialization.

 

You will need to reference System.Xml.Serialization for core serialization and System.IO for file handling.

 

 

 

 

Any property of the serialized class can be excluded by using the [XmlIgnore] attribute

 

 

LIMITATIONS: Multidimensional arrays cannot be serialized by the standard XML serializer. You will need to make a serialization rewrite and XML ignore the original array and instead translate that into something the XML serializer can handle. Something like this (note that in this example the multidimensional integer array is transformed into a string of 2 digit numbers for compactness. It could be any other representation defined by you, as long as the the resulting array is one dimensionel):

The XML output in this example...

 

 

 

 

WPF Visual rendering.

 

 

Transformations in XAML can be either Layout transformations or render transformations.

Layout transforms are rendered at the layout pass of WPF whereas render transforms are rendered after the layout pass. Render transforms do not affect the layout of its parent container, layout transfoms do affect layout.

 

It is very important to understand the difference between the two render modes and when to use what.

In above example using the Layout transform will cause the scrollviewer to automatically adjust scroll extents depending on the new canvas size.

 

 

Using the render transform instead, simply resizes the canvas but does not affect its container and it becomes impossible to scroll to some portions of the canvas

 

 

Dependency properties in XAML.

 

 

XAML markup contains some very powerful features allowing you to connect one property directly to the value of another property.

Binding Text elements to other controls and their values, here we bind the slider value directly to the text box.

 

 

 

Here we bind the state of the checkbox directly to the enabled state of the combo.

With zero code we can turn the combo on or off depending on the state of the checkbox

 

 

 

Control manipulation in XAML.

 

 

Setting containerstyle properties on lists.

 

Certain properties within a ListBox is determined by the ItemContainerStyle rather than the ItemStyle itself. One such property is the margin of each item in a list.

 

 

 

Image Processing.

 

 

Creating, rotating and zooming an image.

 

 

 

Creating an BitmapSource from an area of the screen.

 

This example uses the GDI+ system from WinForms as this isn't supported in WPF. The final image is a WPF BitmapSource that can be assigned directly to the Source propertry of an WPF Image.