Archive for February 2011

WPF and Localization in the XAML with String.Format()

I had a localization issue today.

I was trying to get our product running in Spanish to show decimals as a comma (,) instead of a period (.).

Here is the XAML snippet I have.

        <StackPanel Orientation="Horizontal" Margin="0,0,0,5" >
            <Image Source="{Binding Image}" Width="24" Height="24" />
            <Label>
                <TextBlock>
                  <TextBlock.Text>
                         <MultiBinding StringFormat="{}{0} ({1}:) - {2} {3:F} GB, {4} {5:F} GB, {6} {7:F} GB">
                            <Binding Path="Drive.VolumeLabel" />
                            <Binding Path="Drive.DriveLetter" />
                            <Binding Source="{x:Static p:Resources.Required}" />
                            <Binding Path="Drive.TotalRequiredSpace.Gigabyte" />
                            <Binding Source="{x:Static p:Resources.Recommended}" />
                            <Binding Path="Drive.TotalRecommendedSpace.Gigabyte" />
                            <Binding Source="{x:Static p:Resources.Available}" />
                            <Binding Path="Drive.AvailableSpace.Gigabyte" />
                         </MultiBinding>
                  </TextBlock.Text>
                </TextBlock>
            </Label>
            <Image Source="{Binding DriveSpaceStatusImage}" Width="24" Height="24" Margin="15,0" />
        </StackPanel>

The Drive.TotalRequiredSpace object is a DataMeasurement object (which I have posted code for in a previous post).

The Gigabyte parameter is a Double.  These Double’s displayed the decimal separator using a period (.) character even though I was testing on a Spanish version of Windows Server 2008 R2.

I located the answer on another blog.
WPF Data Binding Cheat Sheet Update – The Internationalization Fix

He was using Date’s which like Double’s require decimals, and was seeing the same issue.  The solution is the same though.

Add the following code somewhere before your first WPF window opens.

System.Windows.FrameworkElement.LanguageProperty.OverrideMetadata
(
    typeof(System.Windows.FrameworkElement),
    new System.Windows.FrameworkPropertyMetadata(
        System.Windows.Markup.XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag)
    )
);

I am pretty sure this is a poor oversight and a bug in my opinion. Even though there is a one line fix, it was a costly fix, as I had to spend time troubleshooting and researching to find this one line solution which should be the default behavior.

A WPF XAML DataBinding Cheat Sheet

I found this Cheat Sheet and thought I would link to it as it could be very useful.

WPF XAML Data Binding Cheat Sheet

Personal Kanban: I just bought the book

I just bought this book today and it should arrive on Wednesday.

My wife and I can read it an improve our home projects. If we can eliminate waste at home, that will be as beneficial in our personal lives as it is for our work lives.

Though we are normal parents, I work and she is a stay at home mom, we both have blogs and we are also very involved in our church, so we should be able to improve our efficiency by visualizing our work.

Depending on how good this book is, I am going to recommend it to others in my family who could benefit from Kanban principles.

The perfect layout for a three column blog

I have always wanted the perfect three column design for my blog. I stumbled across this web site and I think this is the probably as good as it gets.

Holy Grail 3 column liquid-layout: No Quirks Mode, No IE Conditional Comments

This is by Matthew James Taylor and I have to say I am very impressed.

I renamed the CSS id’s to something easier for me to remember. I added a navigation bar, and another div to give a white space on the right and left.

