?

Log in

No account? Create an account
Windows Phone: adding enum settings to your app - Greg [entries|archive|friends|userinfo]
Greg

[ website | gregstoll.com ]
[ userinfo | livejournal userinfo ]
[ archive | journal archive ]

Links
[Links:| * Homepage * Mobile apps (Windows Phone, Win8, Android, webOS) * Pictures * LJBackup * Same-sex marriage map * iTunesAnalysis * Where's lunch? ]

Windows Phone: adding enum settings to your app [Jan. 11th, 2013|09:10 pm]
Greg
[Tags|, ]
[Current Mood |sicksick]

After my last post about adding a settings page to your app, I thought I'd follow it up by adding an enum-like type to the page. Enums and booleans are (at least in my apps!) the most common types of settings, and enums are slightly trickier than the Boolean settings we saw last time.

For this example, I'm going to be adding a QuestionDirection "enum" to my settings page.

Step 1: Add the "enum" class

First, you need a QuestionDirection.cs:


using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;

namespace KnowYourStates.wp7
{
    public class QuestionDirection
    {
        public string Name { get; set; }
        public string DisplayName { get; set; }
        public bool HasForward { get; set; }
        public bool HasBackward { get; set; }
        private static QuestionDirection _forward = new QuestionDirection()
            { Name = "forward", DisplayName = "Forward", HasBackward = false, HasForward = true};
        private static QuestionDirection _backwards = new QuestionDirection()
            { Name = "backwards", DisplayName = "Backward", HasBackward = true, HasForward = false };
        private static QuestionDirection _both = new QuestionDirection()
            { Name = "both", DisplayName = "Forward + Backward", HasBackward = true, HasForward = true };
        private static Collection<QuestionDirection> _directions = null;
        public static QuestionDirection Default
        {
            get
            {
                return _both;
            }
        }
        public static Collection<QuestionDirection> Directions
        {
            get
            {
                if (_directions == null)
                {
                    _directions = new Collection<QuestionDirection>();
                    _directions.Add(_forward);
                    _directions.Add(_backwards);
                    _directions.Add(_both);
                }
                return _directions;
            }
        }
        public override string ToString()
        {
            return DisplayName;
        }

    }
}

Here the important parts are the Default property and the Directions property. (the ToString() method is necessary to make it look right in the ListPicker we're going to use, but how you implement it is up to you)

Step 2: Modify UserSettings.cs

In your UserSettings.cs (see the previous post for the necessary parts), add the following:


        const string QuestionDirectionKeyName = "QuestionDirection";
        QuestionDirection QuestionDirectionDefault;

        public QuestionDirection QuestionDirection
        {
            get
            {
                return GetValueOrDefault<QuestionDirection>(QuestionDirectionKeyName, QuestionDirectionDefault);
            }
            set
            {
                if (AddOrUpdateValue(QuestionDirectionKeyName, value))
                {
                    Save();
                }
            }
        }

        public Collection<QuestionDirection> AllQuestionDirections
        {
            get
            {
                return QuestionDirection.Directions;
            }
        }


and then to the constructor, add

                QuestionDirectionDefault = QuestionDirection.Default;


Step 3: Add a control to the Settings page

Here's the XAML for the Settings PivotItem:

<controls:PivotItem x:Name="SettingsPivotItem" Header="settings">
   <StackPanel Orientation="Vertical">
       <TextBlock Text="Question Mode:" FontSize="{StaticResource PhoneFontSizeLarge}"/>
       <toolkit:ListPicker ItemsSource="{Binding Source={StaticResource appSettings}, Path=AllQuestionDirections}"
           SelectedItem="{Binding Source={StaticResource appSettings}, Path=QuestionDirection, Mode=TwoWay}"/>
    </StackPanel>
</controls:PivotItem>

Note that the ListPicker is from the Windows Phone Toolkit, which is a (free!) must-have. It also requires you to have this inside the PhoneApplicationPage XML element:

    xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"


Just like in the previous post, you can listen for the appSettings changing, and you can access the current setting with UserSettings.Instance.QuestionDirection.


Again, this isn't really that difficult, but whenever I need to do it I find this code and copy it, so now you can too!

--

See all my Windows Phone development posts.

I'm planning on writing more posts about Windows Phone development - what would you like to hear about? Reply here, on twitter at @gregstoll, or by email at greg@gregstoll.com.
LinkReply