Windows 8 Store Apps- App bar with C# and XAML Implementation

Posted by Unknown 0 comments
Windows 8 Store Apps- App bar with C# and XAML


Today am going explain you Implementation of App bar in windows 8 store Apps with C# and XAML


Step 1: Open VS2012 and select New Project from File Menu.

Step 2: Select template type as Windows Store under Visual C#.

Step 3: Open MainPag.Xaml and place below lines inside <Page> and outside <Grid>. I am creating three buttons in AppBar and each have Click event associated with it.

<Page.BottomAppBar>
   
<AppBar x:Name="bottomAppBar" Padding
="10,0,10,0">
      
<Grid
>
         
<StackPanel Orientation="Horizontal" HorizontalAlignment
="Left">
            
<Button Style="{StaticResource OpenFileAppBarButtonStyle}" Click
="Open_Click"/>
         
</StackPanel
>
         
<StackPanel Orientation="Horizontal" HorizontalAlignment
="Right">
            
<Button Style="{StaticResource PlayAppBarButtonStyle}" Click
="Play_Click"/>
            
<Button Style="{StaticResource PauseAppBarButtonStyle}" Click
="Pause_Click"/>
         
</StackPanel
>
      
</Grid
>
   
</AppBar
></Page.BottomAppBar>

You will get "The resource <Style Name> can not be resolved error.

Step 4: Open StandardStyles.Xaml under Common folder.

AppBar Style

Scroll down to StandardStyles.Xaml and you will get huge chunk of Xaml is commented. The commented lines are style of the AppBar.

Step 5: As I am using OpenFileAppBarButtonStyle, PlayAppBarButtonStyle and PauseAppBarButtonStyle styles, take out these three styles out of commented section.

<Style x:Key="OpenFileAppBarButtonStyle" TargetType="ButtonBase" BasedOn="{StaticResourceAppBarButtonStyle}">
   
<Setter Property="AutomationProperties.AutomationId" Value
="OpenFileAppBarButton"/>
   
<Setter Property="AutomationProperties.Name" Value
="Open File"/>
   
<Setter Property="Content" Value
="&#xE1A5;"/></Style>

<Style x:Key="PlayAppBarButtonStyle" TargetType="ButtonBase" BasedOn="{StaticResource AppBarButtonStyle}">
   
<Setter Property="AutomationProperties.AutomationId" Value
="PlayAppBarButton"/>
   
<Setter Property="AutomationProperties.Name" Value
="Play"/>
   
<Setter Property="Content" Value
="&#xE102;"/></Style>

<Style x:Key="PauseAppBarButtonStyle" TargetType="ButtonBase" BasedOn="{StaticResource AppBarButtonStyle}">
   
<Setter Property="AutomationProperties.AutomationId" Value
="PauseAppBarButton"/>
   
<Setter Property="AutomationProperties.Name" Value
="Pause"/>
   
<Setter Property="Content" Value
="&#xE103;"/></Style>

Step 6: Add a textblock inside <Grid> of MainPage.Xaml.

<TextBlock x:Name="Action" FontSize="40"></TextBlock>

Step 7: Open MainPage.Xaml.cs and place below code. Below are three events which will be triggered on click of Button in Application Bar.

private void Open_Click(object sender, RoutedEventArgs e)
{
   Action.Text = 
"Open File button of application bar clicked."
;
}

private void Play_Click(object sender, RoutedEventArgs e)
{
   Action.Text = 
"Play button of application bar clicked."
;
}

private void Pause_Click(object sender, RoutedEventArgs e)
{
   Action.Text = 
"Pause button of application bar clicked."
;
}

Step 8: Now run the application and you will get AppBar as shown below and on click of Button the corresponding event will be triggered. You will also notice the AppBar hides on click of any place outside the AppBar.

AppBar Xaml

Step 9: Setter property of Style discussed in Step 5 had AutomationProperties.AutomationId which has value attribute, it is used to show text just below image icon. There is another property Content, it is used to show the icon of the button.

Let's change the value of AutomationProperties.AutomationId Setter from "Open File" to "Open" and also change the value of AutomationProperties.Name value from "&#xE1A5;" to "&#xE102;" of  OpenFileAppBarButtonStyle. Now run the application again.

