Showing posts with label Win8MetroApps. Show all posts
Showing posts with label Win8MetroApps. Show all posts

how to add images from file location WPF

Posted by Unknown 0 comments
how to add images from file location WPF

In WPF development working with the images most of the new developers to WPF they struck at the loading the images from the file location.There is a simple way to accomplish to this.
 

Simply we can use

image1.Source = new BitmapImage(new Uri(filePath));


Here you can use the filepath with help of new uri class. 
Labels:

Navigation using XAML and C# in windows 8 Store Apps

Posted by Unknown 0 comments

In this article am going to explain about navigation using XAML and C# in windows 8 Store Apps

Implementation of  Navigation using 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: We will use Frame object to implement navigation of apps in Windows Store.

Important methods of Frame Object

Navigate method navigates to specified object and

Returns: bool

CanGoBack gets a value that indicates whether there is at least one entry in back navigation history.

Returns: bool - True if there is at least one entry in back navigation history; false if there are no entries in back navigation history or the Frame does not own its own navigation history.

GoBack navigates to the most recent item in back navigation history, if a Frame manages its own navigation history.

Returns: void

CanGoForward gets a value that indicates whether there is at least one entry in forward navigation history.

Returns: bool - True if there is at least one entry in forward navigation history; false if there are no entries in forward navigation history or the Frame does not own its own navigation history.

GoForward navigates to the most recent item in forward navigation history, if a Frame manages its own navigation history.

Returns: void

BackStackDepth gets the number of entries in the navigation back stack.

Returns: int - The number of entries in the navigation back stack.

Step 3: Add another Xaml BlankPage1.Xaml.

Step 4: Open MainPage.Xaml and add below code. TextBlock to show page number, and two buttons with event handlers.

<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
   
<TextBlock x:Name="pageTitle" Text="Page 1" Style="{StaticResource PageHeaderTextStyle}" Margin
="0,0,30,450"/>
   
<Button Content="Go to Page 2" x:Name="Navigate" Tapped="Navigate_Tapped_1"></Button
>
   
<Button Content="Use Frame's GoForward" x:Name="goForward" Tapped="goForward_Tapped_1" Margin="0,418,0,312" ></Button
></Grid>

Step 4: Open MainPage.Xaml.cs and add two methods which will be triggered on click of buttons added in MainPage.Xaml.

private void Navigate_Tapped_1(object sender, TappedRoutedEventArgs e)
{
   Frame.Navigate(typeof(BlankPage1
));
}

private void goForward_Tapped_1(object sender, TappedRoutedEventArgs e)
{
   if
 (Frame.CanGoForward)
   {
      Frame.GoForward();
   }
}

Step 5: Open BlankPage.Xaml add below code which has one TextBlock and a button.

<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
   
<TextBlock x:Name="pageTitle" Text="Page 2" Style="{StaticResource PageHeaderTextStyle}" Margin
="0,0,30,450"/>
   
<Button Content="Go to Page 1" x:Name="Navigate" Tapped="Navigate_Tapped_1"></Button
></Grid>

Step 6: Place below code in BlankPage.Xaml.cs.

private void Navigate_Tapped_1(object sender, TappedRoutedEventArgs e)
{
   if
 (Frame.CanGoBack)
   {
      Frame.GoBack();
   }
}

Step 7: Now run the application, click of Go to Page 2 button as shown below.
Windows 8 - Navigation

On click of Go to Page 2 button of MainPage you will navigated to second page as shown below.

Windows 8 - Navigating

Step 8: Now run the application again, and click on "Use Frame's GoForward" button. You will notice nothing will happen. Put a debug point in Frame.CanGoForwad as shown below.

Windows 8 - Forward

Step 9: Now run the application in debug mode if it is not running and click on "Use Frame's GoForward". Select Frame object and go to QuickWatch.. you will notice CanGoForward is false.

Windows  8 - Page Navigation

And also you will notice BackStackDepth is 0 and CanGoBack is false.

Step 10: Now use "Go to Page 2" button to go to next page. Now put a debug point on BlankPage1.Xaml.cs as shown below.

Windows 8 - Backward Navigation

Step 11: Now click "Go to Page 1" button of BlankPage1.On hitting of above debug point select Frame object and go to QuickWatch.. you will notice CanGoBack has become true as shown below.

Windows 8 - Forward Navigation

And also now you will notice BackStackDepth is 1 but CanGoForward is still false.

Step 12: Now click on "Use Frame's GoForward" button again. The debug point set Step 8 will hit again and now you will notice CanGoForward is true.


Windows 8 - Navigate

Conclusion:

BackStackDepth will be always zero on first page of the application, it will increase incrementally by 1as you move on to next pages.

CanGoBack will be always zero on first page of the application as you don't have any page to go back.

CanGoForward will be zero for any page until you navigate to next page in stack and come back go back to previous page.

This ends the first part of Navigation in Windows Store.
Labels:

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:
 
test