Friday, June 13, 2014

CHAPTER 3 EX 01 John App for Viewing Images

Ex 1) John is developing a Windows Store app for viewing images.He has created the UI of the app,as shown in the following figure.
Now he has to add the functionalities to the app so that:
 (1)The users are able to select an image from the Image List section.
 (2)Once the user selects the image from the image list section,the preview of the selected image is     displayed in the Image Preview section.
 (3)The preview of the selected image uniformly fits in the defined area.
Help John to add the preceding functionalities to the app.
Prerequisite:To perform the preceding exercise,you need the following files:
 1.jpg,2.jpg,3.jpg,4.jpg,5.jpg,6.jpg,7.jpg,8.jpg

Ans 1)

Paste in xaml



<Page
    x:Class="ImageList2.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:ImageList2"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}" Margin="0,-5,0,5">
        <Button Content="Image List" FontSize="22" FontWeight="Bold" Background="Blue" HorizontalAlignment="Left" Margin="41,28,0,0" VerticalAlignment="Top" Width="229"/>
        <Button Content="Image Preview" HorizontalAlignment="Left" Background="Blue" FontSize="22"   Margin="282,28,0,0" VerticalAlignment="Top" Height="48" Width="808"/>
        <ListBox x:Name="lstboxPictureList" HorizontalAlignment="Left" Height="655" VerticalAlignment="Top" Width="229" Margin="41,81,0,0" SelectionChanged="1stboxPictureList_SelectionChanged" />
        <Button IsEnabled="False" HorizontalAlignment="Left" Margin="282,81,0,0" VerticalAlignment="Top" Height="610" Width="808">
            <Image x:Name="imgpreview" Source="Assets/1.jpg" Stretch="Uniform"></Image>
        </Button>

    </Grid>
</Page>


paste in mainpage.xaml.cs


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.UI.Xaml.Media.Imaging;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace ImageList2
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }
        public void loadPictureList()
        {
            for (int i = 1; i < 9; i++)
            {
                lstboxPictureList.Items.Add(i + ".jpg");
            }
            lstboxPictureList.SelectedIndex=0;
        }
          private void lstboxPictureList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
          imgpreview.Source = new BitmapImage(new Uri("ms-appx:///Assets/" + lstboxPictureList.SelectedValue));
             
        }
       
       
        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.  The Parameter
        /// property is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            loadPictureList();
        }
    }
}


No comments:

Post a Comment