Polska Brain Jelly

This Will Be Boring, I Promise

by on Dec.18, 2011, under Brain Drain

I’m writing a Windows Phone 7 app and I wanted to implement a basic number chooser, but in the style of the date picker or time picker.  For those familiar with the Metro UI, it’s basically like a slot machine dial that spins vertically with the available choices.  An example can be found here.

So I found a reference to the toolkit that I needed and installed it.  Still didn’t have an option to use the control that I wanted (the loopingselector control).

Turns out I hadn’t opened up Visual Studio since Mango came out, and it wasn’t updated to use the 7.1 SDK.  Okay, so I get that updated and convert my solution to 7.1.  Now I can actually access the control.

However it requires some work to create the data source to use for the number range.  There are several examples online of how to create the data source.  That’s great, but they are all in  C#.

I’m writing this in VB.net.

My C# skills are rusty at best.  I do manage to translate everything over and now have a working looping selector that I can specify the range using the control’s xmls parameters.  My code example is below:


Public Class NumbersDataSource
        Implements ILoopingSelectorDataSource

        Private minimum As Integer = 1
        Private maximum As Integer = 32
        Private userSelectedItem As Integer = 1

        Public Function GetNext(relativeTo As Object) As Object Implements Microsoft.Phone.Controls.Primitives.ILoopingSelectorDataSource.GetNext

            Dim nextValue = CInt(relativeTo) + 1
            Return If(nextValue <= MaximumNumber, nextValue, MinimumNumber)

        End Function

        Public Function GetPrevious(relativeTo As Object) As Object Implements Microsoft.Phone.Controls.Primitives.ILoopingSelectorDataSource.GetPrevious

            Dim previousValue = CInt(relativeTo) - 1
            Return If(previousValue >= MinimumNumber, previousValue, MaximumNumber)

        End Function

        Public Property SelectedItem As Object Implements Microsoft.Phone.Controls.Primitives.ILoopingSelectorDataSource.SelectedItem
            Get
                Return userSelectedItem
            End Get
            Set(value As Object)
                Dim oldValue = userSelectedItem
                Dim newValue = CInt(value)

                If (oldValue = newValue) Then
                    Return
                End If

                userSelectedItem = newValue

                RaiseEvent SelectionChanged(Me, New SelectionChangedEventArgs({oldValue}, {newValue}))
            End Set
        End Property

        Public Property MinimumNumber As Integer

            Get
                Return minimum
            End Get
            Set(value As Integer)
                minimum = value
                If (userSelectedItem < minimum) Then
                    SelectedItem = value
                End If
            End Set
        End Property

        Public Property MaximumNumber As Integer

            Get
                Return maximum
            End Get
            Set(value As Integer)
                maximum = value
                If (userSelectedItem > maximum) Then
                    SelectedItem = value
                End If
            End Set
        End Property

        Public Event SelectionChanged(sender As Object, e As System.Windows.Controls.SelectionChangedEventArgs) Implements Microsoft.Phone.Controls.Primitives.ILoopingSelectorDataSource.SelectionChanged

    End Class

So, as I promised, pretty boring stuff. But now I have it here in case I need it again, and it might help some other poor soul looking for a vb.net implementation of the looping selector data source.


Leave a Reply

You must be logged in to post a comment.

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Blogroll

A few highly recommended websites...