Here is the basic html:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-GB">
<head>
  <title>Site Title</title>
  <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />

  <style type="text/css">
  body {
      margin:0;
      padding:0;
  }

  #ContainerMain {}

  a {
      /* color:#369; */
      text-decoration:none;
  }

  a:hover {
      text-decoration: underline;
  }

  h1, h2, h3 {
      margin:.8em 0 .2em 0;
      padding:0;
  }

  p {
      margin:.4em 0 .8em 0;
      padding:0;
  }

  img {
      margin:10px 0 5px;
  }

  #Header {
      float:left;
      width:100%;
      padding:0;
      margin:0;
      border-bottom:1px solid #000;
      background: #CCC;
      color: #FFF;
  }

  #Header p {
      padding:.5em 15px    .2em 15px;
      margin:0;
  }

  #Header h1  {
      padding:.2em 15px;
      margin:0;
  }

  #Header h2  {
      padding:.2em 15px    .7em 15px;
      margin:0;
  }

  #NavBar {
      float:left;
      width:100%;
      padding:0;
      margin:0;
      border-bottom:1px solid #000;
      background: #EEE;
      color: #FFF;

  }

  #ContainerColumns {
      margin-left: 20px;
      margin-right: 20px;
  }

  #ContainerLeft {
      position:relative;  /* This fixes the IE7 overflow hidden bug and stops the layout jumping out of place */
      clear:both;
      float:left;
      width:100%;         /* width of whole page */
      overflow:hidden;    /* This chops off any overhanging divs */
      background:#ffd8b7; /* Left column background colour */
  }

  #ContainerMiddle {
      float:left;
      width:200%;
      position:relative;
      left:200px;
      background:#EEE;    /* Centre column background colour */
  }

  #ContainerRight {
      float:left;
      width:100%;
      position:relative;
      left:50%;
      margin-left:-400px;
      background:#ff9;    /* Right column background colour */
  }

  #ColumnMiddlewrap {
      float:right;
      width:50%;
      position:relative;
      right:100%;
  }

  #ColumnMiddlepad {
      margin:0 15px 0 415px;
      overflow:hidden;
  }

  #ColumnMiddle {
      width:100%;
      overflow:hidden;
  }

  #ColumnLeft {
      float:left;
      width:170px;
      position:relative;
      margin-left:-50%;
      left:215px;
      overflow:hidden;
  }

  #ColumnRight {
      float:left;
      width:170px;
      position:relative;
      left:15px;
      overflow:hidden;
  }

  #footer {
      text-align: center;
      clear:both;
      float:left;
      width:100%;
      padding:0;
      margin:0;
      border-top:1px solid #000;
      background: #CCC;
  }

  #footer p {
      margin: 0px;
      padding: 0px;
      font-size: 12px;
  }
  </style>
</head>
<body>
  <div id="ContainerMain">
    <div id="Header">
      <h1>Site Name</h1>
      <h2>Site Tag Line</h2>
    </div>
    <div id="NavBar">Home</div>
    <div id="ContainerColumns">
      <div id="ContainerLeft">
	<div id="ContainerMiddle">
	  <div id="ContainerRight">
	    <div id="ColumnMiddlewrap">
	      <div id="ColumnMiddlepad">
		<div id="ColumnMiddle">
		  <!-- Column 1 start -->
		  <p>Middle Column</p>
		  <p><a href="www.rhyous.com">http://www.Rhyous.com</a></p>
		  <h1>Heading 1</h1>
		  <h2>Heading 2</h2>
		  <h3>Heading 3</h3>
		  <!-- Column 1 end -->
		</div>
	      </div>
	    </div>
	    <div id="ColumnLeft">
	      <!-- Column 2 start -->
	      <p>Left Column</p>
	      <!-- Column 2 end -->
	    </div>
	    <div id="ColumnRight">
	      <!-- Column 3 start -->
	      <p>Right Column</p>
	      <!-- Column 3 end -->
	    </div>
	  </div>
	</div>
      </div>
    </div>
    <div id="footer">
      <p>Footer</p>
      <p>This is where you put your footer stuff.</p>
      <p>
	<a href="http://validator.w3.org/check?uri=referer">
	  <img src="http://www.w3.org/Icons/valid-xhtml10" alt="Valid XHTML 1.0 Strict" height="31" width="88" />
	</a>
      </p>
    </div>
  </div>
</body>
</html>

Remakes of King’s Quest I, King’s Quest II, and King’s Quest III

My favorite childhood computer games were the King’s Quest games. I recently became aware that a company called Anonymous Game Developers Interactive has started remaking these games.

