官术网_书友最值得收藏!

Understanding the WPF project structure

Once the project is created from the selected template, you will see that the project has few files in it. Among them, you will find two XAML files (App.xaml and MainWindow.xaml) and two associated C# files (App.xaml.cs and MainWindow.xaml.cs) automatically created by Visual Studio:

The App.xaml file is the declarative start point of your application, which extends the Application class and subscribes to application-specific events, unhandled exception, and more. From this class, it gets the starting instruction to navigate to the initial window or page.

Here's the structure of the App.xaml file:

<Application x:Class="Demo.WPF.FirstApp.App" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:local="clr-namespace:Demo.WPF.FirstApp" 
StartupUri="MainWindow.xaml"> 
  <Application.Resources> 
  </Application.Resources> 
</Application> 

Here you can see that StartupUri is set to MainWindow.xaml, which means that the MainWindow.xaml file will launch on startup. If you want to launch a different window/page at startup, replace the same with the desired filename.

Instead of defining StartupUri in the App.xaml file, you can also dynamically set it by subscribing to the application Startup event.

<Application x:Class="Demo.WPF.FirstApp.App" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:local="clr-namespace:Demo.WPF.FirstApp" 
Startup="Application_Startup"> 
  <Application.Resources> 
  </Application.Resources> 
</Application> 

Here is the associated code behind, implementing the Startup event to launch a window named MainWindow on the user screen:

using System.Windows; 
namespace Demo.WPF.FirstApp 
{ 
  /// <summary> 
  /// Interaction logic for App.xaml 
  /// </summary> 
  public partial class App : Application 
  { 
    private void Application_Startup(object sender, 
StartupEventArgs e) { var mainWindow = new MainWindow(); mainWindow.Show(); } } }

The other file, MainWindow.xaml, by default consists of a blank Grid (one of the WPF panels), where you can specify your layouts and put your desired controls to design the UI:

<Window x:Class="Demo.WPF.FirstApp.MainWindow" 
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  xmlns:local="clr-namespace:Demo.WPF.FirstApp" 
  mc:Ignorable="d" 
  Title="MainWindow" Height="350" Width="525"> 
  <Grid> 
  </Grid> 
</Window> 

The related logics, properties, events, and so on, you can define in the associated code behind class file MainWindow.xaml.cs. It's a partial class and all the autogenerated UI-related initialization is written in the InitializeComponent() method defined in the other partial class MainWindow.g.i.cs file that we have already discussed earlier:

  using System.Windows; 
  namespace Demo.WPF.FirstApp 
  { 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
      public MainWindow() 
      { 
         InitializeComponent(); 
      } 
    } 
  } 

When you build any WPF desktop application project, it generates an .exe file along with .dll (if you have any in-house class libraries and/or third-party libraries) files.

主站蜘蛛池模板: 牟定县| 丰原市| 中卫市| 察隅县| 吐鲁番市| 织金县| 淮滨县| 二手房| 阳曲县| 称多县| 吐鲁番市| 广元市| 左云县| 宜兴市| 峡江县| 贵阳市| 治多县| 武安市| 黄冈市| 杭锦后旗| 许昌县| 吴忠市| 邢台县| 丘北县| 阿尔山市| 延安市| 定州市| 阿拉善盟| 松桃| 萨嘎县| 镇原县| 山阳县| 黑水县| 青河县| 额济纳旗| 景宁| 曲麻莱县| 万盛区| 阳曲县| 霍城县| 晋宁县|