Skip Ribbon Commands
Skip to main content

Quick Launch

Home
Some technical things, mostly odd happenings from a remote office
August 24
Toad night light

I turn on a light each night to attract insects for toads (which seems a little odd now that I think about it). The other night I went outside to look at the moon and planets, and I noticed that I had forgotten to turn on the light. On my way back to the house, there were several toads waiting in their usual spot, looking at me wondering what happened to their light.

  

August 23
Red-eyed devil katydid

We came across another red-eyed devil (type of katydid) over the weekend. They are more unique since they are quite large, and very aggressive. When provoked, they rear back in defensive stand and will bite if possible. The bite does not hurt too bad since they don’t inject any type of poison, but can break the skin. Here are photos of one we found before.

   

May 13
The pigs visit at night

At first they were cute, little wild pigs sneaking up to our yard in the late evening eating spilled bird seed and food we put out for the deer. But now they are really a pain… they come up every night, tip over the bird baths, move landscaping rocks around, and root up the plants. I tried larger rocks, but they keep persisting in their quest to root up everything. I can hear them outside my window when working late, mocking me, with their squealy pig laughter while they root up another trailing purple lantana.

September 30
Twelve plus one = Eleven plus two

'Twelve plus one' contains the same letters as 'eleven plus two'. I did not come up with this, but thought it was interesting.

May 15
Big centipede escapes
The kids found a Giant Redheaded Centipede crawling around outside so we caught it and put Bob (kids named it Bob right away) in a cage to observe for a few days. It started off well, it ate 7 mealworms the first evening, but we were surprised to discover it had escaped the next morning. Now we have a 7" centipede crawling around somewhere.
March 26
DynamicTypeService error in Silverlight projects
I ran into an error with web services when updating an application from Silverlight 2 to Silverlight 3. 
Custom tool error: Failed to generate file: Unable to find service 'DynamicTypeService'.  Ensure that the application is installed correctly.
This turned out to be a problem with the upgrade process and not Silverlight 3. I did the following to resolve the problem.
  1. Delete the file \Program Files\Microsoft Visual Studio 9.0\Common7\IDE\Microsoft.VisualStudio.ServicesProxy.dll.
  2. Reinstall Silverlight 3, which installs an updated Microsoft.VisualStudio.ServicesProxy.dll.
  3. Open the solution and update each web service.


January 15
Visual Studio 2008, missing user-handled column in Exceptions window
Sometimes I find that the User-handled column in the Exceptions window is missing in Visual Studio 2008.

image

It can be displayed by selecting Tools / Options… / Debugging / General / and checking Enable Just My Code (Managed Only).

image

Then the User-handled column is available.

image

November 21
Snapshot of XAML control, save to image
You can use the RenderTargetBitmap class to generate an image from a UI element. For example, say you want to generate an image from the XAML element ThePanel.
<StackPanel Name="ThePanel" Orientation="Horizontal">
    <Rectangle Width="50" Height="50" Fill="Red" />
    <Rectangle Width="50" Height="50" Fill="Orange" />
    <Rectangle Width="50" Height="50" Fill="YellowGreen" />
</StackPanel>
You can use RenderTargetBitmap to generate an image, and PngBitmapEncode to save to a PNG file.
void SaveImage(Visual visual, int width, int height, string filePath)
{
    // generate the image
    RenderTargetBitmap bitmap = new RenderTargetBitmap(width, height, 9696PixelFormats.Pbgra32);
    bitmap.Render(visual);

    // create image file
    PngBitmapEncoder image = new PngBitmapEncoder();
    image.Frames.Add(BitmapFrame.Create(bitmap));
    using (Stream fs = File.Create(filePath))
    {
        image.Save(fs);
    }
}
Calling the SaveImage method.
// full path to the image
string filePath = Path.Combine(System.Environment.CurrentDirectory, "sample.png");

// generate image from XAML element
SaveImage(this.ThePanel, 500500, filePath); 
If you are creating the control dynamically, be sure to call Arrange on the control before generating the image.
// full path to the image
string filePath = Path.Combine(System.Environment.CurrentDirectory, "sample.png");

// create instance of control
Button button = new Button();
button.Content = "Nifty Button";

// layout control before generating image
button.Arrange(new Rect(00500500));

// generate image from XAML element
SaveImage(button, 500500, filePath); 


