WP7 Creating a custom chooser/task
Anyone have a clue how to go about creating a custom chooser? Basically what I want is to navigate to a page, select some sorta of data on that page and return an object via some EventArg.
Something similar to how the Tasks/choosers work in WP7 now where I can do:
CustomChooserTask task = new CustomChooserTask();
task.Completed += new EventHandler<CustomResult>(task_Completed);
t开发者_如何学JAVAask.Show();
A chooser is not the right approach for what you are trying to do.
Tasks/Launchers/Choosers are a means for your app to interact with the the core functionality of the phone while allowing the user to be clear about what is happening.
As you're not interacting with the core functionality or data of the phone, creating something which tries to mimic it may be confusing.
A simple approach to your requirement would be to have a global variable (or equivalent) and have the launched (picker) page populate that variable. On returning to the original (requesting) page it could check the global variable. Unfortunately there's no way to pass data back between pages in that way. (At least without getting very creative your backstack manipulation.)
Alternatively, you could look at how the ListPicker
works in the Toolkit and implement something like that.
I know this is a little old and other have said this isn't a good idea. But I believe there are times when being able to implement a choosertask is handy. One obvious example to me is if you want the user to select a photo from there library, take a picture, or draw something on the screen.
These all need to return the same result but drawing your own doesn't have a chooser. So implementing one would be nice to keep your code simple. you have a case statement that tell what chooser to activate.
anyway here is some code i wrote to create a custom choosertask (it's not 100% complete, but it should give you an idea):
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Tasks;
using System.Windows.Controls.Primitives;
namespace tasks {
public class imageResult : TaskEventArgs {
public System.IO.Stream image { get; set; }
}
public class imageChooserTask : ChooserBase<imageResult> {
public override void Show() {
Popup p = new Popup();
p.IsOpen = true;
imageSelector cs = new imageSelector();
p.Child = cs;
p.Closed += new EventHandler(p_Closed);
}
void p_Closed(object sender, EventArgs e) {
Popup p = sender as Popup;
TaskResult tr = (TaskResult)p.Tag;
//some logic to add stream
FireCompleted(sender, new imageResult(), null);
}
}
}
精彩评论