Windows Phone开发(26):启动器与选择器之MediaPlayerLauncher和SearchTask

原文:Windows Phone开发(26):启动器与选择器之MediaPlayerLauncher和SearchTask

启动器与选择器简单的地方在于,它们的使用方法几乎一模一样,从前面几节中,我相信大家基本上都知道如何使用它们了。
这里还是哆嗦一下吧,使用启动器和选择器的步骤如下:
1、实例化,new一个;
2、准备各参数,对相关的属性赋值;
3、Show;
4、对于启动器,不需要这步,但选择器有返回数据,所以需要处理完成事件。

本节再举两例子,启动器和选择器就可以完成了,然后我们下一节开始,探讨新的知识点。

例一:媒体播放器。

这是一个启动器,用起来更方便。
主要属性有:
Controls——要显示控制按钮,如暂集,停止等,它是一个带了Flags特性标记的枚举,所以可以多个值合并,如MediaPlaybackControls.Pause | MediaPlaybackControls.Stop

Location——要播放媒体的位置,Data表示文件存放在独立存储中,Install表示项目中的媒体文件;

Media——要播放文件的URI;

Orientation——这个更好懂了,媒体播放器的方向, 是水平还是垂直,和页面方向一个概念。

<phone:PhoneApplicationPage
    x:Class="sampleApp.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    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">

    <!--LayoutRoot 是包含所有页面内容的根网格-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!--TitlePanel 包含应用程序的名称和页标题-->
        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock x:Name="ApplicationTitle" Text="我的应用程序" Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock x:Name="PageTitle" Text="页面名称" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
        </StackPanel>

        <!--ContentPanel - 在此处放置其他内容-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <Button Content="启动媒体播放器" Height="126" HorizontalAlignment="Left" Margin="31,116,0,0" Name="button1" VerticalAlignment="Top" Width="381" Click="button1_Click" />
        </Grid>
    </Grid>

</phone:PhoneApplicationPage>

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 sampleApp
{
    public partial class MainPage : PhoneApplicationPage
    {
        // 构造函数
        public MainPage()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            MediaPlayerLauncher player = new MediaPlayerLauncher();
            player.Controls = MediaPlaybackControls.All;
            player.Location = MediaLocationType.Install;
            player.Media = new Uri("分飞燕.mp3", UriKind.Relative);
            player.Orientation = MediaPlayerOrientation.Portrait;
            player.Show();
        }
    }
}

 

例二:搜索任务。

SearchTask类也是一个启动器,这个家伙更简单了,它只有一个属性要设置——SearchQuery,就是我们要搜索的关键字。

<phone:PhoneApplicationPage
    x:Class="sampleApp.Page1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Landscape" Orientation="Landscape"
    mc:Ignorable="d" d:DesignHeight="480" d:DesignWidth="728"
    shell:SystemTray.IsVisible="True">

    <!--LayoutRoot 是包含所有页面内容的根网格-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!--TitlePanel 包含应用程序的名称和页标题-->
        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock x:Name="ApplicationTitle" Text="我的应用程序" Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock x:Name="PageTitle" Text="搜索" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
        </StackPanel>

        <!--ContentPanel - 在此处放置其他内容-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <TextBox Height="72" HorizontalAlignment="Left" Margin="12,86,0,0" Name="txtKey" VerticalAlignment="Top" Width="460" />
            <Button Content="搜索" Height="72" HorizontalAlignment="Left" Margin="480,86,0,0" Name="button1" VerticalAlignment="Top" Width="160" Click="button1_Click" />
        </Grid>
    </Grid>

</phone:PhoneApplicationPage>
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 sampleApp
{
    public partial class Page1 : PhoneApplicationPage
    {
        public Page1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            SearchTask searcher = new SearchTask();
            searcher.SearchQuery = txtKey.Text;
            searcher.Show();
        }
    }
}

 

 

下一节开始,我们讨论独立存储。

还有就是提一下建议,博客编辑器有问题,每次都这样,第一次自动保存草稿后,后面就不会保存了,编辑器内的文本无法选定。而点击发表时没有反应,非得刷新页面。

时间: 2024-07-29 19:31:57

Windows Phone开发(26):启动器与选择器之MediaPlayerLauncher和SearchTask的相关文章

Windows Phone开发(22):启动器与选择器之BingMapsDirectionsTask

原文:Windows Phone开发(22):启动器与选择器之BingMapsDirectionsTask 从今天开发始,我们又开始新的征程,接下来的课程我们要熟悉一下启动器和选择器,其实二者是一样的,没有根本的区别,启动器是有返回结果的,如打开搜索应用程序进行搜索,而选择器是有返回内容的,如选择一张照片.   那么,启动器和选择器是啥玩意儿呢?其实我们可以很简单去理解,说白了,就是使用系自带的组件或应用程序.对的,就是这样,我说过,有时候很多概念只是名字上吓人罢了,实际用起来是非常简单的,比如