I’m downloading them now.

I hope they are awesome!

Released: FreeBSD 8.2-RELEASE and 7.4-RELEASE

Universal Remote Control: Philips SRU5107/27 (updated)

Saturday I bought a Philips SRP5107WM/17 Universal Remote Control for about $14 at Walmart. Best purchase I have made in a while. Until I bought the newer version, the Philips SRU5107/27, that works with XBox 360 for $17. (I am taking the older version back to Walmart.)

I have five devices I would like to remote control and each have their own remote control.  No, it is not OK to have 5 remote control devices on your couch. (Ok, if I get the XBox working, I will use it for the DVD player, so it could only be four).  Here are the devices.

1. Motorola Cable Box remote
2. DVD player remote
3. Tivo Series II remote
4. Proscan 40″ LCD TV remote
5. XBox 360 (which I just recently purchased)

This new remote I purchased worked perfectly with my Motorolla Cable Box, Magnovox DVD player, and Tivo Series II. I couldn’t find a perfect match for my Proscan 40″ LCD TV, but I found one that had all but a couple of buttons working and then I used the “Learn” feature the remote control has to fix the remaining buttons. The learn feature is seven levels of awesome!

The one device that is eluding me is the XBox. Supposedly an XBox 360 works with a Universal Remote Control, but I haven’t yet discovered what to do to make it work.

You can get a slightly newer version for a few more dollars through Amazon directly or from an Amazon affiliate for cheaper or the same with shipping that I have linked to on the left of this post. Posts say it worked with the XBox 360 first try. So I will take the older version back to Walmart and have ordered the newer version from Amazon. We’ll see how it works.

Update: I got the Philips SRU5107/27 from Amazon and it works with XBox 360 using remote code 1611. Yeah!

I am very impressed. I thought I was going to have to spend $100 or more but I didn’t. I am getting it all done for under $20.

Update: Just bought a new Blu-Ray player and this thing still works! It is still the best universal remote I have ever seen under $20.

Binding Visibility to a bool value in WPF

Please go here for updates to this post:

Binding Visibility to a bool value in WPF

I was putting code in my ViewModel that returns Visibility but I didn’t really like that very much. If the UI is created with something other than WPF, that is really not going to work. Since I intend to do cross compile my code in Mono, which doesn’t have WPF but uses either Forms or GTK#, I have already encountered this issue. What I really want to use is bool.

The solution is IValueConverter. If you just want the code and don’t want to read this post, just scroll to the bottom and grab the

IValueConverter is part of PresentationFramework (in PresentationFramework.dll) so it isn’t available in Mono, but that is OK because you don’t instantiate it in the ViewModel, you use it in the View so it will only be used when the GUI is part of WPF. So if you are separating your View into a separate DLL, this would be included in the View DLL, that way when you compile everything else, with say a different GUI that uses GTK#, you won’t get a compiler error because PresentationFramework doesn’t exist in Mono.

BooleanToVisibilityConverter

Well, there is an object already created for you called BooleanToVisibilityConverter, but it is limited. True is converted Visibility.Visible. False is converted to Visibility.Collapsed.

Here we see a problem. Visibility has three possible values but a Boolean only has two.

Boolean Visibility
True Visible
False Collapsed
Hidden

This will cover many of the scenarios, but not all.

Here is how it would be used.

For this example, I have this Person class.

    public class Person
    {
        public Person() { }
        public String FirstName { get; set; }
        public String LastName { get; set; }
        public String Age { get; set; }
    }

Here is a simple View for this object. It has a Grid that has a Label and TextBox for each property in the Person object. It also has a CheckBox. The CheckBox gives us a easy bool value, IsChecked. This works similar to a bool property in a ViewModel.

