Wednesday, February 27, 2013

Windows Phone Youtube search app

make a project with the name YouTubeApp and have a main layout at main.xaml as follows


<phone:PhoneApplicationPage 
    x:Class="YouTubeApp.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 is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!--TitlePanel contains the name of the application and page title-->
        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock x:Name="PageTitle" Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
        </StackPanel>

        <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <TextBox Height="72" HorizontalAlignment="Left" Margin="-12,6,0,0" Name="textBox1" Text="Musetheplace" VerticalAlignment="Top" Width="460" />
            <Button x:Name="SearchButton" Content="Search" HorizontalAlignment="Right" VerticalAlignment="Top" Click="SearchButton_Click" Margin="0,66,164,0" />
            <ListBox SelectionChanged="VideoListSelectionChanged" x:Name="ResultsList" Margin="10,144,0,0">
<ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="{Binding Title}" VerticalAlignment="Top" 
         HorizontalAlignment="Left" Width="355" Margin="100,0,0,0" TextWrapping="Wrap"/>
            <Image Source="{Binding VideoImageUrl}" HorizontalAlignment="Left" Width="100"   
         Height="100" Stretch="UniformToFill"/>
                        </StackPanel>
                    </DataTemplate>
                    </ListBox.ItemTemplate>
            </ListBox>


        </Grid>
    </Grid>

    <!--Sample code showing usage of ApplicationBar-->
    <!--<phone:PhoneApplicationPage.ApplicationBar>
        <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
            <shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="Button 1"/>
            <shell:ApplicationBarIconButton IconUri="/Images/appbar_button2.png" Text="Button 2"/>
            <shell:ApplicationBar.MenuItems>
                <shell:ApplicationBarMenuItem Text="MenuItem 1"/>
                <shell:ApplicationBarMenuItem Text="MenuItem 2"/>
            </shell:ApplicationBar.MenuItems>
        </shell:ApplicationBar>
    </phone:PhoneApplicationPage.ApplicationBar>-->

</phone:PhoneApplicationPage>


have a code at main.xaml.cs as follows


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;
using System.Xml.Linq;

namespace YouTubeApp
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }
        private void SearchButton_Click(object sender, RoutedEventArgs e)
        {
            var wc = new WebClient();
            wc.DownloadStringCompleted += DownloadStringCompleted;
            var searchUri = string.Format(
              "http://gdata.youtube.com/feeds/api/videos?q={0}&format=6",
              HttpUtility.UrlEncode(textBox1.Text));
            wc.DownloadStringAsync(new Uri(searchUri));
        }
        void DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            var atomns = XNamespace.Get("http://www.w3.org/2005/Atom");
            var medians = XNamespace.Get("http://search.yahoo.com/mrss/");
            var xml = XElement.Parse(e.Result);
            var videos = (
              from entry in xml.Descendants(atomns.GetName("entry"))
              select new YouTubeVideo
              {
                  VideoId = entry.Element(atomns.GetName("id")).Value,
                  VideoImageUrl = (
                    from thumbnail in entry.Descendants(medians.GetName("thumbnail"))
                    where thumbnail.Attribute("height").Value == "240"
                    select thumbnail.Attribute("url").Value).FirstOrDefault(),
                  Title = entry.Element(atomns.GetName("title")).Value
              }).ToArray();
            ResultsList.ItemsSource = videos;
        }
        private void VideoListSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var video = ResultsList.SelectedItem as YouTubeVideo;
            if (video != null)
            {
                var parsed = video.VideoId.Split('/');
                var id = parsed[parsed.Length - 1];
                var playbackUrl = "vnd.youtube:" + id;

                var task = new WebBrowserTask { URL = playbackUrl };
                task.Show();
            }
        }
    }
    public class YouTubeVideo
    {
        public string Title { get; set; }
        public string VideoImageUrl { get; set; }
        public string VideoId { get; set; }
    }
}


just run the app and have fun with this app.




No comments:

Post a Comment