WPF Image Button with MouseDown state

In CSS we can simply write:

a:active {
background: #000;
color: #fff;
}

for changing the state of a link when one clicks on them. The same applies to lots of other elements (not in old browsers, but more so in newer ones).

As, I’m learning WPF, I found that there wasn’t an easy way to do this, there are no states of a button as we see in iOS development, rather we have triggers:

<UserControl.Resources>
<UserControl.Resources>
<BitmapImage x:Key="btnRefreshOn" UriSource="/Emulator;component/Assets/btn_refresh_on.png" />
<BitmapImage x:Key="btnRefreshOff" UriSource="/Emulator;component/Assets/btn_refresh_off.png" />
</UserControl.Resources>
 
<Grid>
<Button Content="Check Now" Height="30" HorizontalAlignment="Left" Margin="10,4,0,0" Name="btnGetMessagesNow" VerticalAlignment="Top" Width="30">
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<Image Name="btnRefresh" Source="{StaticResource btnRefreshOff}" Stretch="Fill" VerticalAlignment="Center" />
<ControlTemplate.Triggers>
<Trigger Property="ButtonBase.IsPressed" Value="true">
<Setter Property="Image.Source" TargetName="btnRefresh" Value="{StaticResource btnRefreshOn}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Button.Template>
</Button>
</Grid>
</Page>

The code above has a trigger that fires when ButtonBase.IsPressed is fired, and sets the property Source of an image that makes up the template of a button.

One may say, why bother go to this length when we could simply change the source of the image from code behind. If you use UIElement.MouseDown on the target image, any other events such as MouseUp will not fire once you change the source of the target image.

 

End of today computation in C#, a simple performance analysis

Timing DateTime operations in C#. I have seen other programmers use two patterns to compute the end of day. Add 23 hrs, 59 mins, 50 secs or add 1 day and subtract 1 second. I was wondering which of these methods were faster so I decided to time it:

DateTime tic, toc, today;

today = DateTime.now;

tic = DateTime.Now;
for (long i = 0; i < 10000000; i++)
{
    var eod = today.AddHours(23).AddMinutes(59).AddSeconds(59);
}
toc = DateTime.Now;

Console.WriteLine("It took {0}", (toc - tic).TotalMilliseconds);

tic = DateTime.Now;
for (long i = 0; i < 10000000; i++)
{
    var eod = today.AddDays(1).AddSeconds(-1);
}
toc = DateTime.Now;

Console.WriteLine("It took {0}", (toc - tic).TotalMilliseconds);

tic = DateTime.Now;
int seconds = 3600 * 23 + 60 * 59 + 59;
for (long i = 0; i < 10000000; i++)
{
    var eod = today.AddSeconds(seconds);
}
toc = DateTime.Now;

Console.WriteLine("It took {0}", (toc - tic).TotalMilliseconds);

Results:

~0.700 ms: Add 23 hours, 59 minutes and 59 seconds takes:
~0.550 ms: Add 1 day, subtract 1 second
~0.250 ms: Add computed number of seconds

The reason why the third method is faster, is that it makes less value assignements than the previous 2. The first method makes 3 method calls and 3 value assignments, the next one 2 and the last 1, in the inner body of the loop. If repeated with  today being say, 30/12/2011, the results are pretty much the same.

So, a conclusion from this is a bit unexpected, value assignment and method invocations are more expensive (in this case) than time computations.

Back online

After crashing my VPS, I can’t seem to find my backups, and so all my Drupal content is gone. This is a new start, new config:

For a while I’ve been wanting to setup nginx, Apache has been so good to me, however, let’s give nginx a shot. Instructions to get it all working are at http://library.linode.com/web-servers/nginx/php-fastcgi/ubuntu-11.04-natty and they work just fine. I should try mariadb at some point, oh well, one change at a time. Nginx + PHP5 + FastCGI it is for now.