1 0 Tag Archives: silverlight
post icon

Selecting an Item in a ComboBox after Adding Items

When adding items to a Combobox dynamically you might notice that setting an items IsSelected = true directly aftewards fails to work.

For example, this fails to select the first item in the ComboBox:

ComboBoxItem cbi;
 
foreach (string category in Categories)
{
    cbi = new ComboBoxItem();
    cbi.Content = category;
   CategoryCB.Items.Add(cbi);
}
 
if (CategoryCB.Items.Count > 0)
{
    cbi = (ComboBoxItem)CategoryCB.Items[0];
    cbi.IsSelected = true;
}

The way to do this is via the SelectedIndex property off your ComboBox control:

if (CategoryCB.Items.Count > 0)
{
    CategoryCB.SelectedIndex = 0;
}

Thanks,

–Mike

Leave a Comment
post icon

Detecting Users Win7 Mobile Theme Color

If you want your Win7 mobile application to mimic the theme/system colors your users phone is using you can do so through the the use of the Theme Resources.

For example, the color of each of the tiles in your phone is called the “Accent” color. To obtain this color simply call:

SolidColorBrush brush=Application.Current.Resources["PhoneAccentBrush"] as SolidColorBrush;

Background and Foreground colors:

background = Application.Current.Resources["PhoneBackgroundBrush"] as SolidColorBrush;
foreground = Application.Current.Resources["PhoneForegroundBrush"] as SolidColorBrush;


For more details on the other colors available see: http://msdn.microsoft.com/en-us/library/ff769552(VS.92).aspx

Thanks,

–Mike

Leave a Comment
post icon

Funny Sounds, Radio Streams & Japanese – Win7 Mobile Apps

Well, I am having a lot of fun on my vacation creating applications in Silverlight for the Win7 mobile phone.

My latest published application allows you to play a variety of sounds including some funny, obnoxious ones that will find some good laughs at a party.

The application is called “Funny Sounds” and can be downloaded via the marketplace. Direct Zune link: http://social.zune.net/redirect?type=phoneApp&id=10fb5adc-390b-e011-9264-00237de2db9e

Let me know if there are any sounds you would like to have in this app to make your friends laugh!

I have two other applications I am about to publish soon next week.

The first is a program that allows you to listen to radio stations that support SHOUTCast streams. This application allows you to pick a genre and a sub-genre to help you find the radio station that is right for you. Screenshots:

image image

Finally, if you are interested in learning Japanese you will love my next application. Titled “PeraPera” (which means “fluent” in Japanese), this application allows you to study Kanji, Hiragana, Katakana as well as vocabulary. Before I got into Computer Science, Japanese was my passion and I spent over 5 years in Japan studying Japanese. I have always wanted to make a program like this and the Win7 Mobile Platform was the perfect place. Couple sneak preview screenshots below:

image image

Thanks,
–Mike

Leave a Comment
post icon

Playing sound effects on Windows Phone 7

To play sounds on the Windows Phone 7 you can make use of the SoundEffect library that comes with XNA even if you are building an application in Silverlight.

Start by adding a reference to Microsoft.Xna.Framework.dll.

Next, add the following using statements to your file:

using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework;


This class supports using WAV files. If you have an MP3 file, you can convert it to a WAV using a conversion tool (there are tons of free tools on the Internet).

Next, add your wav file to your project. Finally, open the stream to the WAV file using TitleContainer.OpenStream() and create a SoundEffect from that stream. In the example below, I am opening a wave file called crickets.wav that is located in my folder called sounds.

Stream stream = TitleContainer.OpenStream("sounds/crickets.wav");
SoundEffect effect = SoundEffect.FromStream(stream);
FrameworkDispatcher.Update();
effect.Play();


In general wave files are much larger than compressed MP3 files. On the phone, you can play one Mp3 at a time using the MediaElement object. When creating your MediaElement, make certain you add it to the Silverlight tree if it’s not statically declared otherwise it will not load.

Also, when you deploy the app to your actual phone device through VS 2010 and Zune make certain you unplug or close Zune first before attempting to play sound otherwise they will fail to load. You can play sounds without this problem if you run your app in the emulator.

The following code gives and example of playing sound through the MediaElement:

MainPage.xaml:

<MediaElement x:Name="MainME" AutoPlay="False" MediaOpened="MainME_MediaOpened" MediaFailed="MainME_MediaFailed"></MediaElement>

MainPage.xaml.cs:

private void SoundBtn_Click(object sender, RoutedEventArgs e)
{
    MainME.Source = new Uri("sounds/mySound.mp3", UriKind.Relative);
    MainME.Position = new TimeSpan(0);
}
 
private void MainME_MediaOpened(object sender, RoutedEventArgs e)
{
    MainME.Play();
}


Thanks,

–Mike

Leave a Comment
post icon

Forward Navigation on the Windows Phone

Just a quick note to state that currently there is no forward navigation stack on the windows 7 mobile phone. Therefore, if you call this.NavigationService.CanGoForward you will notice this values always returns false. This is currently By Design.

Navigating back works as expected:

private void BackBtn_Click(object sender, EventArgs e)
{
    if (this.NavigationService.CanGoBack)
        this.NavigationService.GoBack();
}


Thanks,

–Mike

Leave a Comment