<Window x:Class="TestBooleanToVisibilityConverter.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:TestBooleanToVisibilityConverter"
        Title="MainWindow"
        SizeToContent="WidthAndHeight"
        >
    <Grid Margin="20">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Grid Name="PersonViewGrid">
            <Grid.Resources>
                <BooleanToVisibilityConverter x:Key="BoolToVisConverter"/>
            </Grid.Resources>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <Label Content="First Name:" Grid.Column="0" Grid.Row="0" />
            <TextBox Grid.Column="1" Grid.Row="0" Name="firstNameTextBox"
                     Text="{Binding Path=FirstName, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" MinWidth="175" />
            <Label Content="Last Name:" Grid.Column="0" Grid.Row="1" />
            <TextBox Grid.Column="1" Grid.Row="1" Name="lastNameTextBox"
                     Text="{Binding Path=LastName, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" MinWidth="175" />
            <Label Content="Age:" Grid.Column="0" Grid.Row="2"
                   Visibility="{Binding IsChecked, ElementName=ShowAgeCheckBox, Converter={StaticResource BoolToVisConverter}}"/>
            <TextBox Grid.Column="1" Grid.Row="2" Name="ageTextBox"
                     Text="{Binding Path=Age, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" MinWidth="175"
                   Visibility="{Binding IsChecked, ElementName=ShowAgeCheckBox, Converter={StaticResource BoolToVisConverter}}"/>
        </Grid>
        <Grid Grid.Row="1">
            <CheckBox Name="ShowAgeCheckBox" Content="Show Age" />
        </Grid>
    </Grid>
</Window>

I am not using MVVM for this example, but instead there is a just a single object created in the code behind for demo purposes.

using System;
using System.Windows;

namespace TestBooleanToVisibilityConverter
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent(); Person p = new Person() { FirstName = "Michael", LastName = "Michaels", Age = "33" };
            PersonViewGrid.DataContext = p;
        }

    }
}

Ok, now build the project and you will see that the Label and TextBox for Age are hidden until you check the box.

Writing your own Bool to Visibility Converter

Sometimes you may need to write you own Converter.  For example, in the above project, it is annoying how the CheckBox moves up and down in position because Visibility.Collapsed is used instead of Visibility.Hidden.  You may want to use Visibility.Hidden instead.

You can write your own Converter that returns Visibility.Hidden instead of Visibility.Collapsed.

BooleanToVisibleOrHidden.cs

using System;
using System.Windows.Data;
using System.Windows;

namespace TestBooleanToVisibilityConverter
{
    class BoolToVisibleOrHidden : IValueConverter
    {
        #region Constructors
        /// <summary>
        /// The default constructor
        /// </summary>
        public BoolToVisibleOrHidden() { }
        #endregion

        #region IValueConverter Members
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            bool bValue = (bool)value;
            if (bValue)
                return Visibility.Visible;
            else
                return Visibility.Hidden;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            Visibility visibility = (Visibility)value;

            if (visibility == Visibility.Visible)
                return true;
            else
                return false;
        }
        #endregion
    }
}

Here we do the conversion ourselves and now we have a different conversion table.

Boolean Visibility
True Visible
Collapsed
False Hidden

Now replace the Converter object in your XAML.  We only change Line 15.

      <local:BoolToVisibleOrHidden x:Key="BoolToVisConverter"/>

This works, but it could be improved. This still leaves us having to choose between two objects.

Creating a Converter that supports a choice of Hidden or Collapsed.

Here we will provide a property that determines if we should collapse or not.

Add a property called Collapse and return the appropriate Visibility based on that property. Here is the new object. As you see, the code changes to add this feature is really just an empty bool property and an if statement that used the bool property.

using System;
using System.Windows.Data;
using System.Windows;

namespace TestBooleanToVisibilityConverter
{
    class BoolToVisibleOrHidden : IValueConverter
    {
        #region Constructors
        /// <summary>
        /// The default constructor
        /// </summary>
        public BoolToVisibleOrHidden() { }
        #endregion

        #region Properties
        public bool Collapse { get; set; }
        #endregion

