مهندسی کامپیوتر گرایش نرم افزار دانشگاه صنعتی ارومیه ورودی1391

امروزه پیشرفت دانش بشری تنها با کامپیوتر میسر است

مهندسی کامپیوتر گرایش نرم افزار دانشگاه صنعتی ارومیه ورودی1391

امروزه پیشرفت دانش بشری تنها با کامپیوتر میسر است

visual studio

 learn visual studio

 

When you start Visual Studio for the first time, you must choose a settings combination that applies a set of pre-defined customizations to the IDE. Each settings combination has been designed to make it easier for you to develop applications.

Figure 1: Choose Default Environment Settings dialog box

Choose Default Environment Settings dialog box

When you start Visual Studio for the first time, you must choose a settings combination that applies a set of pre-defined customizations to the IDE. Each settings combination has been designed to make it easier for you to develop applications.

Figure 1: Choose Default Environment Settings dialog box

Choose Default Environment Settings dialog box

This walkthrough is written with General Development Settings applied, which applies the least amount of customization to the IDE. You can change your settings combination by using the Import and Export Settings Wizard. For more information, see How to: Change Select Settings.

After you open Visual Studio, you can identify the three basic parts of the IDE: tool windows, menus and toolbars, and the main window space. Tool windows are docked on the left and right sides of the application window, with Quick Launch, the menu bar, and the standard toolbar at the top. In the center of the application window is the Start Page. When a solution or project is loaded, editors and designers appear in this space. When you develop an application, you’ll spend most of your time in this central area.

Figure 2: Visual Studio IDE

IDE with General Settings Applied

By using the Options dialog box, you can make additional customizations to Visual Studio, such as changing the font face and size that’s used for text in the editor or the color theme that’s applied to the IDE. Depending on the settings combination that you’ve applied, some items in that dialog box might not appear automatically. You can make sure that all possible options appear by choosing the Show all settings check box.

Figure 3: Options dialog box

Options dialog box wirh Show all settings option

In this example, you’ll change the color theme of the IDE from light to dark.

To change the color theme of the IDE

  1. Open the Options dialog box.

    Options command on the Tools menu
  2. Change the Color theme to Dark.

    Dark color theme selected

The colors in Visual Studio should match the following image:

IDE with Dark Theme applied

To return the IDE to the Light color theme, repeat the previous steps, and except choose Light in step 2. For more information, see Customizing the Development Environment.

When you create an application in Visual Studio, you first create a project and a solution. For this example, you’ll create a Windows Presentation Foundation application.

To create the WPF project

  1. On the menu bar, choose File, New, Project.

    On the menu bar, choose File, New, Project

    Or use Quick Launch to open the New Project dialog box.

    In the Quick Launch box, specify new project
  2. Choose the Visual Basic or the Visual C# WPF Application template, and then name the project HelloWPFApp.

    Create a Visual Basic WPF Project, HelloWPFApp

    OR

    Create a Visual C# WPF Project, HelloWPFApp

The HelloWPFApp project and solution, with the basic files for a WPF Application, are created and automatically loaded into Solution Explorer. The MainWindow.xaml file and either MainWindow.xaml.vb or MainWindow.xaml.cs are opened in the WPF Designer, depending on whether you chose Visual Basic or Visual C#. The following items appear in Solution Explorer:

Figure 5: Project items

Solution Explorer with HelloWPFApp files loaded

After you create the project, you can start to customize it. By using the Properties window, you can display and change options for project items, controls, and other items in an application. By using the Project Designer or Property Pages, you can display and change options for projects and solutions.

In the next example, you’ll give MainWindow a more specific name.

To change the name of MainWindow.xaml

  1. In Solution Explorer, open the Properties window for MainWindow.xaml, and change the File Name property to Greetings.

    Properties window with File Name highlighted
    Note Note

    This change will cause an error, which you’ll fix later in this walkthrough.

  2. In the WPF Designer, choose the MainWindow form.

  3. In the Properties window, change the value of the Title property to Greetings.

Solution Explorer shows that you’ve changed the name of the MainWindow.xaml file to Greetings.xaml and that you’ve changed the name of MainWindow.xaml.vb or MainWindow.xaml.cs to Greetings.xaml.vb or Greetings.xaml.cs. In addition, the title bar for MainWindow.xaml now reads Greetings.

The WPF application that you’re creating contains three types of controls: a TextBlock control, two RadioButton controls, and a Button control. The WPF Designer shows a design view and a XAML view of Greetings.xaml simultaneously in what’s called a split view. You can use this view to interact with both the visual elements and the markup of your XAML files without having to switch tabs in the designer. For more information, see WPF Designer for Windows Forms Developers.

