Click here to Skip to main content
15,882,017 members
Articles / Mobile Apps / Windows Phone 7

Wp7nl Utilities Contribution

Rate me:
Please Sign up or sign in to vote.
4.53/5 (5 votes)
4 Jul 2013CPOL4 min read 7.4K   3  
Wp7nl utilities contribution

When building apps for Windows Phone, I use my own library. I’m a “lazy” developer and don’t like writing code twice. So I place a lot of code that can be reused, like behaviors, converters and extension methods, in my library. For I while, I’ve been thinking about sharing the library as an open source project on Codeplex, but in the end decided to contribute to the Wp7nl Utilities project of Joost van Schaik. The first bunch of utilities are added. I will continue to add more and describe those on my blog too.

Actions for Launchers

behaviorsI often have simple trigger actions that need to be executed in my apps. For example, opening a URL in Internet Explorer or sending an email. Because I use Blend a lot (and I’m lazy ;) ), I just like to drag and drop an action to a button or text block and be done with it. The great thing about actions and behaviors is that you can bind the properties which makes them very useful in templates.

At this point, only a few of the possible Windows Phone Launchers are available as trigger actions, but I’m sure the list will grow over time. If you would like one added, just let me know and I’ll see what I can do.

NavigateToUrlAction

Using these actions is very simple. The code surrounding the action is pretty much the same for all actions, only the event name might change. The NavigateToUrlAction performs a WebBrowserTask. It opens up the Url property so you can bind to it. As a small extra feature, it checks if a network connection is available and shows a message if there isn’t. This message can be customized as well.

XML
<Button Content="Wp7nl" HorizontalAlignment="Left" VerticalAlignment="Top">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Tap">
            <behaviors:NavigateToUrlAction Url="http://wp7nl.codeplex.com"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
</Button>

SendEmailAction

This action executes the EmailComposeTask. The “To”, “Subject” and “Body” properties can be bound to.

NavigatieToMarketplaceDetailAction

This action executes the MarketPlaceDetailTask. It opens up the marketplace and shows the details of your app.

NavigatieToReviewAction

Getting people to review your apps is crucial for a successful app. This action executes the MarketplaceReviewTask and makes it very easy for users to review.

ShareLinkAction

Sharing links is a very common task in apps. This action executes the ShareLinkTask. So you can use the native sharing features of the phone. The “LinkUri”, “Message” and “Title” properties can be bound to.

Limit TextBox Behavior

I ran into cases where I wanted to limit the number of characters a user can enter. This behavior an only be used with a TextBox element. You can set the maximum number characters that can be entered in the “MaxChars” property. As a bonus feature, you can have the phone vibrate a little when the maximum number of characters is reached.

XML
<TextBox x:Name="LimitedTextbox">
    <i:Interaction.Behaviors>
        <behaviors:LimitTextBoxBehavior MaxChars="25" Vibrate="True"/>
    </i:Interaction.Behaviors>
</TextBox>

Visible On Locale Behavior

The VisibleOnLocaleBehavior sets the attached element to visible if the current UI language of the phone matches the specified two letter ISO language name and hides it when it doesn’t. The behavior can be used if you have certain element, like images, that are language specific.

XML
<TextBlock Text="English" >
    <i:Interaction.Behaviors>
        <behaviors:VisibleOnLocaleBehavior LanguageCode="en"/>
    </i:Interaction.Behaviors>
</TextBlock>
<TextBlock Text="Nederlands">
    <i:Interaction.Behaviors>
        <behaviors:VisibleOnLocaleBehavior LanguageCode="nl"/>
    </i:Interaction.Behaviors>
</TextBlock>

Visibility In Trial Behavior

The VisibilityInTrialBehavior sets the attached element to visible or collapsed if the app is in trial mode. Whether the attached element should be visible or collapsed is determined by the “Visibility” property.

XML
<TextBlock Text="Not in trial mode">
    <i:Interaction.Behaviors>
        <behaviors:VisibilityInTrialBehavior Visibility="Collapsed"/>
    </i:Interaction.Behaviors>
</TextBlock>

This behavior makes use of the TrialHelper. This helper is used to see if your app is running in trial mode. To be able to run your application in trial mode, you can add “TRIAL” as a conditional compilation symbol to fake it when debugging.

image

Converters

I’ve added a couple of converters to the Wp7nl library. Most of which are very simple.

ObjectToStringValueConverter

Let’s start with the one that needs the most explanation. The ObjectToStringConverter is used to create a nicer representation of an object. Add properties of the bound object between { } to the converter parameters.

Take, for example, a Person class.

C#
public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

Whenever you would like to have a textblock show the last name and the first name separated with a comma, you could just add that to the converter parameter.

For this example, I’ve set the datacontext of the mainpage to a new instance of the person class directly in the constructor. You wouldn’t do that in a real application of course, but you’ll get the idea.

C#
public MainPage()
{
    InitializeComponent();
    DataContext = new Person{FirstName = "Larry", LastName = "Laffer"};
}

Then, I’ve added a databinding to the textblock.

XML
<TextBlock Text="{Binding ConverterParameter=\{LastName\}\, \{FirstName\}, 
   Converter={StaticResource ObjectToStringValueConverter}}"/>

When creating the binding in Blend, you don’t have to escape the curly brackets, as shown in the image below:

image

NullToCollapsedConverter

Returns Visibility.Collapsed for any value that is null, empty, only whitespaces or an empty guid. Very useful to hide empty stuff. You don’t want to show a navigate to URL button if the URL is empty, for example.

StripHtmlValueConverter

Removes all HTML from a string and decodes ‘&XXXX’ to regular chars. Often when data is requested from external services like Twitter or Facebook, it contains HTML at some point. This HTML has to be converted to either a regular string or to some sort of rich text block. This converter does the first, removing all HTML. <BR /> tags are changed to new lines.

ToLowerValueConverter

Converts a string to all lowercase characters. Useful for headers.

ToUpperValueConverter

Converts a string to all uppercase characters. Useful for titles.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Velicus B.V.
Netherlands Netherlands
Microsoft MVP Client Dev . Founder of http://StoreAppsUG.nl, the Dutch Windows Store apps and Windows Phone apps usergroup. XAML / HTML5 developer. Writer. Composer. Musician.

Twitter
@Sorskoot

Awards / Honers
• October 2010,2011,2012,2013: Awarded Microsoft Expression Blend MVP
• June 2009: Second Place in the WinPHP challenge
• February 2009: Runner-up in de Mix09 10k Challenge
• June 2008: Winner of the Microsoft expression development contest at www.dekickoff.nl

Bio
I started programming around 1992, when my father had bought our first home computer. I used GWBasic at that time. After using QBasic and Pascal for a few years I started to learn C/C++ in 1996. I went to the ICT Academy in 1997 and finnished it in 2002. Until December 2007 I worked as a 3D specialist. Besides modelling I worked on different development projects like a 3D based Scheduler and different simultion tools in C# and Java. Though out the years I've gained much experience with ASP.NET, Silverlight, Windows Phone and WinRT.

Comments and Discussions

 
-- There are no messages in this forum --