        #region IValueConverter Members
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            bool bValue = (bool)value;
            if (bValue)
            {
                return Visibility.Visible;
            }
            else
            {
                if (Collapse)
                    return Visibility.Collapsed;
                else
                    return Visibility.Hidden;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            Visibility visibility = (Visibility)value;

            if (visibility == Visibility.Visible)
                return true;
            else
                return false;
        }
        #endregion
    }
}

Now in your XAML you have the option to do nothing, which uses the bool default value false, or to set the Collapse property to true as shown below.

      <local:BoolToVisibleOrHidden x:Key="BoolToVisConverter" Collapse="True"/>

We now support either feature with the following table. We probably at this point would rename the object to BooleanToVisibilityConverter, but Microsoft already took that object name so I will leave it named as is.

Boolean Visibility
True Visible
False – Collapse=True Collapsed
False – Collapse=False Hidden

We are starting to get a little more usability from one object.

Adding the Reverse feature so False is Visible and True is Collapsed or Hidden

Lets say we want to change the CheckBox so that instead of saying “Show Age” it says “Hide Age”.

            <CheckBox Name="ShowAgeCheckBox" Content="Hide Age" />

Now we have to reverse the mapping. If Reverse=”True” we want the mapping to be like this:

Boolean Visibility
False Visible
True – Collapse=True Collapsed
True – Collapse=False Hidden

This is also quite simple. We add another bool property called Reverse. Then key of that in another if statement.

using System;
using System.Windows.Data;
using System.Windows;

namespace TestBooleanToVisibilityConverter
{
    class BoolToVisibleOrHidden : IValueConverter
    {
        #region Constructors
        /// <summary>
        /// The default constructor
        /// </summary>
        public BoolToVisibleOrHidden() { }
        #endregion

        #region Properties
        public bool Collapse { get; set; }
        public bool Reverse { get; set; }
        #endregion

        #region IValueConverter Members
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            bool bValue = (bool)value;

                if (bValue != Reverse)
                {
                    return Visibility.Visible;
                }
                else
                {
                    if (Collapse)
                        return Visibility.Collapsed;
                    else
                        return Visibility.Hidden;
                }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            Visibility visibility = (Visibility)value;

                if (visibility == Visibility.Visible)
                    return !Reverse;
                else
                    return Reverse;
        }
        #endregion
    }
}

Now you can reverse this very easily in the XAML.

      <local:BoolToVisibleOrHidden x:Key="BoolToVisConverter" Collapse="True" Reverse="True" />

And now you have a much more full featured converter.

Additional Thoughts

I have to wonder why the developers didn’t do this originally with the BooleanToVisibilityConverter object. It is so simple. This is a perfect example of where Microsoft would benefit from Open Sourcing some of their code. A dozen people would have contributed this change by now if they had and all Microsoft would have to do is look at the submitted code, approve, and check it in.

BSD Licenses good, GPL bad: Microsoft Bans Some Open Source Licenses from WP7 Marketplace

Microsoft is not going to allow GPL onto their phones.
Microsoft Bans Some Open Source Licenses from WP7 Marketplace

Microsoft has stated that its Windows Phone 7 marketplace will reject any apps that use the GPL (GNU General Public License) and similar licenses.

“The Windows Phone Marketplace supports several open source licenses, including BSD, MIT, Apache Software License 2.0, MS-PL and other similar permissive licenses.  We revise our Application Provider Agreement from time to time based on customer and developer feedback, and we are exploring the possibility of modifying it to accommodate additional open source-based applications in upcoming revisions.”

Microsoft is doing the right thing and cannot be blamed in the slightest. The GPL is often termed a viral license and for good reason. Once you use it in your code everything is infected by it. Others say it is a spiderweb license, that once you are in the spider’s web, you can’t get out. The BSD license instead of the GPL is probably the single biggest reason to use FreeBSD over Linux, especially for enterprise business such as Microsoft, Apple, and others.

I don’t like the entrapment of the GPL. Students often first encounter the GPL in college, where they hear that the GPL is free and start using it. Only later do they realize they are trapped. Some don’t mind, wish they would have understood the license better.

