locked
Does my Win8 Metro App need MainPage.xaml? RRS feed

  • Question

  • I'm looking through the samples, and they seem to all have a MainPage.xaml in them, which, when using VS2011, I have none, when creating a completely blank Win8 Metro application.  I get App.xaml and BlankPage.xaml, and that's it.  Should I create a new page and call it MainPage.xaml, or what do I do here?

    James


    Also, I'm trying to get used to VS2011 and how to program a very simple hello world, but it doesn't seem to recognize the Console.WriteLine("hello world"); function.  How do I do this for a metro app?  Or better yet, how to I also write a simple 1-button app that will display "hello world" when I press the button?  And how do I make an app display another bit of text if I press the button again?
    Sunday, April 8, 2012 3:40 AM

Answers

  • Hi,

    The MainPage.xaml is just a name and you can name your pages whatever your want.

    The "Console.WriteLine();" is only for console applications, you cannot use it in metro style application because there is no console.

    To create a hello world app follow these steppes:

    1. Go to your BlankPage.xml drag a TextBlock and a button to your page.
    2. Name your TextBlock, using the properties window or if you want to do it in the XAML: x:Name="helloWorldTextBlock"
    3. Double click on your button, it will create an even handler for Click event.
    4. In your code add a line like this (depending on what you named your TextBlock): helloWorldTextBlock.Text = "Hello World"

    -Soroush


    Sunday, April 8, 2012 7:18 AM
  • Also if you want the button to display something else when it's clicked twice,

    you can create a bool field and set it to true after the first click:

    private bool _isClicked = false;
    
     private void myButton_Tapped(object sender, TappedRoutedEventArgs e)
            {
                 if (!_isClicked)
                 {
                    helloWorldTextBlock.Text = "Hello World";
                    _isClicked = true;
                 }
                 else
                 {
                    //button is already clicked
                    helloWorldTextBlock.Text = "Hello World, 2nd click";
                 }
            }

    Or you can check the text property of your textblock:

    private void myButton_Tapped(object sender, TappedRoutedEventArgs e)
    { if (helloWorldTextBlock.Text != "Hello World") { helloWorldTextBlock.Text = "Hello World"; } else { //button is already clicked helloWorldTextBlock.Text = "Hello World, 2nd click"; } }


    -Soroush


    Sunday, April 8, 2012 7:34 AM

All replies

  • Hi,

    The MainPage.xaml is just a name and you can name your pages whatever your want.

    The "Console.WriteLine();" is only for console applications, you cannot use it in metro style application because there is no console.

    To create a hello world app follow these steppes:

    1. Go to your BlankPage.xml drag a TextBlock and a button to your page.
    2. Name your TextBlock, using the properties window or if you want to do it in the XAML: x:Name="helloWorldTextBlock"
    3. Double click on your button, it will create an even handler for Click event.
    4. In your code add a line like this (depending on what you named your TextBlock): helloWorldTextBlock.Text = "Hello World"

    -Soroush


    Sunday, April 8, 2012 7:18 AM
  • Also if you want the button to display something else when it's clicked twice,

    you can create a bool field and set it to true after the first click:

    private bool _isClicked = false;
    
     private void myButton_Tapped(object sender, TappedRoutedEventArgs e)
            {
                 if (!_isClicked)
                 {
                    helloWorldTextBlock.Text = "Hello World";
                    _isClicked = true;
                 }
                 else
                 {
                    //button is already clicked
                    helloWorldTextBlock.Text = "Hello World, 2nd click";
                 }
            }

    Or you can check the text property of your textblock:

    private void myButton_Tapped(object sender, TappedRoutedEventArgs e)
    { if (helloWorldTextBlock.Text != "Hello World") { helloWorldTextBlock.Text = "Hello World"; } else { //button is already clicked helloWorldTextBlock.Text = "Hello World, 2nd click"; } }


    -Soroush


    Sunday, April 8, 2012 7:34 AM
  • Ah, I guess that makes sense, but every C# book I see uses that, and I've not seen anything to the contrary online, so I wasn't sure.  That does help, thanks.  In VS2011, how to I add controls on the screen?  I'm not seeing that.

    James

    Monday, April 9, 2012 4:00 AM
  • You can use the "Toolbox" panel(Left side of the window). If you can't find it, go to View menu or press "CTRL + W, X".

    -Soroush


    Monday, April 9, 2012 6:03 AM