1 0 Archive | September, 2010
post icon

Silverlight Tip of the Day #35 – Dynamically Changing Styles

Recently I was trying to make it so that at a click of a button I could change the Foreground color of all HyperlinkButton’s in my application.

To do so I traversed through all styles in my App.xaml until I found the template for my HyperlinkButton. Once found, I removed the setter that contained the Foreground color and I then tried to replace it with a new Setter that contains the new Foreground color.

The following code shows how I was trying to do this:

foreach (DictionaryEntry obj in App.Current.Resources)
{
    if(obj.Value.GetType() == typeof(Style))
    {
        Style resourceStyle = (Style)obj.Value;
        if (resourceStyle.TargetType == typeof(HyperlinkButton))
        {
            resourceStyle.Setters.RemoveAt(0); // the first one is the foreground color

            Setter s = new Setter(HyperlinkButton.ForegroundProperty,
                new SolidColorBrush(newColor));
            resourceStyle.Setters.Add(s);

            break;
        }
    }
}
 

The problem is once a Style has been applied to a live element it’s consider “sealed” and it’s setters cannot be modified. That is, the Style.IsSealed property is set to true. The result is when you run the code above you get the following exception thrown:

Error HRESULT E_FAIL has been returned from a call to a COM component.

To work around this problem all you have to do is copy and replace style with a style that contains the new color you want. The following code shows you how to accomplish this:

Style style = new Style(typeof(HyperlinkButton));
style.BasedOn = App.Current.Resources[typeof(HyperlinkButton)] as Style;
style.Setters.Add(new Setter(HyperlinkButton.ForegroundProperty,                        new SolidColorBrush(color)));
App.Current.Resources.Remove(typeof(HyperlinkButton));
App.Current.Resources.Add(typeof(HyperlinkButton), style);

Thanks to Jesse Bishop for showing me this technique!

–Mike

Leave a Comment
post icon

Silverlight Tip of the Day #34 – Removing Underline from the Hyperlink Control

If you wish to remove the underline from the Hyperlink Control in Silverlight you can do so by overloading the default template and deleting the sections with UnderlineTextBlock referenced in it.

The template can be found at: http://msdn.microsoft.com/en-us/library/cc296242(v=VS.95).aspx

Copy this template to your App.xaml and remove all references of <ObjectAnimationUsingKeyFrames Storyboard.TargetName="UnderlineTextBlock"…> from within the VisualState sections.

If you want all hyperlink buttons in your application to automatically use your new template simply avoid giving it a key name (x:Name=””).

Thanks,
–Mike

Leave a Comment
28. Sep, 2010
post icon

Silverlight Win7 Mobile – Castle Defense

Recently I have been working on some applications for the upcoming release of the Win7 mobile phone. One of them is called “Castle Defense”. The goal of the game is to keep waves of creatures from reaching your castle. To do this, you must build a maze of towers that will attack the creatures as they wind their way towards you castle entrance. If 20 creatures enter you castle you lose the game. Survive all 45 waves of creatures to win the game.

The game is still under construction but I thought I’d post a link so you can preview it through your browser. Feedback on this game would be greatly appreciated (the good, the bad and the ugly).

You can check it out here: http://www.michaelsnow.com/towers/default.aspx

Stuff to do still:

1. Need help with art work (I don’t like the way my towers look). Need additional map backgrounds, towers, effects (arrows, cannons, explosions, etc). Any one want to help out? :)
2. Need a lot of work on game balance to make each level of difficulty appropriate to the level.
3. Going to change how you place towers on the map, it’s a little hard right now using the small mobile device.
4. Need to add functionality to upgrade and sell towers.

Screen Shots:

image image

Thanks,

–Mike

Leave a Comment
post icon

Win7 Mobile Development Tools

If you haven’t installed the Visual Studio Mobile tools for building Win7 applications I would highly recommend you do so now via one of the following links:

The release notes can be found here: Release Notes

Programming Resources: http://charlespetzold.com/phone/index.html

Channel 9 Training: http://channel9.msdn.com/learn/courses/WP7TrainingKit/

Windows Phone Developer Forums: http://social.msdn.microsoft.com/Forums/en-US/windowsphone7series

XNA Creators Club: http://creators.xna.com/en-US/

image

Pre-Requites: Uninstall any non-RTM versions of VS 2010.

Thanks,
–Mike

Leave a Comment