To start creating the UI for your application, you can add a TextBlock control to Greetings.xaml.

To add a TextBlock control

  1. In the Toolbox, search for the TextBlock control.

    Toolbox with the TextBlock control highlighted
  2. Add a TextBlock control to the design surface, and center the control near the top of the window.

Your window should resemble the following illustration:

Figure 7: Greetings window with TextBlock control

TextBlock control on the Greetings form

As the XAML view shows, the following XAML markup has been added for the control:

<TextBlock HorizontalAlignment="Center" TextWrapping="Wrap" VerticalAlignment="Center" RenderTransformOrigin="4.08,2.312" Margin="237,57,221,238"><Run Text="TextBlock"/><InlineUIContainer><TextBlock TextWrapping="Wrap" Text="TextBlock"/>

You can edit control properties in several ways such as directly in the XAML view, in the Properties window, and in some cases, by using commands on the shortcut menu. For this TextBlock control, you’ll modify the display text directly in the XAML view.

To customize the text in the text block

  1. In the XAML view, locate the markup for TextBlock, and change the value of Text=”TextBlock” to Text=”Select a message option and then choose the Display button.”

  2. In the Design view, enlarge the TextBlock control so that all of the text that you’ve specified appears, and then save your changes.

Next, you’ll add two RadioButton controls to the form.

To add radio buttons

  1. In the Toolbox, search for the RadioButton control.

    Toolbox window with RadioButton control selected
  2. Add two RadioButton controls to the design surface, and move them so that they appear under the TextBlock control.

    Your window should resemble this example:

    Figure 8: RadioButtons in the Greetings window.

    Greetings form with textblock and two radiobuttons
  3. In the Properties window for the left RadioButton control, change the Name property to RadioButton1.

  4. In the Properties window for the right RadioButton control, change the Name property to RadioButton2, and then save your changes.

You can now add display text for each RadioButton control. The following procedure updates the Content property for a RadioButton control.

To add display text for each radio button

  1. On the design surface, open the shortcut menu for RadioButton1, choose Edit Text, and then enter Hello.

  2. Open the shortcut menu for RadioButton2, choose Edit Text, and then enter Goodbye.

The final UI element that you’ll add is a Button control.

To add the button control

  1. In the Toolbox, search for the Button control, and then add it to the design surface, under the RadioButton controls.

  2. In the XAML view, change the value of Content for the Button control from Content=”Button” to Content=”Display”, and then save the changes.

    The markup should resemble the following example: <Button Content="Display" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="215,204,0,0"/>

Your window should resemble the following illustration.

Figure 9: Final Greetings UI

Greetings form with control labels

When this application runs, a message box will appear after a user first chooses a radio button and then chooses the Display button. One message box will appear for Hello, and another will appear for Goodbye. To create this behavior, you’ll add code to the Button_Click event in Greetings.xaml.vb or Greetings.xaml.cs.