Here is a simple rhyme to remember which license to use:

If you want your software to really be free,
    license it with BSD.
If you want your software to be in license hell,
    use the GPL.

This post shows that Microsoft feels the same as many of us who are anti-GPL.

Obviously they don’t want to have those who write their apps ever accidental depend on another app, only to find out the app they depended on is GPL, so their entire work must be GPL as well. They are doing what humanity tries to do with any virus, eradicating it and prevent infection by eliminating the virus, just as we have done with small pox, from the world.

For more information on the differences between the BSD License and the GPL, read this post.
Differences between the BSD/FreeBSD Copyrights and the GNU Public License (GPL)

My spread out topics and feeds

I knew I could provide a feed for just FreeBSD or just for C# as I am still on WordPress, and haven’t moved to SilverStripe yet (that will take time), but I finally got around to figuring it out.

All posts

Follow Us on RSS https://www.rhyous.com/feed

FreeBSD

Follow Us on RSShttps://www.rhyous.com/category/freebsd/feed/

C#

Follow Us on RSShttps://www.rhyous.com/category/development/c-c-sharp/feed/

Basically, these are Category feeds.

http://perishablepress.com/press/2008/03/09/what-is-my-wordpress-feed-url/

Backing up PostgreSQL databases nightly on FreeBSD

I didn’t write this post, a blogger named Keith did, but I am definitely going to link to his work on his blog.

Backing up PostgreSQL databases nightly on FreeBSD

He did a good job editing this script to backup postgresql on FreeBSD. While he does say he got some of it from another site he found on using Google…

…Using Google I found a generic script, after some custom tweaks for FreeBSD I had the script I wanted.

…so some of the work should be attributed elsewhere. I want to thank him for the work he did. This was something that was on my to do list and now it is pretty much done for me.

Here is the script he posted.

#!/bin/sh
# Location of binaries
bin="/usr/local/bin"
# Location of the backup logfile.
logfile="/data/backup/postgres/postgres.log"
# Location to place backups.
backup_dir="/data/backup/postgres"
username="pgsql"
database="template1"
touch $logfile
timeslot=`date +%H-%M`
databases=`$bin/psql -h localhost -d $database -U $username -q -c "\l" | sed -n 4,/\eof/p | grep -v rows\) | awk {'print $1'}`

for i in $databases; do
        echo "Backup and Vacuum complete at $timeinfo for time slot $timeslot on database: $i " >> $logfile
        $bin/vacuumdb -z -h localhost -U $username $i >/dev/null 2>&1
        j="60"
        while [ $j -ge 0 ]
        do
                temp0=`expr $j - 1`
                temp1=$j
                j=`expr $j - 1`
                if [ $temp0 -lt 0 ]
                then
                        suffix0=""
                        suffix1=".0"
                else
                        suffix0=".$temp0"
                        suffix1=".$temp1"
                fi
                if [ -f $backup_dir/postgresql-$i-database.gz$suffix0 ]
                then
                        echo "Renaming postgresql-$i-database.gz$suffix0 to postgresql-$i-database.gz$suffix1"
                        mv $backup_dir/postgresql-$i-database.gz$suffix0 $backup_dir/postgresql-$i-database.gz$suffix1
                fi
        done
        if [ $i != 'template0' ]
        then
                $bin/pg_dump -U $username $i -h 127.0.0.1 | gzip > "$backup_dir/postgresql-$i-database.gz"
        fi
done

Here is what I like about his script:

  1. Variables are used at the top of the script that are easily modifiable.
  2. It backs up all databases without having to list the databases, so you don’t have to update the list with every new database.
  3. It does compress using gzip the databases, so uses as little space as possible.

Here are some ideas for future enhancements to the script

  1. The gzip compression level as a variable up top. The default gzip compression level is 6 and 9 could make the file significantly smaller with large databases, while 1 would be faster for machines with weaker processors.
  2. Database to exclude from the backup.  I like the idea to get them all by default, but sometimes you have a test database you just don’t care about and you don’t want a nightly backup of it.
  3. I don’t see the password in the script, and I am not sure how it is authenticating without it, maybe a password feature needs to be added.

