Sie sind auf Seite 1von 39

DONG NAI UNIVERSITY OF TECHNOLOGY

1
6. Extending the Photos Hub
5. Capturing photos
3. Acquiring a single photo
2. Search extensibility
1. Launchers and Choosers
7. Lenses
8. Sharing photos
4. Working with the media library
DONG NAI UNIVERSITY OF TECHNOLOGY
2
1. Launchers and Choosers
Both Launchers and Choosers are API mechanisms for
invoking existing apps and services on the phone. In the API,
theyre all called tasks.
The difference between Launchers and Choosers is that
Launchers start a feature but dont return a value, whereas
Choosers launch a feature and do return a value.
Invoking a Launcher or Chooser causes your app (the
invoking app) to be deactivated.
using Microsoft.Phone.Tasks;
DONG NAI UNIVERSITY OF TECHNOLOGY
3
1. Launchers and Choosers
User guide: Launchers for Windows Phone:
http://msdn.microsoft.com/en-
us/library/windowsphone/develop/ff769550(v=vs.105).aspx
User guide: Choosers for Windows Phone:
http://msdn.microsoft.com/en-
us/library/windowsphone/develop/ff769543(v=vs.105).aspx
DONG NAI UNIVERSITY OF TECHNOLOGY
4
1. Launchers and Choosers
Launchers
Task Description
BingMapsDirectionsTask Launches the Maps app, specifying a starting
and/or ending location, for which driving or
walking directions are displayed (identical to the
MapsDirectionsTask).
BingMapsTask Launches the Maps app centered at the specified or
current location (identical to the MapsTask)
ConnectionSettingsTask Launches a settings dialog with which the user can
change the devices connection settings
EmailComposeTask Composes a new email
MapDownloaderTask Provides a mechanism for the user to download
maps for offline use
MapUpdaterTask Provides a mechanism for the user to update map
data he has previously downloaded for offline use.
DONG NAI UNIVERSITY OF TECHNOLOGY
5
1. Launchers and Choosers
Launchers
Task Description
MapsDirectionsTask Launches the Maps app, specifying a starting
and/or ending location, for which driving or
walking directions are displayed (identical to
BingMapsDirectionsTask).
MapsTask Launches the Maps app centered at the specified or
current location (identical to BingMapsTask)
MarketplaceDetailTask Launches the Windows Phone Store and displays
the details for a specific app
MarketplaceHubTask Launches the Windows Phone Store and searches
for a particular type of content
MarketplaceReviewTask Displays the Windows Phone Store review page for
the current app
MarketplaceSearchTask Launches the Windows Phone Store and displays
the search results from the specified search terms.
DONG NAI UNIVERSITY OF TECHNOLOGY
6
1. Launchers and Choosers
Launchers
Task Description
MediaPlayerLauncher Launches Media Player, and plays the specified media
file
PhoneCallTask Initiates a phone call to a specified number.
SaveAppointmentTask Provides a mechanism for the user to save an
appointment from your app
SearchTask Launches Microsoft Bing Search with a specified
search term
ShareLinkTask Launches a dialog with which the user can share a link
on the social networks of her choice. If the user does
not have any social networks set up, the Launcher
silently fails
ShareMediaTask Provides a mechanism for your app to share a media
item with one of the media-sharing apps on the phone
DONG NAI UNIVERSITY OF TECHNOLOGY
7
1. Launchers and Choosers
Launchers
Task Description
ShareStatusTask Launches a dialog with which the user can share a
status message on the social networks of her choice.
If the user does not have any social networks set up,
the Launcher silently fails
SmsComposeTask Composes a new text message.
WebBrowserTask Launches Microsoft Internet Explorer and browses to
a specific URI
DONG NAI UNIVERSITY OF TECHNOLOGY
8
1. Launchers and Choosers
Choosers
Task Description
AddWalletItemTask Launches the Wallet app and allows the user to add
the supplied item to his wallet
AddressChooserTask Launches the Contacts app with which the user can
find an address.
CameraCaptureTask Opens the Camera app to take a photo
EmailAddressChooserTask Provides a mechanism for the user to select an
email address from his Contacts List.
GameInviteTask Shows the game invite screen with which the user
can invite players to a multiplayer game session.
PhoneNumberChooserTask Provides a mechanism for the user to select a
phone number from his Contacts List
DONG NAI UNIVERSITY OF TECHNOLOGY
9
1. Launchers and Choosers
Choosers
Task Description
PhotoChooserTask Provides a mechanism for the user to select an
image from his Picture Gallery or take a photo
SaveContactTask Launches the Contacts app with which the user can
save a contact.
SaveEmailAddressTask Saves an email address to an existing or new
contact.
SavePhoneNumberTask Saves a phone number to an existing or new contact
SaveRingtoneTask Launches the Ringtones app with which the user
can save a ringtone from your app to the system
ringtones list
DONG NAI UNIVERSITY OF TECHNOLOGY
10
1. Launchers and Choosers
The basic steps for using a Launcher are as follows:
1. Create an instance of the type that represents the
specific Launcher feature that you want to use.
2. Set properties on the object as appropriate.
3. Invoke the Show method
DONG NAI UNIVERSITY OF TECHNOLOGY
11
1. Launchers and Choosers
The basic steps for using a Launcher are as follows:
Simple Example for SMS message:
SmsComposeTask smsTask = new SmsComposeTask();
smsTask.To = "0987773061";
smsTask.Body = "Al Nm Mi ! mi hn nm c";
smsTask.Show();
DONG NAI UNIVERSITY OF TECHNOLOGY
12
1. Launchers and Choosers
The basic steps for using a Chooser are as follows:
1. Create an instance of the type that represents the specific
Chooser feature that you want to use. It is common to declare
this as a field in your page class.
2. Hook up the Completed event on the object (or provide an
inline delegate or lambda), which will be invoked when the
Chooser task completes.
3. Set properties on the object as appropriate.
4. Invoke the Show method.
5. In your Completed event handler or delegate, process the
return value from the Chooser.
DONG NAI UNIVERSITY OF TECHNOLOGY
13
1. Launchers and Choosers
The basic steps for using a Chooser are as follows:
Simple Example for PhoneNumberChooserTask :
PhoneNumberChooserTask pnChooserTask;