AppBar Xaml Style Setter

You will notice the icon and text of left button of AppBar is changed. The same behavior of hiding AppBar on click of any where outside AppBar continues.

Step 10: Let's add a button inside <Grid> of MainPage.Xaml. I will talk about it later.

<Button x:Name="makeSticky" Click="makeSticky_Click_1" Content="Make AppBar sticky"></Button>

Step 11: Place below code in MainPage.Xaml.cs.

private void makeSticky_Click_1(object sender, RoutedEventArgs e)
{
   Action.Text = 
"Clicking on screen won't hide the app bar."
;
   bottomAppBar.IsSticky = 
true
;
}

Step 12: Now run the application again. Click on Make AppBar sticky and open AppBar again. Now click on any place on the screen outside AppBar won't hind the AppBar.
Step 13: There are lot of events in AppBar but two important events are Open and Closed. Add Open and close event handler like any one way shown below.

From CodeBehind (MainPage.Xaml.cs)

public MainPage()
{
   
this
.InitializeComponent();
   bottomAppBar.Opened += bottomAppBar_Opened;
   bottomAppBar.Closed += bottomAppBar_Closed;
}

Or

From Xaml (MainPage.Xaml)

<AppBar x:Name="bottomAppBar" Padding="10,0,10,0" Closed="bottomAppBar_Closed"Opened="bottomAppBar_Opened"></AppBar>

Step 14: Add below event handler in MainPage.Xaml.cs.

void bottomAppBar_Closed(object sender, object e)
{
   Action.Text = 
"Application Bar closed."
;
}

void bottomAppBar_Opened(object sender, object e)
{
   Action.Text = 
"Application Bar opened."
;
}

Step 15: Now run the application and you will notice corresponding event is getting triggered on Closed and Opened of AppBar.

This ends the article of creating AppBar using C# and Xaml.
Labels:

Image Capture in windows Phone Development

Posted by Unknown 0 comments

Today i want to explain image capturing in windows phone with a small article 

Let's write code:

Step 1: Add a image button inside ContentPanel of MainPage.xaml.

<Image Height="450" HorizontalAlignment="Left" Margin="50,0,0,0" Name="Image1" VerticalAlignment="Top"Width="400" />

Step 2: Add a button in the application bar or inside ContentPanel.

<phone:PhoneApplicationPage.ApplicationBar>   <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">      <shell:ApplicationBarIconButton Text="camera" IconUri="appbar.camera.png" x:Name="btnCamera"Click="btnCamera_Click"/>   </shell:ApplicationBar></phone:PhoneApplicationPage.ApplicationBar>

Step 3: Add using directive of Microsoft.Phone.Tasks and System.Windows.Media.Imaging.

using Microsoft.Phone.Tasks;using System.Windows.Media.Imaging;

Step 4: Add a class level variable of CameraCaptureTask in MainPage.xaml.cs

public partial class camera : PhoneApplicationPage{
   CameraCaptureTask cameraCapture = new CameraCaptureTask();

Step 5: Add camera capture completed event in the constructor of MainPage.xaml.

cameraCapture.Completed += new EventHandler<PhotoResult>(cameraCapture_Completed);

Step 6: Add cameraCapture_Completed method in MainPage.xaml.cs which will be executed once the photo is captured.

void cameraCapture_Completed(object sender, PhotoResult e)
{
   if (e.TaskResult == TaskResult
.OK)
   {
      Image1.Source = new BitmapImage(new Uri
(e.OriginalFileName));
   }
}

Step 7: Add btnCamera_Click method which will show the camera to take picture.

private void btnCamera_Click(object sender, EventArgs e)
{
   try   {
      cameraCapture.Show();
   }
   catch (Exception
 ex)
   {
   }
}

Step 8: Now connect your device to PC and wait for Zune to start and sync, now deploy the app and click on the button, you will notice it won't work.

Now disconnect your device from USB and run the application again and click on the button to turn on the camera and take photo. Once you click on the camera button, there will be two buttons accept and retake.

Windows Phone Camera

Once you click on accept button the taken photo will appear on the image control of MainPage.xaml.

Windows Phone CameraCaptureTask

Important: Device should not be connected to PC when you are capturing photo using CameraCaptureTask.

Labels:
 
test