Personal Kanban

At work, our development cycle is being converted to a Kanban system.  I read a book, cleverly titled Kanban, about the process and it was really good.  I have been thinking of bringing Kanban home and then I googled “Personal Kanban” and low and behold there is already a book on it.
http://www.personalkanban.com/pk/personal-kanban-the-book/

The idea of Kanban can be summed up best in two ideas that are intended to eliminate wasted time:

  1. Make the work visible. – If you always know what is next, you don’t waste time wondering what to do next or forget and do something not yet needed.
  2. Limit the Work in progress – By not jumping from item to item, you have less wasted time.

So it is basically like a “honey-do” list, but where you limit the amount of items you can actually be working on at one time.

The idea is to have columns similar to this:

List of Work Prepared work Work in progress (WIP) Complete
Clean garage Grease hinges Write Wix fragment app Fix sink
Fix paint in archway Repair siding
Add shelves to garage Get new tires
Help brother with his website Fix blog

So I am going to try this at home for a while. I may even buy the book. I did put it on my amazon wish list.

BSD Magazine February Issue is out: ZFS and FreeBSD

BSD Magazine February Issue released.

Download it here:

http://bsdmag.org/magazine/1638-zfs-and-freebsd

Handling a custom name space (xmlns) in an XML with Xml Serialization

I have an xml that has a name space. This gave me to problems I had to resolve.

  1. How do I deserialize this xml with that name space value?
  2. How do I serialize the object to only have this one name space?

Deserializing an Xml with a Name Space

Below is an Xml file. Notice that the PrimaryNode has a name space that isn’t the default.

<?xml version="1.0" encoding="utf-8"?>
<PrimaryNode xmlns="http://some/random/namespace/example>
  <SecondaryNode>
    <TertiaryNode />
    <TertiaryNode />
  </SecondaryNode>
</PrimaryNode>

I had an object created as follows.

using System;
using System.Xml.Serialization;
using System.Collections.ObjectModel;

namespace XmlNameSpaceTest.Model
{
    [Serializable()]
    public class PrimaryNode
    {
        private ObservableCollection<SecondaryNode> _Children;

        public PrimaryNode() { }

        [XmlElement("SecondaryNode")]
        public ObservableCollection<SecondaryNode> Children
        {
            get
            {
                if (_Children == null)
                    _Children = new ObservableCollection<SecondaryNode>();
                return _Children;
            }
        }
    }
}

I used this static class for the serialization

using System;
using System.IO;
using System.Xml.Serialization;

namespace XmlNameSpaceTest.Model
{
    public class Serializer
    {
        #region Functions
        public static void SerializeToXML<T>(T t, String inFilename)
        {
            XmlSerializer serializer = new XmlSerializer(t.GetType());
            TextWriter textWriter = new StreamWriter(inFilename);
            serializer.Serialize(textWriter, t);
            textWriter.Close();
        }

        public static T DeserializeFromXML<T>(String inFilename)
        {
            XmlSerializer deserializer = new XmlSerializer(typeof(T));
            TextReader textReader = new StreamReader(inFilename);
            T retVal = (T)deserializer.Deserialize(textReader);
            textReader.Close();
            return retVal;
        }
        #endregion
    }
}

But it failed to deserialize because of the name space. It gave me this exception (which I trimmed to only show the important parts as it was very long).

System.InvalidOperationException was unhandled
  Message=There is an error in XML document (2, 2).
  Source=System.Xml
  StackTrace:
       <-- snipped -->
  InnerException: System.InvalidOperationException
       Message=<PrimaryNode xmlns='http://some/random/namespace/example'> was not expected.
       Source=bgwwvhdx
       StackTrace:
            at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderPrimaryNode.Read7_PrimaryNode()

