1 0
post icon

How to Convert an Enum to its String Value

Getting the string value of any given enum is as simple as:

Electronic electronic = Electronic.Breakbeat;
 
// value = "Breakbeat";
string value = electronic.ToString();   


Where Electronic is an Enum declared like this:

public enum Electronic
{
    AcidHouse,
    Ambient,
    BigBeat,
    Breakbeat,
    Dance,
    Demo,
    Disco,
    Downtempo,
    DrumandBass,
    Electro,
    Garage,
    HardHouse,
    Houe,
    IDM,
    Jungle,
    Progressive,
    Techno,
    Trance,
    Tribal,
    TripHop,
}

Also, if you want to get the count of items in an enum in Silverlight you will have to create your version of Enum.GetValues so this method is not included in the framework for the Win7 Mobile.

public static IEnumerable<T> GetValues<T>()
{
    Type type = typeof(T);
 
    if (!type.IsEnum)
    {
        //  throw Exception
    }
 
    FieldInfo[] fields = type.GetFields(BindingFlags.Public | BindingFlags.Static);
 
    foreach (var item in fields)
    {
        yield return (T)item.GetValue(null);
    }
}
 
int length = GetValues<Electronic>().Count();
 
Oh, and Merry Christmas everyone!!
 
 

Thanks,

–Mike

Leave a Comment
25. Dec, 2010
post icon

Win7 Image Client for Bing, FlickR & Google

I recently published a Win7 mobile phone version of my web-based Galactic Image application that is found here: http://www.galacticimg.com.

To find it search for “Zynpo Image Browser” on the Marketplace or use this direct link to open it in Zune on your PC:

http://social.zune.net/redirect?type=phoneApp&id=bd24da58-c9f6-df11-9264-00237de2db9e

Similar to the web version, the mobile version allows you to search and browse for images using engines such as Google, Bing or image providers link Flickr.com. It also allows you to browse images stored on your phone and you can save images you find on the Internet directly to your phone. Finally, double click any image to go into full screen browsing mode.

I would love to hear your feedback (the good, the bad and the ugly). Also, what features would you like supported in addition to what’s already supported? What would you have done differently?

image image

imageimage

Thanks,
–Mike

Leave a Comment
23. Dec, 2010
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
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