private void Button_Click_1(object sender, RoutedEventArgs e)
{
pnChooserTask = new PhoneNumberChooserTask();

pnChooserTask.Completed += new
EventHandler<PhoneNumberResult>(pnChooserTask_Completed);

pnChooserTask.Show();
}

DONG NAI UNIVERSITY OF TECHNOLOGY
14
1. Launchers and Choosers
void pnChooserTask_Completed(object sender,
PhoneNumberResult e) {
if (e.TaskResult == TaskResult.OK)
{
MessageBox.Show("The phone number for " +
e.DisplayName + " is " + e.PhoneNumber);

PhoneCallTask phoneCallTask = new PhoneCallTask();
phoneCallTask.DisplayName = e.DisplayName;
phoneCallTask.PhoneNumber = e.PhoneNumber;
phoneCallTask.Show();
}
}
PhoneCallTask is protected by the ID_CAP_PHONEDIALER capability
DONG NAI UNIVERSITY OF TECHNOLOGY
15
2. Search extensibility
Another way that your app can communicate with standard
phone services is by extending the Bing search experience with
custom behavior, integrating your app seamlessly with the
search results.

There are two ways by which you can extend the Bing search
behavior in your apps: App Connect and App Instant Answer.
With both features, you can set up your app so that it shows up
in the Bing search results when the user taps the hardware
Search button

