Archive

Archive for March, 2009

Loading an Object from XAML Markup

March 30, 2009 Leave a comment

I’ve got to say that my absolute favorite feature of WPF is that you can create your entire user interface in a string of XAML.

Read on at https://loosexaml.wordpress.com/loading-ui-from-markup/

Advertisements

Using Reflection in WPF

March 30, 2009 Leave a comment

Reflection is a very powerful tool for building code that is highly customizable at runtime, making it very reusable. If you’re going to make an application that is primarily loose XAML, you will want to make whatever code you do write as flexible as possible, and to do so, it helps to have a firm grasp of reflection.

Read on at https://loosexaml.wordpress.com/reflection-for-wpf-rockstars/

Model-View-Controller with WPF

March 30, 2009 Leave a comment

With WPF, it is very simple follow the MVC pattern where a CLR object or XML is acting as your Model, and you want to present the data from the Model in a View, which is built in XAML by binding WPF controls to elements in the Model. What happens when we want to make changes to the data in the Model, such as updating some properties of that object? Your user is interacting with the View, so you need something in between the View and the Model, which is your Controller.

The most common approach to updating the data is to write event handlers in the code behind for your XAML file (your View). However, this means that your Controller logic is tightly coupled with your View…you can’t easily reuse it in another View. If you implement your Controller logic as a WPF ICommand, you can keep the logic in a different class and use it declaratively from any XAML file.

Read on at https://loosexaml.wordpress.com/model-view-controller-for-a-list/

Powershell to find the next available port

March 30, 2009 Leave a comment

Every now and then I’m installing service application on a development machine, and I just want to find the next available port that I can use. Here is a handy Powershell script to do that, which can be easily run as part of a deployment script.

param([int]$port = $(throw("Port required.")))
$isInUse = $FALSE
while(!$isInUse)
{
	trap [System.Net.Sockets.SocketException]
	{
		write-host "Unable to open socket on port $port."
		$script:port = $script:port + 1
		$script:isInUse = $TRUE;
		continue;
	}

	$listener = New-Object System.Net.Sockets.TcpListener([System.Net.IPAddress]::Any, $port)
	$listener.Start()
	write-host "Socket opened successfully on port $port."
	$listener.Stop()
	$isInUse = $FALSE
	break
}
return $port

The easiest way to call this is to include it directly in another powershell script, but if you want to save this as a script that you can call from a different script as a cmdlet, you need to execute this command first, which allows execution of unsigned scripts from the local machine.

set-ExecutionPolicy RemoteSigned

After you’re done with your processing, you may want to set this back to the default restricted setting:

set-ExecutionPolicy Restricted