Windows Phone开发(23):启动器与选择器之CameraCaptureTask和PhotoChooserTask

原文:Windows Phone开发(23):启动器与选择器之CameraCaptureTask和PhotoChooserTask 这两个组件都属于选择器,而且它们也有很多相似的地方,最明显的上一点,它们都是用来选择图片.     一.CameraCaptureTask选择器.   它用于启动照相机,当你拍下照片后,自动把照的字节流返回给调用方应用程序.前文说过,启动器和选择的使用方法和步骤都是一样的.对于CameraCaptureTask组件也如此,不过注意的一点是,处理Completed事件

Windows Phone开发(25):启动器与选择器之WebBrowserTask

原文:Windows Phone开发(25):启动器与选择器之WebBrowserTask 从名字上就看出来,这个家伙就是打开浏览并浏览到指定页面. 它有两个用途完全一样的属性:Uri属性是System.Uri类型,这是新写进的属性: URL是字符串类型,但如果使用该属性,会发出警告"已过时",所以建议使用前者. 下面这个例子,点击按钮后都是打开WEB浏览器并定位到文本框中输入的地址,但分别用了上面所说的两个属性,当程序运行后,你会发现其效果是一样的. <phone:PhoneA

Windows Phone开发(24):启动器与选择器之发送短信

原文:Windows Phone开发(24):启动器与选择器之发送短信 本节我们通过一个简单的发送短信示例来演示一下如果配合使用PhoneNumberChooserTask和SmsComposeTask类. PhoneNumberChooserTask是选择器,它用于从你的电话簿里选择你要发送短信的电话号码: SmsComposeTask就是用来启动发送短信组件并显示发送窗口. 注意,这些操作都在用户的操控之中,发送短信一定会显示可视化页面的,而且不会偷偷地在后台发送,因为Windows pho

Windows 8开发入门(十八)windows 8开发模拟器和程序中关联文件类型

本文将讲解两个内容,1.Windows 8 开发模拟器的使用.2.程序中设置关联文件类型. 一. Windows 8 开发模拟器的使用 1.在VS2012中自带有Windows 8开发模拟器,我们选择VS界面中的调试按 钮从"本地计算器"改选为"Simulater"模拟器即可,如下图: 2.点击"Simulator"按钮即可弹出以下界面 3.此时我们可以看到模拟器上面有很多按钮这些按钮的功能有模拟单点触摸.多点触摸.旋转设备等操作 .我们在这里可

【万里征程——Windows App开发】控件大集合2

下面再来看看一些前面还没有讲过的控件,不过控件太多以至于无法全部列出来,大家只好举一反三啦. Button 前面最常用的控件就是Button啦,Button还有一个有意思的属性呢,当把鼠标指针放在Button上时,就会在Button的头顶冒出一串文本啦.这个不太截图哎-- <Button ToolTipService.ToolTip="Go to www.blog.csdn.net/nomasp" Margin="692,458,0,230" /> Bu

Windows 10开发基础——文件、文件夹和库(一)

原文:Windows 10开发基础--文件.文件夹和库(一) 主要内容:      1.枚举查询文件和文件夹      2.文本文件读写的三种方法--创建写入和读取文件      3.获得文件的属性   枚举查询文件和文件夹 先了解一下文件查询的几个方法: StorageFolder.GetFilesAsync: 获取当前文件夹中的所有文件,返回一个 IReadOnlyList<StorageFile>集合          IReadOnlyList<StorageFile> f

Windows Phone 7开发教程(1)——Windows Phone开发工具初体验

这是为<程序员>4月期写的文章,是在<双重惊喜-- Windows Phone Developer Tools初体验>的基础上增加了一些内容, 包括WebBrowser空间.MediaPlayer空间,还有一个非常简单的XNA程序.希望对 Windows Phone开发有兴趣的朋友有一些帮助. Windows Phone开发工具在MIX 2010上火热登场了.Windows Mobile开发者们 压抑许久的热情终于爆发出来,对于Windows Phone的华丽转身,开发者们褒贬 不

Windows 8开发入门(二十) Windows 8中的GridView使用(二)

Windows 8中的GridView使用(二)和DataTemplateSelector 在本文中所讲述内容的实例仍然沿用于上篇文章,有什么疑惑可以参考上篇文章. 一 GroupStyle 在GridView控件中我们可以对数据进行分组显示,通过对GridView的GroupStyle进行控制,分别设置 GroupStyle.HeaderTemplate和GroupStyle.Panel.如下代码: <GridView.GroupStyle> <GroupStyle> <G