The solution was simple. I had to add the XmlRoot tag above the class.

    [Serializable()]
    [XmlRoot(Namespace="http://some/random/namespace/example")]
    public class PrimaryNode
    {
        ... snipped...
    }

This solved problem 1. The Xml now deserialized just fine.

Serializing an Xml so it only contains a single Name Space

When I serialized the object, there were two default name spaces, but neither were the custom name space that the Xml needed.

<?xml version="1.0" encoding="utf-8"?>
<PrimaryNode xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <SecondaryNode>
    <TertiaryNode />
    <TertiaryNode />
  </SecondaryNode>
</PrimaryNode>

Here is what I had to do.

1. Add a XmlSerializerNamespaces property with only the one name space added.

    [Serializable()]
    [XmlRoot(Namespace="http://some/random/namespace/example")]
    public class PrimaryNode
    {
        ... snipped...

        private XmlSerializerNamespaces _Namespaces;

        public XmlSerializerNamespaces NameSpaces
        {
            get
            {
                if (_Namespaces == null)
                {
                    _Namespaces = new XmlSerializerNamespaces();
                    _Namespaces.Add("", "http://schemas.microsoft.com/wix/2006/wi");
                }
                return _Namespaces;
            }
        }
    }

2. Then I had to change my Serializer class. I needed to overload the Serialize function by adding a version of it that accepts the XmlSerializerNamespaces object as the third parameter.

        public static void SerializeToXML<T>(T t, String inFilename, XmlSerializerNamespaces namespaces)
        {
            XmlSerializer serializer = new XmlSerializer(t.GetType());
            TextWriter textWriter = new StreamWriter(inFilename);
            serializer.Serialize(textWriter, t, namespaces);
            textWriter.Close();
        }

3. Then, you’ve probably realized already, when serializing the object to Xml, we use this new function and pass it the PrimaryNode.NameSpaces as the third parameter.

This solved the second problem.

I hope this post helps you.

WPF Label, TextBox, and Mnemonics

Hello. Today I had to get mnemonic to work. Mnemonic is the ability to navigate to a control using a key stroke combination. One of the most common mnemonics is Alt + F. Alt + F usually navigates to the File drop down menu in any Window, and most everyone is familiar with this mnemonic.

In WPF, to get mnemonics, you pretty much just put an underscore in front of a word. For example, for Alt + F, you would enter: _File

Well, in WPF I needed to do the Label, TextBox matching, so that the mnemonics is on a Label but the key strokes navigate the keyboard focus to the TextBox.

Lets say you have a simple form in WPF like this:

The XAML to make these work together using mnemonics is simple. The important property is Target.

<Window x:Class="Mnemonics.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" >
    <Grid FocusManager.FocusedElement="{Binding ElementName=textBoxFirstName}">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Label Target="{Binding ElementName=textBoxFirstName}" HorizontalContentAlignment="Right">
            <AccessText TextWrapping="WrapWithOverflow">First _Name:</AccessText>
        </Label>
        <TextBox Name="textBoxFirstName" Width="200" Height="20" Grid.Column="1"/>
        <Label Target="{Binding ElementName=textBoxLastName}" HorizontalContentAlignment="Right" Grid.Row="1">
            <AccessText TextWrapping="WrapWithOverflow">_Last Name:</AccessText>
        </Label>
        <TextBox Name="textBoxLastName" Width="200" Height="20" Grid.Row="1" Grid.Column="1"/>
    </Grid>
</Window>

Lets discuss the code above briefly.

  1. Notice line 5 first.  Here I am setting the FocusManager.FocusedElement to the first text box so that you can immediately type in the first field when arriving at the window.
  2. Notice the use of AccessText instead of TextBlock (lines 15, 19).  You don’t have to do it this way.  You could have just set the Content property on the label. You don’t need the AccessText at all unless you want to wrap.  If you want to wrap, you have to do it this way.
  3. Notice the use of Target (lines 18,14) and how it is bound to the element that should receive focus when the mnemonic keys are pressed.

Hope this helps you.