To display the message boxes

  1. On the design surface, double-click the Display button.

    Greetings.xaml.vb or Greetings.xaml.cs opens, with the cursor in the Button_Click event.

  2. For Visual Basic, enter the following code:

            If RadioButton1.IsChecked = True Then
                MessageBox.Show("Hello.")
            Else RadioButton2.IsChecked = True
                MessageBox.Show("Goodbye.")
            End If
    

    For Visual C#, enter the following code:

               if (RadioButton1.IsChecked == true)
               {
                   MessageBox.Show("Hello.");
               }
               else
               {
                   RadioButton2.IsChecked = true;
                   MessageBox.Show("Goodbye.");
    
  3. Save the application.

Next, you’ll debug the application to look for errors and test that both message boxes appear correctly.

In this step, you’ll debug the application to verify whether it works correctly. For more information, see Building a WPF Application (WPF) and Debugging WPF.

To debug HelloWPFApp

  1. Start the debugger.

    Start Debugging command on the Debug menu

    The debugger starts, and several tool windows appear. On the bottom of the IDE, the Autos, Locals, and Watch windows are docked together on the left side, and the Call Stack, Breakpoints, Command, Immediate, and Output windows are docked together on the right side. The IntelliTrace window is docked with Solution Explorer on the right side of the IDE.

    Figure 9: Debugging Tool Windows

    Greetings form in debug mode

    In this case, the Exception Assistant Dialog Box also appears, indicating that an IOException has occurred: Cannot locate resource ‘mainwindow.xaml’.

  2. In the Exception Assistance dialog box, choose the OK button, and then stop the debugger.

    Stop Debugging command on the Debug menu

Because we renamed Mainwindow.xaml to Greetings.xaml at the start of this walkthrough, the solution is still pointing to mainwindow.xaml as the startup URI for the application. Therefore, Visual Studio can’t build the project.

To specify Greetings.xaml as the startup URI for Visual Basic

  1. Open the Project Designer.

    HelloWPFApp properties command on the Project menu

    The Project Designer appears, with the Application page showing.

  2. In the Startup URI list, choose Greetings.xaml, and then save the changes.

To specify Greetings.xaml as the startup URI for Visual C#

  1. In Solution Explorer, open the App.xaml file.

  2. In the XAML view, change the value of StartupUri from StartupUri="MainWindow.xaml" to StartupUri="Greetings.xaml", and then save the changes.

By starting the debugger again, you can verify that this change has fixed the IOException.

  1. Choose the F5 key to start debugging.

    The Greetings window appears in debug mode.

  2. Choose the SHIFT + F5 keys to stop debugging.

For more information, see How to: Modify Project Properties and Configuration Settings

By adding some breakpoints, you can test the code during debugging. You can add breakpoints by choosing Debug, Toggle Breakpoint on the menu bar or by clicking in the left margin of the editor next to the line of code where you want the break to occur.

To add breakpoints

  1. Open Greetings.xaml.vb or Greetings.xaml.cs, and then perform one of the following steps:

    • For Visual Basic, place focus on the following line: MessageBox.Show("Hello.")

    • For Visual C#, place focus on the following line: MessageBox.Show("Hello.");

  2. Add a breakpoint from the menu bar.

    Toggle Breakpoint command on the Debug menu

    A red circle appears next to the line of code in the far left margin of the editor window.

  3. Perform one of the following steps:

    • For Visual Basic, locate the following line: MessageBox.Show("Goodbye.")

    • For Visual C#, locate the following line: MessageBox.Show("Goodbye.");

  4. Choose the F9 key to add a breakpoint, and then choose the F5 key to start debugging.

  5. In the Greetings window, choose the Hello radio button, and then choose the Display button.

    When the program stops running, the IntelliTrace window provides detailed information, including that the Display button has been chosen and that a breakpoint has been encountered. The Call Stack window is updated to show the current context of the application.

    Figure 11: Call Stack and IntelliTrace windows

    Break mode while debugging the Greetings form
  6. On the menu bar, choose Debug, Step Out.

    The application resumes execution, and a message box with the word “Hello” appears.

  7. Choose the OK button to close the message box.

    Notice that the IntelliTrace window continues to track what is happening in the code.

  8. In the Greetings window, choose the Goodbye radio button, and then choose the Display button.

    Again, the application stops running, and the information in the Call Stack and IntelliSense windows is updated.

  9. On the menu bar, choose Debug, Step Out, and then choose the OK button to close the message box.

  10. Choose the SHIFT + F5 keys to stop debugging.

  11. On the menu bar, choose Debug, Disable All Breakpoints.

Now that you’ve verified that everything works, you can prepare a release build of the application.

To clean the solution files and build a release version

  1. From the menu bar, delete intermediate files and output files that were created during previous builds.

    The Clean Solution command on the Build menu
  2. Change the build configuration for HelloWPFApp from Debug to Release.

    The Standard toolbar with Release selected
  3. Build the solution.

    When you start Visual Studio for the first time, you must choose a settings combination that applies a set of pre-defined customizations to the IDE. Each settings combination has been designed to make it easier for you to develop applications.

    Figure 1: Choose Default Environment Settings dialog box

    Choose Default Environment Settings dialog box

    This walkthrough is written with General Development Settings applied, which applies the least amount of customization to the IDE. You can change your settings combination by using the Import and Export Settings Wizard. For more information, see How to: Change Select Settings.

    After you open Visual Studio, you can identify the three basic parts of the IDE: tool windows, menus and toolbars, and the main window space. Tool windows are docked on the left and right sides of the application window, with Quick Launch, the menu bar, and the standard toolbar at the top. In the center of the application window is the Start Page. When a solution or project is loaded, editors and designers appear in this space. When you develop an application, you’ll spend most of your time in this central area.

    Figure 2: Visual Studio IDE

    IDE with General Settings Applied

    By using the Options dialog box, you can make additional customizations to Visual Studio, such as changing the font face and size that’s used for text in the editor or the color theme that’s applied to the IDE. Depending on the settings combination that you’ve applied, some items in that dialog box might not appear automatically. You can make sure that all possible options appear by choosing the Show all settings check box.

    Figure 3: Options dialog box

    Options dialog box wirh Show all settings option

    In this example, you’ll change the color theme of the IDE from light to dark.

    To change the color theme of the IDE

    1. Open the Options dialog box.

      Options command on the Tools menu
    2. Change the Color theme to Dark.

      Dark color theme selected

    The colors in Visual Studio should match the following image:

    IDE with Dark Theme applied

    To return the IDE to the Light color theme, repeat the previous steps, and except choose Light in step 2. For more information, see Customizing the Development Environment.

    When you create an application in Visual Studio, you first create a project and a solution. For this example, you’ll create a Windows Presentation Foundation application.

    To create the WPF project

    1. On the menu bar, choose File, New, Project.

      On the menu bar, choose File, New, Project

      Or use Quick Launch to open the New Project dialog box.

      In the Quick Launch box, specify new project
    2. Choose the Visual Basic or the Visual C# WPF Application template, and then name the project HelloWPFApp.

      Create a Visual Basic WPF Project, HelloWPFApp

      OR

      Create a Visual C# WPF Project, HelloWPFApp

    The HelloWPFApp project and solution, with the basic files for a WPF Application, are created and automatically loaded into Solution Explorer. The MainWindow.xaml file and either MainWindow.xaml.vb or MainWindow.xaml.cs are opened in the WPF Designer, depending on whether you chose Visual Basic or Visual C#. The following items appear in Solution Explorer:

    Figure 5: Project items

    Solution Explorer with HelloWPFApp files loaded

    After you create the project, you can start to customize it. By using the Properties window, you can display and change options for project items, controls, and other items in an application. By using the Project Designer or Property Pages, you can display and change options for projects and solutions.

    In the next example, you’ll give MainWindow a more specific name.

    To change the name of MainWindow.xaml

    1. In Solution Explorer, open the Properties window for MainWindow.xaml, and change the File Name property to Greetings.

      Properties window with File Name highlighted
      Note Note

      This change will cause an error, which you’ll fix later in this walkthrough.

    2. In the WPF Designer, choose the MainWindow form.

    3. In the Properties window, change the value of the Title property to Greetings.

    Solution Explorer shows that you’ve changed the name of the MainWindow.xaml file to Greetings.xaml and that you’ve changed the name of MainWindow.xaml.vb or MainWindow.xaml.cs to Greetings.xaml.vb or Greetings.xaml.cs. In addition, the title bar for MainWindow.xaml now reads Greetings.

    The WPF application that you’re creating contains three types of controls: a TextBlock control, two RadioButton controls, and a Button control. The WPF Designer shows a design view and a XAML view of Greetings.xaml simultaneously in what’s called a split view. You can use this view to interact with both the visual elements and the markup of your XAML files without having to switch tabs in the designer. For more information, see WPF Designer for Windows Forms Developers.

    To start creating the UI for your application, you can add a TextBlock control to Greetings.xaml.

    To add a TextBlock control

    1. In the Toolbox, search for the TextBlock control.

      Toolbox with the TextBlock control highlighted
    2. Add a TextBlock control to the design surface, and center the control near the top of the window.

    Your window should resemble the following illustration:

    Figure 7: Greetings window with TextBlock control

    TextBlock control on the Greetings form

    As the XAML view shows, the following XAML markup has been added for the control:

    <TextBlock HorizontalAlignment="Center" TextWrapping="Wrap" VerticalAlignment="Center" RenderTransformOrigin="4.08,2.312" Margin="237,57,221,238"><Run Text="TextBlock"/><InlineUIContainer><TextBlock TextWrapping="Wrap" Text="TextBlock"/>
    

    You can edit control properties in several ways such as directly in the XAML view, in the Properties window, and in some cases, by using commands on the shortcut menu. For this TextBlock control, you’ll modify the display text directly in the XAML view.

    To customize the text in the text block

    1. In the XAML view, locate the markup for TextBlock, and change the value of Text=”TextBlock” to Text=”Select a message option and then choose the Display button.”

    2. In the Design view, enlarge the TextBlock control so that all of the text that you’ve specified appears, and then save your changes.

    Next, you’ll add two RadioButton controls to the form.

    To add radio buttons

    1. In the Toolbox, search for the RadioButton control.

      Toolbox window with RadioButton control selected
    2. Add two RadioButton controls to the design surface, and move them so that they appear under the TextBlock control.

      Your window should resemble this example:

      Figure 8: RadioButtons in the Greetings window.

      Greetings form with textblock and two radiobuttons
    3. In the Properties window for the left RadioButton control, change the Name property to RadioButton1.

    4. In the Properties window for the right RadioButton control, change the Name property to RadioButton2, and then save your changes.

    You can now add display text for each RadioButton control. The following procedure updates the Content property for a RadioButton control.

    To add display text for each radio button

    1. On the design surface, open the shortcut menu for RadioButton1, choose Edit Text, and then enter Hello.

    2. Open the shortcut menu for RadioButton2, choose Edit Text, and then enter Goodbye.

    The final UI element that you’ll add is a Button control.

    To add the button control

    1. In the Toolbox, search for the Button control, and then add it to the design surface, under the RadioButton controls.

    2. In the XAML view, change the value of Content for the Button control from Content=”Button” to Content=”Display”, and then save the changes.

      The markup should resemble the following example: <Button Content="Display" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="215,204,0,0"/>

    Your window should resemble the following illustration.

    Figure 9: Final Greetings UI

    Greetings form with control labels

    When this application runs, a message box will appear after a user first chooses a radio button and then chooses the Display button. One message box will appear for Hello, and another will appear for Goodbye. To create this behavior, you’ll add code to the Button_Click event in Greetings.xaml.vb or Greetings.xaml.cs.

    To display the message boxes

    1. On the design surface, double-click the Display button.

      Greetings.xaml.vb or Greetings.xaml.cs opens, with the cursor in the Button_Click event.

    2. For Visual Basic, enter the following code:

              If RadioButton1.IsChecked = True Then
                  MessageBox.Show("Hello.")
              Else RadioButton2.IsChecked = True
                  MessageBox.Show("Goodbye.")
              End If
      

      For Visual C#, enter the following code:

                 if (RadioButton1.IsChecked == true)
                 {
                     MessageBox.Show("Hello.");
                 }
                 else
                 {
                     RadioButton2.IsChecked = true;
                     MessageBox.Show("Goodbye.");
      
    3. Save the application.

    Next, you’ll debug the application to look for errors and test that both message boxes appear correctly.

    In this step, you’ll debug the application to verify whether it works correctly. For more information, see Building a WPF Application (WPF) and Debugging WPF.

    To debug HelloWPFApp

    1. Start the debugger.

      Start Debugging command on the Debug menu

      The debugger starts, and several tool windows appear. On the bottom of the IDE, the Autos, Locals, and Watch windows are docked together on the left side, and the Call Stack, Breakpoints, Command, Immediate, and Output windows are docked together on the right side. The IntelliTrace window is docked with Solution Explorer on the right side of the IDE.

      Figure 9: Debugging Tool Windows

      Greetings form in debug mode

      In this case, the Exception Assistant Dialog Box also appears, indicating that an IOException has occurred: Cannot locate resource ‘mainwindow.xaml’.

    2. In the Exception Assistance dialog box, choose the OK button, and then stop the debugger.

      Stop Debugging command on the Debug menu

    Because we renamed Mainwindow.xaml to Greetings.xaml at the start of this walkthrough, the solution is still pointing to mainwindow.xaml as the startup URI for the application. Therefore, Visual Studio can’t build the project.

    To specify Greetings.xaml as the startup URI for Visual Basic

    1. Open the Project Designer.

      HelloWPFApp properties command on the Project menu

      The Project Designer appears, with the Application page showing.

    2. In the Startup URI list, choose Greetings.xaml, and then save the changes.

    To specify Greetings.xaml as the startup URI for Visual C#

    1. In Solution Explorer, open the App.xaml file.

    2. In the XAML view, change the value of StartupUri from StartupUri="MainWindow.xaml" to StartupUri="Greetings.xaml", and then save the changes.

    By starting the debugger again, you can verify that this change has fixed the IOException.

    1. Choose the F5 key to start debugging.

      The Greetings window appears in debug mode.

    2. Choose the SHIFT + F5 keys to stop debugging.

    For more information, see How to: Modify Project Properties and Configuration Settings

    By adding some breakpoints, you can test the code during debugging. You can add breakpoints by choosing Debug, Toggle Breakpoint on the menu bar or by clicking in the left margin of the editor next to the line of code where you want the break to occur.

    To add breakpoints

    1. Open Greetings.xaml.vb or Greetings.xaml.cs, and then perform one of the following steps:

      • For Visual Basic, place focus on the following line: MessageBox.Show("Hello.")

      • For Visual C#, place focus on the following line: MessageBox.Show("Hello.");

    2. Add a breakpoint from the menu bar.

      Toggle Breakpoint command on the Debug menu

      A red circle appears next to the line of code in the far left margin of the editor window.

    3. Perform one of the following steps:

      • For Visual Basic, locate the following line: MessageBox.Show("Goodbye.")

      • For Visual C#, locate the following line: MessageBox.Show("Goodbye.");

    4. Choose the F9 key to add a breakpoint, and then choose the F5 key to start debugging.

    5. In the Greetings window, choose the Hello radio button, and then choose the Display button.

      When the program stops running, the IntelliTrace window provides detailed information, including that the Display button has been chosen and that a breakpoint has been encountered. The Call Stack window is updated to show the current context of the application.

      Figure 11: Call Stack and IntelliTrace windows

      Break mode while debugging the Greetings form
    6. On the menu bar, choose Debug, Step Out.

      The application resumes execution, and a message box with the word “Hello” appears.

    7. Choose the OK button to close the message box.

      Notice that the IntelliTrace window continues to track what is happening in the code.

    8. In the Greetings window, choose the Goodbye radio button, and then choose the Display button.

      Again, the application stops running, and the information in the Call Stack and IntelliSense windows is updated.

    9. On the menu bar, choose Debug, Step Out, and then choose the OK button to close the message box.

    10. Choose the SHIFT + F5 keys to stop debugging.

    11. On the menu bar, choose Debug, Disable All Breakpoints.

    Now that you’ve verified that everything works, you can prepare a release build of the application.

    To clean the solution files and build a release version

    1. From the menu bar, delete intermediate files and output files that were created during previous builds.

      The Clean Solution command on the Build menu
    2. Change the build configuration for HelloWPFApp from Debug to Release.

      The Standard toolbar with Release selected
    3. Build the solution.

      When you start Visual Studio for the first time, you must choose a settings combination that applies a set of pre-defined customizations to the IDE. Each settings combination has been designed to make it easier for you to develop applications.

      Figure 1: Choose Default Environment Settings dialog box

      Choose Default Environment Settings dialog box

      This walkthrough is written with General Development Settings applied, which applies the least amount of customization to the IDE. You can change your settings combination by using the Import and Export Settings Wizard. For more information, see How to: Change Select Settings.

      After you open Visual Studio, you can identify the three basic parts of the IDE: tool windows, menus and toolbars, and the main window space. Tool windows are docked on the left and right sides of the application window, with Quick Launch, the menu bar, and the standard toolbar at the top. In the center of the application window is the Start Page. When a solution or project is loaded, editors and designers appear in this space. When you develop an application, you’ll spend most of your time in this central area.

      Figure 2: Visual Studio IDE

      IDE with General Settings Applied

      By using the Options dialog box, you can make additional customizations to Visual Studio, such as changing the font face and size that’s used for text in the editor or the color theme that’s applied to the IDE. Depending on the settings combination that you’ve applied, some items in that dialog box might not appear automatically. You can make sure that all possible options appear by choosing the Show all settings check box.

      Figure 3: Options dialog box

      Options dialog box wirh Show all settings option

      In this example, you’ll change the color theme of the IDE from light to dark.

      To change the color theme of the IDE

      1. Open the Options dialog box.

        Options command on the Tools menu
      2. Change the Color theme to Dark.

        Dark color theme selected

      The colors in Visual Studio should match the following image:

      IDE with Dark Theme applied

      To return the IDE to the Light color theme, repeat the previous steps, and except choose Light in step 2. For more information, see Customizing the Development Environment.

      When you create an application in Visual Studio, you first create a project and a solution. For this example, you’ll create a Windows Presentation Foundation application.

      To create the WPF project

      1. On the menu bar, choose File, New, Project.

        On the menu bar, choose File, New, Project

        Or use Quick Launch to open the New Project dialog box.

        In the Quick Launch box, specify new project
      2. Choose the Visual Basic or the Visual C# WPF Application template, and then name the project HelloWPFApp.

        Create a Visual Basic WPF Project, HelloWPFApp

        OR

        Create a Visual C# WPF Project, HelloWPFApp

      The HelloWPFApp project and solution, with the basic files for a WPF Application, are created and automatically loaded into Solution Explorer. The MainWindow.xaml file and either MainWindow.xaml.vb or MainWindow.xaml.cs are opened in the WPF Designer, depending on whether you chose Visual Basic or Visual C#. The following items appear in Solution Explorer:

      Figure 5: Project items

      Solution Explorer with HelloWPFApp files loaded

      After you create the project, you can start to customize it. By using the Properties window, you can display and change options for project items, controls, and other items in an application. By using the Project Designer or Property Pages, you can display and change options for projects and solutions.

      In the next example, you’ll give MainWindow a more specific name.

      To change the name of MainWindow.xaml

      1. In Solution Explorer, open the Properties window for MainWindow.xaml, and change the File Name property to Greetings.

        Properties window with File Name highlighted
        Note Note

        This change will cause an error, which you’ll fix later in this walkthrough.

      2. In the WPF Designer, choose the MainWindow form.

      3. In the Properties window, change the value of the Title property to Greetings.

      Solution Explorer shows that you’ve changed the name of the MainWindow.xaml file to Greetings.xaml and that you’ve changed the name of MainWindow.xaml.vb or MainWindow.xaml.cs to Greetings.xaml.vb or Greetings.xaml.cs. In addition, the title bar for MainWindow.xaml now reads Greetings.

      The WPF application that you’re creating contains three types of controls: a TextBlock control, two RadioButton controls, and a Button control. The WPF Designer shows a design view and a XAML view of Greetings.xaml simultaneously in what’s called a split view. You can use this view to interact with both the visual elements and the markup of your XAML files without having to switch tabs in the designer. For more information, see WPF Designer for Windows Forms Developers.

      To start creating the UI for your application, you can add a TextBlock control to Greetings.xaml.

      To add a TextBlock control

      1. In the Toolbox, search for the TextBlock control.

        Toolbox with the TextBlock control highlighted
      2. Add a TextBlock control to the design surface, and center the control near the top of the window.

      Your window should resemble the following illustration:

      Figure 7: Greetings window with TextBlock control

      TextBlock control on the Greetings form

      As the XAML view shows, the following XAML markup has been added for the control:

      <TextBlock HorizontalAlignment="Center" TextWrapping="Wrap" VerticalAlignment="Center" RenderTransformOrigin="4.08,2.312" Margin="237,57,221,238"><Run Text="TextBlock"/><InlineUIContainer><TextBlock TextWrapping="Wrap" Text="TextBlock"/>
      

      You can edit control properties in several ways such as directly in the XAML view, in the Properties window, and in some cases, by using commands on the shortcut menu. For this TextBlock control, you’ll modify the display text directly in the XAML view.

      To customize the text in the text block

      1. In the XAML view, locate the markup for TextBlock, and change the value of Text=”TextBlock” to Text=”Select a message option and then choose the Display button.”

      2. In the Design view, enlarge the TextBlock control so that all of the text that you’ve specified appears, and then save your changes.

      Next, you’ll add two RadioButton controls to the form.

      To add radio buttons

      1. In the Toolbox, search for the RadioButton control.

        Toolbox window with RadioButton control selected
      2. Add two RadioButton controls to the design surface, and move them so that they appear under the TextBlock control.

        Your window should resemble this example:

        Figure 8: RadioButtons in the Greetings window.

        Greetings form with textblock and two radiobuttons
      3. In the Properties window for the left RadioButton control, change the Name property to RadioButton1.

      4. In the Properties window for the right RadioButton control, change the Name property to RadioButton2, and then save your changes.

      You can now add display text for each RadioButton control. The following procedure updates the Content property for a RadioButton control.

      To add display text for each radio button

      1. On the design surface, open the shortcut menu for RadioButton1, choose Edit Text, and then enter Hello.

      2. Open the shortcut menu for RadioButton2, choose Edit Text, and then enter Goodbye.

      The final UI element that you’ll add is a Button control.

      To add the button control

      1. In the Toolbox, search for the Button control, and then add it to the design surface, under the RadioButton controls.

      2. In the XAML view, change the value of Content for the Button control from Content=”Button” to Content=”Display”, and then save the changes.

        The markup should resemble the following example: <Button Content="Display" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="215,204,0,0"/>

      Your window should resemble the following illustration.

      Figure 9: Final Greetings UI

      Greetings form with control labels

      When this application runs, a message box will appear after a user first chooses a radio button and then chooses the Display button. One message box will appear for Hello, and another will appear for Goodbye. To create this behavior, you’ll add code to the Button_Click event in Greetings.xaml.vb or Greetings.xaml.cs.

      To display the message boxes

      1. On the design surface, double-click the Display button.

        Greetings.xaml.vb or Greetings.xaml.cs opens, with the cursor in the Button_Click event.

      2. For Visual Basic, enter the following code:

                If RadioButton1.IsChecked = True Then
                    MessageBox.Show("Hello.")
                Else RadioButton2.IsChecked = True
                    MessageBox.Show("Goodbye.")
                End If
        

        For Visual C#, enter the following code:

                   if (RadioButton1.IsChecked == true)
                   {
                       MessageBox.Show("Hello.");
                   }
                   else
                   {
                       RadioButton2.IsChecked = true;
                       MessageBox.Show("Goodbye.");
        
      3. Save the application.

      Next, you’ll debug the application to look for errors and test that both message boxes appear correctly.

      In this step, you’ll debug the application to verify whether it works correctly. For more information, see Building a WPF Application (WPF) and Debugging WPF.

      To debug HelloWPFApp

      1. Start the debugger.

        Start Debugging command on the Debug menu

        The debugger starts, and several tool windows appear. On the bottom of the IDE, the Autos, Locals, and Watch windows are docked together on the left side, and the Call Stack, Breakpoints, Command, Immediate, and Output windows are docked together on the right side. The IntelliTrace window is docked with Solution Explorer on the right side of the IDE.

        Figure 9: Debugging Tool Windows

        Greetings form in debug mode

        In this case, the Exception Assistant Dialog Box also appears, indicating that an IOException has occurred: Cannot locate resource ‘mainwindow.xaml’.

      2. In the Exception Assistance dialog box, choose the OK button, and then stop the debugger.

        Stop Debugging command on the Debug menu

      Because we renamed Mainwindow.xaml to Greetings.xaml at the start of this walkthrough, the solution is still pointing to mainwindow.xaml as the startup URI for the application. Therefore, Visual Studio can’t build the project.

      To specify Greetings.xaml as the startup URI for Visual Basic

      1. Open the Project Designer.

        HelloWPFApp properties command on the Project menu

        The Project Designer appears, with the Application page showing.

      2. In the Startup URI list, choose Greetings.xaml, and then save the changes.

      To specify Greetings.xaml as the startup URI for Visual C#

      1. In Solution Explorer, open the App.xaml file.

      2. In the XAML view, change the value of StartupUri from StartupUri="MainWindow.xaml" to StartupUri="Greetings.xaml", and then save the changes.

      By starting the debugger again, you can verify that this change has fixed the IOException.

      1. Choose the F5 key to start debugging.

        The Greetings window appears in debug mode.

      2. Choose the SHIFT + F5 keys to stop debugging.

      For more information, see How to: Modify Project Properties and Configuration Settings

      By adding some breakpoints, you can test the code during debugging. You can add breakpoints by choosing Debug, Toggle Breakpoint on the menu bar or by clicking in the left margin of the editor next to the line of code where you want the break to occur.

      To add breakpoints

      1. Open Greetings.xaml.vb or Greetings.xaml.cs, and then perform one of the following steps:

        • For Visual Basic, place focus on the following line: MessageBox.Show("Hello.")

        • For Visual C#, place focus on the following line: MessageBox.Show("Hello.");

      2. Add a breakpoint from the menu bar.

        Toggle Breakpoint command on the Debug menu

        A red circle appears next to the line of code in the far left margin of the editor window.

      3. Perform one of the following steps:

        • For Visual Basic, locate the following line: MessageBox.Show("Goodbye.")

        • For Visual C#, locate the following line: MessageBox.Show("Goodbye.");

      4. Choose the F9 key to add a breakpoint, and then choose the F5 key to start debugging.

      5. In the Greetings window, choose the Hello radio button, and then choose the Display button.

        When the program stops running, the IntelliTrace window provides detailed information, including that the Display button has been chosen and that a breakpoint has been encountered. The Call Stack window is updated to show the current context of the application.

        Figure 11: Call Stack and IntelliTrace windows

        Break mode while debugging the Greetings form
      6. On the menu bar, choose Debug, Step Out.

        The application resumes execution, and a message box with the word “Hello” appears.

      7. Choose the OK button to close the message box.

        Notice that the IntelliTrace window continues to track what is happening in the code.

      8. In the Greetings window, choose the Goodbye radio button, and then choose the Display button.

        Again, the application stops running, and the information in the Call Stack and IntelliSense windows is updated.

      9. On the menu bar, choose Debug, Step Out, and then choose the OK button to close the message box.

      10. Choose the SHIFT + F5 keys to stop debugging.

      11. On the menu bar, choose Debug, Disable All Breakpoints.

      Now that you’ve verified that everything works, you can prepare a release build of the application.

      To clean the solution files and build a release version

      1. From the menu bar, delete intermediate files and output files that were created during previous builds.

        The Clean Solution command on the Build menu
      2. Change the build configuration for HelloWPFApp from Debug to Release.

        The Standard toolbar with Release selected
      3. Build the solution.

        Build Solution command on the Build menu  
نظرات 0 + ارسال نظر
برای نمایش آواتار خود در این وبلاگ در سایت Gravatar.com ثبت نام کنید. (راهنما)
ایمیل شما بعد از ثبت نمایش داده نخواهد شد