全部博文(403)
2012年(403)
分类: 嵌入式
2012-03-04 11:11:51
跟林永坚老师学习wp7
wp7内置的启动器
wp7内置的选择器
启动器的使用时的注意事项:
声明一个Chooser:
PhotoChooserTask photoChooserTask;
NeW一个Chooser的实例:并注册CompleteD事件
public MainPage()
{
InitializeComponent();
photoChooserTask = new PhotoChooserTask();
photoChooserTask.Completed += new EventHandler
} |
实现Completed事件:
void photoChooserTask_Completed(object sender, PhotoResult e)
{
if (e.TaskResult == TaskResult.OK)
{
MessageBox.Show(e.ChosenPhoto.Length.ToString());
System.Windows.Media.Imaging.BitmapImage bmp = new System.Windows.Media.Imaging.BitmapImage();
bmp.SetSource(e.ChosenPhoto);
myImage.Source = bmp;
}
} |
总结:启动器和选择器的最明显区别就是启动器没有返回任何信息,所以不需要注册Completed的事件,而选择器则会返回一些信息,所以需要注册Completed的事件,他们都需要先NEw一个实例,然后配置参数,最后SHOw()就可以实现调用;
后台完整代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;
namespace LaunchersAndChoosers
{
public partial class MainPage : PhoneApplicationPage
{
PhotoChooserTask photoChooserTask;
SavePhoneNumberTask savePhoneNumberTask;
// Constructor
public MainPage()
{
InitializeComponent();
photoChooserTask = new PhotoChooserTask();
photoChooserTask.Completed += new EventHandler
savePhoneNumberTask = new SavePhoneNumberTask();
savePhoneNumberTask.Completed += new EventHandler
}
void savePhoneNumberTask_Completed(object sender, TaskEventArgs e)
{
switch (e.TaskResult)
{
//Logic for when the number was saved successfully
case TaskResult.OK:
MessageBox.Show("Phone number saved.");
break;
//Logic for when the task was cancelled by the user
case TaskResult.Cancel:
MessageBox.Show("Save cancelled.");
break;
//Logic for when the number could not be saved
case TaskResult.None:
MessageBox.Show("Phone number could not be saved.");
break;
}
}
void photoChooserTask_Completed(object sender, PhotoResult e)
{
if (e.TaskResult == TaskResult.OK)
{
MessageBox.Show(e.ChosenPhoto.Length.ToString());
System.Windows.Media.Imaging.BitmapImage bmp = new System.Windows.Media.Imaging.BitmapImage();
bmp.SetSource(e.ChosenPhoto);
myImage.Source = bmp;
}
}
private void button1_Click(object sender, RoutedEventArgs e)
{
SearchTask tastk = new SearchTask();
tastk.SearchQuery = "littlefeihu";
tastk.Show();
}
private void button2_Click(object sender, RoutedEventArgs e)
{
PhoneCallTask task = new PhoneCallTask();
task.PhoneNumber = "123546879";
task.DisplayName = "littlefeihu";
task.Show();
}
private void button3_Click(object sender, RoutedEventArgs e)
{
photoChooserTask.Show();
}
private void button4_Click(object sender, RoutedEventArgs e)
{
try
{
savePhoneNumberTask.PhoneNumber = "2065550123";
savePhoneNumberTask.Show();
}
catch (System.InvalidOperationException ex)
{
MessageBox.Show("An error occurred.");
}
}
}
} |
前段Xaml代码:
x:Class="LaunchersAndChoosers.MainPage"
xmlns=""
xmlns:x=""
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d=""
xmlns:mc=""
mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True">
|
对于应用程序事件的跟踪,我们可以通过VS自带的OutPut窗口来观察,方便好用
比如我们在后台写上如下的跟踪代码:
打开output窗口,F5启动调试,如下图我们就跟踪到了事件的调用顺序等信息: