Class in Windows Store App Using C#

Introduction

In this article I describe how to work with a class in a Windows Store app. In other words I bind the class. So let us understand the scenario.

Here I have added a class by right-clicking on the app name in Solution Explorer and create a class called employee and build it. Now add some controls to display the value to the class object.

So use the following procedure to create it.

Step 1

First of all you must create a new Windows Store Application.

  • Open Visual Studio 2012
  • "File" -> "New" -> "Project..."
  • Choose "Template" -> "Visual C#" -> "Window Store app"
  • Choose "Blank App (XAML)" then rename the application

new-windows-store-app.jpg

Step 2

Now add a class by right-clicking on the name of the application in Solution Explorer and write the following simple code:

 

namespace binding_from_class

{

    public class employee

    {

        public int id { get; set; }

        public string name { get; set; }

        public int salary { get; set; }

    }

}

 

Step 3

Write the following XAML code in "Mainpage.Xaml" (that is available in Solution Explorer):

<Page

    x:Class="binding_from_class.MainPage"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:local="using:binding_from_class"

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    mc:Ignorable="d">

 

    <Grid Background="Red">

        <Grid.ColumnDefinitions>

            <ColumnDefinition Width="285*"/>

            <ColumnDefinition Width="378*"/>

            <ColumnDefinition Width="703*"/>

        </Grid.ColumnDefinitions>

        <Grid.RowDefinitions>

            <RowDefinition Height="90*"/>

            <RowDefinition Height="64*"/>

            <RowDefinition Height="61*"/>

            <RowDefinition Height="52*"/>

            <RowDefinition Height="51*"/>

            <RowDefinition Height="450*"/>

        </Grid.RowDefinitions>

        <TextBlock x:Name="text1"  FontFamily="Arial" FontSize="20" FontWeight="ExtraBold" Grid.Column="1" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Top" Height="54" Margin="89,0" Width="200" Foreground="White" TextAlignment="Center"   ></TextBlock>

        <TextBlock x:Name="text2" FontFamily="Arial" FontSize="20"  FontWeight="ExtraBold" Grid.Column="1" Grid.Row="2" HorizontalAlignment="Center" VerticalAlignment="Top" Height="51" Margin="38,0,10,0" Width="330" Foreground="White" TextAlignment="Center"   ></TextBlock>

        <TextBlock x:Name="text3" FontFamily="Arial" FontSize="20"  FontWeight="ExtraBold" Grid.Column="1" Grid.Row="3" HorizontalAlignment="Center" VerticalAlignment="Top" Height="52" Margin="10,0" Width="358" Foreground="White" TextAlignment="Center" ></TextBlock>

        <Button x:Name="button1" Background="Yellow" Foreground="Black"  Grid.Column="1" Grid.Row="4" Content="Show" HorizontalAlignment="Center" VerticalAlignment="Top" Margin="136,0,101,0" Width="141" Height="50" Click="button1_Click" ></Button>

    </Grid>

</Page>

 

Step 4

Now write the following C# code for the button within "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;

 

namespace binding_from_class

{

    public sealed partial class MainPage : Page

    {

        public MainPage()

        {

            this.InitializeComponent();

        }

        protected override void OnNavigatedTo(NavigationEventArgs e)

        {

        }

        employee emp = new employee();     

        private void button1_Click(object sender, RoutedEventArgs e)

        {

            emp.id = 12;

            emp.name = "Satya";

            emp.salary = 6000;

            text1.Text = emp.id.ToString ();

            text2.Text = emp.name;

            text3.Text = emp.salary.ToString();

        }

    }

}

 

Step 5

Now run your app and click on the "Show" button.

class-output.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all