Please visit this link to learn more:
http://msdn.microsoft.com/en-
us/library/windowsphone/develop/hh286420(v=vs.105).aspx
DONG NAI UNIVERSITY OF TECHNOLOGY
16
2. Search extensibility
Requirement App Connect App Instant Answer
WMAppManifest.xml Requires Extensions entries
for each Bing category that
you want to extend
No specific changes
required
Extras.xml Required. Specifies captions
to be used in the search
results apps pivot item.
Not used
UriMapper Recommended. Allows you
to re-route to a specific page
on app startup.
Not required
Target page You can re-route to multiple
different pages, depending
on the search item, if you
want.
No option. Your app
is launched as
normal,
with its default
startup page
DONG NAI UNIVERSITY OF TECHNOLOGY
17
2. Search extensibility
Requirement App Connect App Instant Answer
Query string You should parse the
incoming query string
for categories and item
names for which you
want to provide extensions
You should parse the
incoming query string
for the bing_query value.
Search connection Bing includes your app in
the search results
when the users search
matches the categories for
which you registered
extensions.
Bing includes your app in
the search results,
based on whether it
thinks the query is
relevant to your app.
DONG NAI UNIVERSITY OF TECHNOLOGY
18
2. Search extensibility
The App Connect extensibility model
DONG NAI UNIVERSITY OF TECHNOLOGY
19
2. Search extensibility
The App Instant Answer extensibility model
DONG NAI UNIVERSITY OF TECHNOLOGY
20
3. Acquiring a single photo
Many apps need to provide the option to acquire a single photo
from the user, either selected from the media library or
captured by the camera. Windows Phone offers built-in tasks
that make this process simple: PhotoChooserTask and
CameraChooserTask
using Microsoft.Phone.Tasks;
using System.Windows.Media.Imaging;
DONG NAI UNIVERSITY OF TECHNOLOGY
21
3. Acquiring a single photo
PhotoChooserTask photoChooser = new PhotoChooserTask();
photoChooser.Completed += photoChooser_Completed;
photoChooser.Show();
private void photoChooser_Completed(object sender, PhotoResult e)
{
if (e.TaskResult != TaskResult.OK)
{
return;
}
BitmapImage image = new BitmapImage();
image.SetSource(e.ChosenPhoto);
myimage.Source = image;
}
DONG NAI UNIVERSITY OF TECHNOLOGY
22
4. Working with the media library
The Media Library APIs in the XNA framework facilitate deeper
integration with the users photos collection
using Microsoft.Xna.Framework.Media;
Reading photos
Adding new images
DONG NAI UNIVERSITY OF TECHNOLOGY
23
4. Working with the media library
This list is populated in the OnNavigatedTo method of the
AlbumListPage. All of the photo albums available on the device
are found under the RootPictureAlbum of the MediaLibrary.
protected override void OnNavigatedTo(NavigationEventArgs e)
{
using (MediaLibrary mLib = new MediaLibrary())
{
mylistbox.ItemsSource = mLib.RootPictureAlbum.Albums;
}
}
ID_CAP_MEDIALIB_PHOTO capability
DONG NAI UNIVERSITY OF TECHNOLOGY
24
4. Working with the media library
The Pictures property returns all of the photos in the users
library as a single collection, whereas the RootPictureAlbum
property returns a collection of PictureAlbum instances,
which correspond to the individual user photo albums:
foreach (PictureAlbum picAlbum in
mediaLibrary.RootPictureAlbum.Albums){
foreach (Picture pic in picAlbum.Pictures)
{
BitmapImage bitMap = new BitmapImage();
bitMap.SetSource(pic.GetImage());
Image img = new Image();//Process Image here
img.Source = bitMap;
}
}
DONG NAI UNIVERSITY OF TECHNOLOGY
25
4. Working with the media library
int pictureCollectionSize;
PictureCollection pictureCollection;
public void createPictureSlideShow() {
slideshowPivot.Items.Clear();
pictureCollection = picAlbum.Pictures;
pictureCollectionSize = pictureCollection.Count;
for (int i = 0; i < pictureCollectionSize; i++) {
PivotItem pitem = new PivotItem();
pitem.Header = "Hnh " + (i + 1);
BitmapImage bitMap = new BitmapImage();
bitMap.SetSource(pictureCollection[i].GetImage());
Image img = new Image();
img.Source = bitMap;
img.Stretch = Stretch.Fill;
pitem.Content = img;
slideshowPivot.Items.Add(pitem);
}
}
DONG NAI UNIVERSITY OF TECHNOLOGY
26
4. Working with the media library
Adding new images
var mediaLibrary = new MediaLibrary();
var targetJpegStream = new MemoryStream();

grayscaleImage.SaveJpeg(targetJpegStream,
grayscaleImage.PixelWidth,
grayscaleImage.PixelHeight, 0, 100);

targetJpegStream.Seek(0, SeekOrigin.Begin);

string grayscaleFilename = "gray_hinh_moi";
mediaLibrary.SavePicture(grayscaleFilename, targetJpegStream);
DONG NAI UNIVERSITY OF TECHNOLOGY
27
5. Capturing photos
PhotoCamera in Microsoft.Devices namespace and
PhotoCaptureDevice in Windows.Phone.Media.Capture

