Saturday, September 22, 2012

Setup Controls using C# code, instead of XAML.

In this exercise, we are going to add TextBlock using C# code, instead of XAML.

Start Visual Studio Express 2012 RC for Windows 8, New Project..., using template of Visual C# Windows Metro styles, Blank App (XAML), with name of helloMetro_Csharp. OK.

New Project..., using template of Visual C# Windows Metro styles, Blank App (XAML), with name of helloMetro_Csharp.


Double click on MainPage.xaml in Solution Explorer to open the XAML defination of the screen. Modify the code to add x:Name="mycontent" for <Grid> element. And save it.

Modify XAML to add x:Name


    <Grid x:Name="mycontent"
        Background="{StaticResource ApplicationPageBackgroundThemeBrush}">

    </Grid>


Double click on MainPage.xaml.cs to edit the code behind MainPage.

Modify the code behind MainPage


        public MainPage()
        {
            this.InitializeComponent();

            TextBlock myTextBlock = new TextBlock();
            myTextBlock.Text = "Hello Dev-MicroSoft";
            myTextBlock.FontFamily = new FontFamily("Times New Roman");
            myTextBlock.FontSize = 80;
            myTextBlock.HorizontalAlignment = HorizontalAlignment.Center;
            myTextBlock.VerticalAlignment = VerticalAlignment.Center;

            mycontent.Children.Add(myTextBlock);

        }


Save and Run it.

Hello Dev-MicroSoft


No comments:

Post a Comment