September 24
Make sure Silverlight application runs on different languages
It's a good idea to make sure your Silverlight application runs correctly when running on a system that uses a different language. This is not localizing an application (changing the user interface), but just making sure it executes as expected.

For example, the following code executes correctly running as English (United States).
// get data, could be from an XML file or some other data source
string someNumber = "1.23";
string someDate = "2/15/1564";

// convert data, everything is fine running as English
double number = double.Parse(someNumber);
DateTime date = DateTime.Parse(someDate);
But fails when running as Dutch (Belgium).
// get data, could be from an XML file or some other data source
string someNumber = "1.23";
string someDate = "2/15/1564";

// not good, returns 123.0 instead of 1.23 when running as Dutch
double number = double.Parse(someNumber);

// not good, throws a FormatException when running as Dutch
DateTime date = DateTime.Parse(someDate);
This can be fixed by specifying the culture specific format when parsing the string.
// get data, could be from an XML file or some other data source
string someNumber = "1.23";
string someDate = "2/15/1564";

// good, returns 1.23 when running as Dutch
double number = double.Parse(someNumber, CultureInfo.InvariantCulture);

// good, returns correct date when when running as Dutch
DateTime date = DateTime.Parse(someDate, CultureInfo.InvariantCulture);
The point is not the specific parse calls, but there is a potential problem anytime you format or parse a string.

Help catch problems
You can use FxCop or Visual Studio Code Analysis to help find globalization (and other) problems.

Testing
You can use Regional and Language Options under control panel to help test.

  • Run Regional and Language Options from control panel.
  • Select the Formats tab.
  • Select a language such as Dutch (Belgium) under current format.
  • Press Apply.
  • Run your application and test.

September 23
Silverlight, handling events for nested controls
Silverlight does not have as much support to handle events from unreferenced child controls as WPF. For example, say you have the following control hierarchy and Page needs to handle events from the Child Controls.

This is easy in WPF since you can use commands, or UIElement.AddHandler. For example:
public MainWindow()
{
    InitializeComponent();

    // subscribe to events raised by any child, Child Control is a Button in this example
    this.AddHandler(Button.ClickEvent, new RoutedEventHandler(ChildClicked));
}
        
private void ChildClicked(object sender, RoutedEventArgs e)
{
    // called when event is raised by any child
}
Or you can declare the event handler in XAML.
<Window x:Class="WpfEvents.MainWindow"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:controls="clr-namespace:WpfEvents"
  Title="WPF Events" Height="200" Width="200"
  Button.Click="ChildClicked">
Back to Silverlight, it does not support commands or UIElement.AddHandler. There are several options:

Hookup each event handler
You can specify the event handler for each child control but that is the whole point... we want to handle events even though we don't have a reference to the individual child controls.
// hooking up events requires a reference to each control
this.ChildControl1.Click += new RoutedEventHandler(ChildControl1_Click);
this.ChildControl2.Click += new RoutedEventHandler(ChildControl2_Click);
this.ChildControl3.Click += new RoutedEventHandler(ChildControl3_Click);
this.ChildControl4.Click += new RoutedEventHandler(ChildControl4_Click);
Bubble up the events
You can write code that bubbles up the events (re-raises the events). This usually becomes undesirable, especially when you have controls nested at several levels.

Use global Events class
One option is to create a global Events class.

  • There is only one instance of the Events class in the application.
  • It contains a list of global events and helper methods to raise the events.
  • Any object can subscribe to events.
  • Any object can raise events.
  • That means the main page can handle events from any child control, children can handle events from the main page, and siblings can handle events from each other.
  • A reference to the control is not required to handle events.
Example Events class.
public static class Events
{
    // example global event that can be handled by any object in the application
    public static event EventHandler ChildControlClicked;
    public static void RaiseChildControlClicked(object sender, EventArgs args)
    {
        if (ChildControlClicked != null)
        {
            ChildControlClicked(sender,args);
        }
    }
}
Example of raising an event.
// raise global event
Events.RaiseChildControlClicked(thisEventArgs.Empty);
Example of handling an event.
public Page()
{
    InitializeComponent();
    
    // subscribe to global event
    Events.ChildControlClicked += new System.EventHandler(Events_ChildControlClicked);
}

private void Events_ChildControlClicked(object sender, System.EventArgs e)
{
    // called when the event is raised, it could be raised by
    // any object in the application, this object does not need
    // a reference to the object that raised the event
}

1 - 10Next