1 0 Tag Archives: silverlight
post icon

Win7 Mobile Application Bar – AG_E_PARSER_BAD_PROPERTY_VALUE

I recently uncommented the code that is there (commented out by default) which allows you to display and application bar in your Win7 Mobile applications:

<phone:PhoneApplicationPage.ApplicationBar>
    <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
        <shell:ApplicationBarIconButton IconUri="/Images/button1.png" Text="Button 1"/>
        <shell:ApplicationBarIconButton IconUri="/Images/button2.png" Text="Button 2"/>
        <shell:ApplicationBar.MenuItems>
            <shell:ApplicationBarMenuItem Text="Menu Item 1"/>
            <shell:ApplicationBarMenuItem Text="Menu Item 2"/>
            <shell:ApplicationBarMenuItem Text="Menu Item 3"/>
            <shell:ApplicationBarMenuItem Text="Menu Item 4"/>
        </shell:ApplicationBar.MenuItems>
    </shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>

 

However, when I ran the application I got the infamous error: AG_E_PARSER_BAD_PROPERTY_VALUE

The problem was I had taken the event callbacks from my buttons in the main grid and moved them to the buttons in the application bar. These event signatures are different though and as a result you will get the error stated above. Notice the difference below, one uses EventArgs the other uses RoutedEventArgs.

Button event signature from main grid:

private void Button_Click(object sender, RoutedEventArgs e)
{
}

 

Button event signature from application bar:

private void Button_Click(object sender, EventArgs e)
{
}

 

Thanks,

–Mike

Leave a Comment
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 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

Silverlight Tip of the Day #32 – BiDi – Bi-Directional Text

BiDi or Bi-directional text is text that flows right-to-left (RTL) and left-to-right (LTR). For example, while English flows left to right other languages such as Arabic, Hebrew and Persian scripts flow right to left. Chinese characters can also be written in either RTL or LTR directions.

Silverlight supports BiDi through a property called FlowDirection. This property can be set to either RightToLeft or the default LeftToRight.

The following code below demonstrates this property in action:

<TextBlock FlowDirection="RightToLeft" Text="نصائح Silverlight من اليوم"/>
<TextBlock FlowDirection="RightToLeft" Text="Silverlight 每日提示"/>
<TextBlock FlowDirection="LeftToRight" Text="Silverlight Tips of the Day"/>

 

The first two TextBlocks flow RTL and the last one flows LTR.

Demo:

[silverlight: Tip32_BIDI.xap]

Note that this property can also be applied to other controls such as RichTextBox, ListBox, TextBox, etc.

Source: Tip32_BIDI.zip

Thanks,

–Mike

Leave a Comment
post icon

Silverlight Tip of the Day #31 – Pinning Full Screen Mode

Full screen can be toggled on and off via a user command such as a button click by setting the following property to true or false: Application.Current.Host.Content.IsFullScreen

Normally Silverlight applications will only stay in full screen mode until a user hits the <ESC> key or until the application loses focus. For example, if the user has multiple monitors and they click on another application in another monitor this will cause the Silverlight application to return to windowed mode. If you want to prevent your Silverlight application from leaving full screen mode in this case you can do so by setting:

Application.Current.Host.Content.FullScreenOptions = 
                 System.Windows.Interop.FullScreenOptions.StaysFullScreenWhenUnfocused;

 

Now, when a user clicks on another application in another monitor they will be prompted with the following dialog:

image

If the user clicks ‘Remember my answer’ and the “Yes” button they will only need to be prompted this one time.

In the following demo below I have added a button that toggles full screen mode as well as an event that will update the status text to show whether we are in full screen mode or not:

App.Current.Host.Content.FullScreenChanged += ((sender, args) =>
{
    if (App.Current.Host.Content.IsFullScreen)
    {
        ModeTB.Text = "Mode=Full Screen";
    }
    else
    {
        ModeTB.Text = "Mode=Windowed";
    }
});

 

Demo:

[silverlight: Tip31_Pinning.xap]

Thanks,

–Mike

Leave a Comment