To build the photo preview and capture experience directly in
the app, enabling camera apps that go beyond the built-in
functionality.
Capabilities:
ID_CAP_ISV_CAMERA ,
ID_CAP_MEDIALIB_PHOTO
DONG NAI UNIVERSITY OF TECHNOLOGY
28
5. Capturing photos
PhotoCamera class
Using VideoBrush to display Camera
Tab on surface to
capture photos
MainPage.xaml
DONG NAI UNIVERSITY OF TECHNOLOGY
29
5. Capturing photos
PhotoCamera class
Coding MainPage
using Microsoft.Devices;
using Microsoft.Xna.Framework.Media;
using System.Windows.Media;
PhotoCamera camera;
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
camera = new PhotoCamera(CameraType.Primary);
camera.Initialized += camera_Initialized;
viewFinderVideoBrush.SetSource(camera);
}
DONG NAI UNIVERSITY OF TECHNOLOGY
30
5. Capturing photos
PhotoCamera class
Coding MainPage
void camera_Initialized(object sender,
CameraOperationCompletedEventArgs e){
if (e.Succeeded) {
//Press Camera Button on Real Device
CameraButtons.ShutterKeyPressed +=
CameraButtons_ShutterKeyPressed;
//Available to capture photo to library
camera.CaptureImageAvailable +=
camera_CaptureImageAvailable;
camera.FlashMode = FlashMode.Auto;
}
}
DONG NAI UNIVERSITY OF TECHNOLOGY
31
5. Capturing photos
PhotoCamera class
Coding MainPage
void CameraButtons_ShutterKeyPressed(object sender, EventArgs e)
{
try {
//User pressed the Camera Button on Physical device
camera.CaptureImage();
}
catch (InvalidOperationException ex) { }
}
Tap event on the canvas:
private void viewFinderCanvas_Tap(object sender,
System.Windows.Input.GestureEventArgs e)
{//Call CaptureImage method here
}
DONG NAI UNIVERSITY OF TECHNOLOGY
32
5. Capturing photos
PhotoCamera class
Coding MainPage
Auto Save Picture To Camera Roll if Image is Available
void camera_CaptureImageAvailable(object sender,
ContentReadyEventArgs e)
{
string path = "picture" +
DateTime.Now.ToString("YYYYMMDDHHMMSSmmmm") + ".jpg";

MediaLibrary library = new MediaLibrary();

library.SavePictureToCameraRoll(path, e.ImageStream);

}

DONG NAI UNIVERSITY OF TECHNOLOGY
33
5. Capturing photos
PhotoCamera class
Coding MainPage
Auto change Camera Orientation If device is rotated
protected override void OnOrientationChanged
(OrientationChangedEventArgs e) {

if (Orientation == PageOrientation.LandscapeRight) {
viewFinderVideoBrush.RelativeTransform = new
CompositeTransform() { CenterX = 0.5, CenterY = 0.5, Rotation = 180 };
}
else {
viewFinderVideoBrush.RelativeTransform = new
CompositeTransform() { CenterX = 0.5, CenterY = 0.5, Rotation = 0 };
}
base.OnOrientationChanged(e);
}
DONG NAI UNIVERSITY OF TECHNOLOGY
34
6. Extending the Photos Hub
Apps pivot
The simplest form of Photos Hub
extensibility is the ability to be
included in the apps pivot
<Extensions>
<Extension
ExtensionName="Photos_Extra_Hub"
ConsumerID="{5B04B775-356B-
4AA0-AAF8-6491FFEA5632}"
TaskID="_default" />
</Extensions>
DONG NAI UNIVERSITY OF TECHNOLOGY
35
7. Lenses
Lenses are camera apps that can be launched directly from
the built-in camera app and which naturally complement it by
using the camera sensor to provide a specialized experience
DONG NAI UNIVERSITY OF TECHNOLOGY
36
8. Sharing photos
Users dont want to just take photos; they want to share them,
too. With Windows Phone, apps can participate in this sharing
flow, whether the user is explicitly sharing a photo through the
ShareMediaTask or automatically uploading them to the
cloud by using auto-upload
DONG NAI UNIVERSITY OF TECHNOLOGY
37
8. Sharing photos
<Capability Name="ID_CAP_MEDIALIB_PHOTO" />
<Extensions>
<Extension ExtensionName="Photos_Extra_Share"
ConsumerID=
"{5B04B775-356B-4AA0-AAF8-6491FFEA5632}"
TaskID="_default" />
</Extensions>
WMAppManifest.xml
DONG NAI UNIVERSITY OF TECHNOLOGY
38
8. Sharing photos
protected override void OnNavigatedTo(NavigationEventArgs e)
{
IDictionary<string, string> queryStrings =
this.NavigationContext.QueryString;
if (queryStrings.ContainsKey("FileId"))
{
MediaLibrary library = new MediaLibrary();
Picture photoFromLibrary =
library.GetPictureFromToken(queryStrings["FileId"]);

BitmapImage bitmapFromPhoto = new BitmapImage();

bitmapFromPhoto.SetSource(photoFromLibrary.GetPreviewImage());
image1.Source = bitmapFromPhoto;
}
}
Coding MainPage
DONG NAI UNIVERSITY OF TECHNOLOGY
END
39

Das könnte Ihnen auch gefallen