Windows Phone development and learning-loopingselector

Source: Internet
Author: User
Tags integer numbers

In fact, loopingselector has been mentioned in the previous custom datetimepicker. This is a control with a scroll effect. It is more beautiful than the list control, and the user experience is much better. This control is not available in the control provided by Microsoft. It is available in Silverlight. download the latest toolkit and reference it.

xmlns: toolkit = "clr-namespace: Microsoft.Phone.Controls.Primitives; assembly = Microsoft.Phone.Controls.Toolkit"
You can add loopingselector in xaml

<toolkit: LoopingSelector Grid.Column = "0" x: Name = "selectorDouble" ItemMargin = "2,3,3,2" ItemSize = "100,100" FontSize = "33" />
The
However, many times, I find that the loopingselector displays simple integer numbers, which is the case in the date control. Obviously this is not enough. We should let him display floating-point types, string types, and even include pictures, effects. Just like below

These materials are actually said on the Internet, WP7 LoopingSelector in depth gives a detailed implementation process, see here: http://windowsphonegeek.com/articles/WP7-LoopingSelector-in-depth--Part3-Advanced-data-binding

For my current project, I only need to display the month and days in the lunar calendar. I refer to the author's source code and find that the implementation is very simple. I don't even need to create two separate classes like the previous custom datetimepicker control. , These classes could have been put together, or even for any type, so it is a matter of course to implement floating point type or string, picture, etc.

The most important thing is the following two functions, everything else is easy to handle

// abstract the reusable code in a base class
// this will allow us to concentrate on the specifics when implementing deriving looping data source classes
public abstract class LoopingDataSourceBase: ILoopingSelectorDataSource
{
private object selectedItem;

#region ILoopingSelectorDataSource Members

public abstract object GetNext (object relativeTo);

public abstract object GetPrevious (object relativeTo);

public object SelectedItem
{
get
{
return this.selectedItem;
}
set
{
// this will use the Equals method if it is overridden for the data source item class
if (! object.Equals (this.selectedItem, value))
{
// save the previously selected item so that we can use it
// to construct the event arguments for the SelectionChanged event
object previousSelectedItem = this.selectedItem;
this.selectedItem = value;
// fire the SelectionChanged event
this.OnSelectionChanged (previousSelectedItem, this.selectedItem);
}
}
}

public event EventHandler <SelectionChangedEventArgs> SelectionChanged;

protected virtual void OnSelectionChanged (object oldSelectedItem, object newSelectedItem)
{
EventHandler <SelectionChangedEventArgs> handler = this.SelectionChanged;
if (handler! = null)
{
handler (this, new SelectionChangedEventArgs (new object [] {oldSelectedItem}, new object [] {newSelectedItem}));
}
}

#endregion
}

public class ListLoopingDataSource <T>: LoopingDataSourceBase
{
private LinkedList <T> linkedList;
private List <LinkedListNode <T >> sortedList;
private IComparer <T> comparer;
private NodeComparer nodeComparer;

public ListLoopingDataSource ()
{
}

public IEnumerable <T> Items
{
get
{
return this.linkedList;
}
set
{
this.SetItemCollection (value);
}
}

private void SetItemCollection (IEnumerable <T> collection)
{
this.linkedList = new LinkedList <T> (collection);

this.sortedList = new List <LinkedListNode <T >> (this.linkedList.Count);
// initialize the linked list with items from the collections
LinkedListNode <T> currentNode = this.linkedList.First;
while (currentNode! = null)
{
this.sortedList.Add (currentNode);
currentNode = currentNode.Next;
}

IComparer <T> comparer = this.comparer;
if (comparer == null)
{
// if no comparer is set use the default one if available
if (typeof (IComparable <T>). IsAssignableFrom (typeof (T)))
{
comparer = Comparer <T> .Default;
}
else
{
throw new InvalidOperationException ("There is no default comparer for this type of item. You must set one.");
}
}

this.nodeComparer = new NodeComparer (comparer);
this.sortedList.Sort (this.nodeComparer);
}

public IComparer <T> Comparer
{
get
{
return this.comparer;
}
set
{
this.comparer = value;
}
}

public override object GetNext (object relativeTo)
{
// find the index of the node using binary search in the sorted list
int index = this.sortedList.BinarySearch (new LinkedListNode <T> ((T) relativeTo), this.nodeComparer);
if (index <0)
{
return default (T);
}

// get the actual node from the linked list using the index
LinkedListNode <T> node = this.sortedList [index] .Next;
if (node == null)
{
// if there is no next node get the first one
node = this.linkedList.First;
}
return node.Value;
}

public override object GetPrevious (object relativeTo)
{
int index = this.sortedList.BinarySearch (new LinkedListNode <T> ((T) relativeTo), this.nodeComparer);
if (index <0)
{
return default (T);
}
LinkedListNode <T> node = this.sortedList [index] .Previous;
if (node == null)
{
// if there is no previous node get the last one
node = this.linkedList.Last;
}
return node.Value;
}

private class NodeComparer: IComparer <LinkedListNode <T >>
{
private IComparer <T> comparer;

public NodeComparer (IComparer <T> comparer)
{
this.comparer = comparer;
}

#region IComparer <LinkedListNode <T >> Members

public int Compare (LinkedListNode <T> x, LinkedListNode <T> y)
{
return this.comparer.Compare (x.Value, y.Value);
}
#endregion
}
Then we can set the data source for the loopingselector when the page is initialized, and then bind it to OK. In the author's example, the method of defining all and adding one at a time is given.

public MainPage ()
{
InitializeComponent ();
string [] cityNames = new string [] {"London", "New York", "Barcelona", "Madrid", "Berlin", "Bonn", "Munich", "Las Vegas"};
this.selectorString.DataSource = new ListLoopingDataSource <string> () {Items = cityNames, SelectedItem = "Madrid"};

List <int> numbers = new List <int> {1, 2, 3, 4, 5, 6, 7, 8, 9};
this.selectorInt.DataSource = new ListLoopingDataSource <int> () {Items = numbers, SelectedItem = 5};

List <DateTime> dates = new List <DateTime> ();
DateTime selectedDate = DateTime.Now;
dates.Add (selectedDate);
dates.Add (DateTime.Now.AddMonths (1));
dates.Add (DateTime.Now.AddMonths (2));
dates.Add (DateTime.Now.AddMonths (3));
dates.Add (DateTime.Now.AddMonths (4));
dates.Add (DateTime.Now.AddMonths (5));
dates.Add (DateTime.Now.AddMonths (6));
this.selectorDateTime.DataSource = new ListLoopingDataSource <DateTime> () {Items = dates, SelectedItem = selectedDate};

List <double> doubleNumbers = new List <double> {1.1, 2.15, 3.66, 4.457, 5.036, 6.7, 7.4, 8.54, 9.11};
this.selectorDouble.DataSource = new ListLoopingDataSource <double> () {Items = doubleNumbers, SelectedItem = 6.7};
}
The final effect is as follows

In fact, I don't think it is necessary to make a user control, it is more convenient to make a page. The datetimepicker included in the toolkit is actually a page. Instead of making a control and then putting a control on it, it is better to implement it in one step.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.