Most applications need a settings page to allow a user to configure how they wish to use your application. In this tutorial we’ll start to look at making the Translation Direction configurable. We’ll shortly be adding in a Settings page, but there’s some groundwork required before we begin.
Outcome
By the end of this tutorial the Translation Direction Attribute should have a custom Display Attribute associated to it, allowing for the associated display string to be displayed on the view via a Value Converter, which will look up the display text for a given enum value.
Prerequisites
- Morse Coder Part 12 (or a fork on GitHub);
- Visual Studio 2015 Community Edition;
- Windows 10.
Implementation
Custom Attribute
Start by creating an Attributes Folder in the MorseCoder.PCL Project and create a new class called DisplayAttribute.
using System; namespace MorseCoder.PCL.Attributes { public class DisplayAttribute : Attribute { public string DisplayString { get; private set; } public DisplayAttribute(string displayString) { DisplayString = displayString; } } }
What does this do?
- Gives our Attribute a constructor to allow us to pass in values to it in order to define properties.
- Gives our Attribute a property which we can retrieve information from. Note the public ‘getter’ and private ‘ setter’.
- Assigns the property a value from the constructor.
Enum
Let’s modify our TranslationDirection enum to include the new Display Attribute, allowing us to define the text associated with the currently selected TranslationDirection.
Let’s decorate our enum with our new attribute. By convention, we don’t need to provide the Attribute part of the class name when using the attribute.
Note the use of the constructor we just created.
public enum TranslationDirection { [Display("Morse to Alphabet")] MorseToAlphabet, [Display("Alphabet to Morse")] AlphabetToMorse, }
Converter
We can now create a converter to present a display string to the view based on the TranslationDirection being bound against, very similar to the TranslationDirectionToMorseInputVisibilityConverter class we created earlier.
Create a TranslationDirectionToDisplayStringConverter class and inherit from IValueConverter. As before we’ll assert correct type is being used, then we can retrieve our new DisplayAttribute from our TranslationDirection object.
public object Convert(object value, Type targetType, object parameter, string language) { if (value is TranslationDirection) { var type = value.GetType(); var member = type.GetMember(value.ToString()); var attributes = member.First().GetCustomAttributes(typeof(DisplayAttribute), false); return attributes.First(); } return new InvalidCastException("Input value not of type TranslationDirection"); }
What’s this doing?
- Retrieving an array of MemberInfo objects where the Member name matches our enumeration value;
- Retrieving the Attributes associated with our enumeration value;
- Returning the first attribute in the array (would probably be more careful using this in a real application!).
Summary
Where are we?
- We created a custom Display Attribute;
- We decorated the Translation Direction enumeration with our new attribute;
- We created a converter to convert between the enumeration value and the display string.
We’ll look at:
- Creating an Settings ViewModel and View;
- methods of serialising POCOs to Application Settings;
- MVVM Light Messaging.
One thought on “Universal App Tutorials Part 13 : Custom Enum Attributes”