Learn the solution and project structure of WPF

1. Overview

The ancients said: If a worker wants to do a good job, he must first sharpen his tools.

To learn a new development, you must first understand its development tools and project structure. At present, the development tool for WPF projects is commonly used Visual Studio, and there are other development tools such as: Rider. Here we use Microsoft’s official development tool – Visual Studio (below VS for short)

2. Create a new project/solution

Open VS, create a new project, the page is as follows:

Select the C# language Windows platform desktop project, here choose to create a .Framework application:

image-20200314125329618

Click Next, fill in the project name, and select the framework version. If it is a single project solution, check and place it in the same directory, and if it is a multi-project solution, do not check it.

The following figure shows that the solution and the project are placed in the same directory

image-20200314125617589

The following figure shows that the solution and the project are not checked in the same directory

image-20200314142634492

Click Create, VS will automatically create a new project.

3. Directory structure for solutions and projects

After creating the WPF project, switch to Solution Explorer to view the project structure:

The following figure shows the default directory structure diagram of different directories of solutions and projects

The following figure shows the directory structure diagram of the same directory of the solution and the project

image-20200314143000240

There is basically no difference from the above picture, the only difference is that the title of the solution mentions: "Solution "FirstWPF"" behind the description:

  • Solution "FirstWPF" (1 project/1 total)
  • Solution "FirstWPF" (1 project)

But looking at the folder from the explorer, you can see the difference:

image-20200314143535448

image-20200314143704983

image-20200314143946380

It can be seen from the above figure that the difference is to move the .vs文件夹and files to the same level as the project folder. Among them:FirstWPF.shFirstWPF

  • FirstWPF.sh : This file is the configuration file for the solution
  • FirstWPF.csproj : This file is the configuration file of the project

We opened two solution configuration files created in different ways to view the content and found the difference:

## 同一个目录的 解决方案部分内容
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FirstWPF", "FirstWPF.csproj", "{28D50E34-3BD6-4AF0-8B56-FF05F054DAAF}"
 
## 不同一个目录的 解决方案部分内容
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FirstWPF", "FirstWPF\FirstWPF.csproj", "{13EAA442-8D8A-4848-9996-39F215B9C57D}"

4. Directory structure of WPF project

In the Solution Explorer of VS, the display mode can be switched:

image-20200314145305593

In the figure above, we can see the project structure of the WPF project, and its purpose is as follows:

  • FirstWPF : the name of the solution
    • FirstWPF : the name of the project
      • bin/ : Used to save the assembly after the project is generated, generally there are two versions of Debug and Release. We can modify it through: project properties—>configuration properties—>output path
        • Debug
        • Release
      • obj/ : Save the compilation result of each module, correspondingly there will be two directories of Debug and Release
        • Debug
        • Release
      • Properties : project properties folder
        • AssemblyInfo.cs : Assembly configuration information file, can be modified
        • Resources.Designer.cs : The resource design view automatically generated by VS, open as the following " Resource Management View "
        • Resources.resx : together with the above Resources.Designer.cs form a resource management view
        • Settings.Designer.cs : VS automatically generates the project configuration view
        • Settings.settings : Application Settings allows dynamic storage and indexing of application property settings and other information.
        • App.config : The property configuration file of the project
      • App.xaml : The xaml file of the subclass of the project entry Application class
      • App.xaml.cs : A subclass of the project's entry Application class
      • FirstWPF.csproj : project structure configuration file
      • MainWindow.xaml : The design file of the default first interface
      • MainWindow.xaml.cs : The code file of the default first interface

Switching to the project mode in the solution explorer shows the following:

image-20200314150625780

Double-click Propertiesto display the UI interface of the project configuration:

image-20200314150704278

Double-click Resources.resxto display the resource management view: resource management view

image-20200314145847987

Double-clicking Settings.settingswill display the settings view:

image-20200314150842149

5. The interface default content of the WPF project

5.1 Project file FirstWPF.csproj

Open FristWPF.csprojthe file, the content is as follows:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProjectGuid>{13EAA442-8D8A-4848-9996-39F215B9C57D}</ProjectGuid>
    <OutputType>WinExe</OutputType>
    <RootNamespace>FirstWPF</RootNamespace>
    <AssemblyName>FirstWPF</AssemblyName>
    <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
    <FileAlignment>512</FileAlignment>
    <ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
    <WarningLevel>4</WarningLevel>
    <Deterministic>true</Deterministic>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <PlatformTarget>AnyCPU</PlatformTarget>
    <DebugSymbols>true</DebugSymbols>
    <DebugType>full</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>bin\Debug\</OutputPath>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <PlatformTarget>AnyCPU</PlatformTarget>
    <DebugType>pdbonly</DebugType>
    <Optimize>true</Optimize>
    <OutputPath>bin\Release\</OutputPath>
    <DefineConstants>TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <ItemGroup>
    <Reference Include="System" />
    <Reference Include="System.Data" />
    <Reference Include="System.Xml" />
    <Reference Include="Microsoft.CSharp" />
    <Reference Include="System.Core" />
    <Reference Include="System.Xml.Linq" />
    <Reference Include="System.Data.DataSetExtensions" />
    <Reference Include="System.Net.Http" />
    <Reference Include="System.Xaml">
      <RequiredTargetFramework>4.0</RequiredTargetFramework>
    </Reference>
    <Reference Include="WindowsBase" />
    <Reference Include="PresentationCore" />
    <Reference Include="PresentationFramework" />
  </ItemGroup>
  <ItemGroup>
    <ApplicationDefinition Include="App.xaml">
      <Generator>MSBuild:Compile</Generator>
      <SubType>Designer</SubType>
    </ApplicationDefinition>
    <Page Include="MainWindow.xaml">
      <Generator>MSBuild:Compile</Generator>
      <SubType>Designer</SubType>
    </Page>
    <Compile Include="App.xaml.cs">
      <DependentUpon>App.xaml</DependentUpon>
      <SubType>Code</SubType>
    </Compile>
    <Compile Include="MainWindow.xaml.cs">
      <DependentUpon>MainWindow.xaml</DependentUpon>
      <SubType>Code</SubType>
    </Compile>
  </ItemGroup>
  <ItemGroup>
    <Compile Include="Properties\AssemblyInfo.cs">
      <SubType>Code</SubType>
    </Compile>
    <Compile Include="Properties\Resources.Designer.cs">
      <AutoGen>True</AutoGen>
      <DesignTime>True</DesignTime>
      <DependentUpon>Resources.resx</DependentUpon>
    </Compile>
    <Compile Include="Properties\Settings.Designer.cs">
      <AutoGen>True</AutoGen>
      <DependentUpon>Settings.settings</DependentUpon>
      <DesignTimeSharedInput>True</DesignTimeSharedInput>
    </Compile>
    <EmbeddedResource Include="Properties\Resources.resx">
      <Generator>ResXFileCodeGenerator</Generator>
      <LastGenOutput>Resources.Designer.cs</LastGenOutput>
    </EmbeddedResource>
    <None Include="Properties\Settings.settings">
      <Generator>SettingsSingleFileGenerator</Generator>
      <LastGenOutput>Settings.Designer.cs</LastGenOutput>
    </None>
  </ItemGroup>
  <ItemGroup>
    <None Include="App.config" />
  </ItemGroup>
  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

1. ToolsVersion

The meaning of this attribute is the version of MSBuild, which is described in the official MSBuild documentation:

image-20200314151600460

2. PropertyGroup

The next three PropertyGroups respectively configure the build type of the project: Debug | Release, including:

  • Configuration : configuration name
  • Platform : operating platform
  • ProjectGuid : the guid of the project
  • OutputType: output type, win exe
  • RootNamespace : the name of the root namespace
  • AssemblyName : Assembly name

Wait for some configuration information. We open the configuration manager to see the UI interface:

image-20200314151839971

image-20200314151915149

3. Reference ItemGroup

The following ItemGroup is reference information, and the following content is referenced by default:

  • System : Contains base and base classes for defining common value and reference data types, events and event handlers, interfaces, attributes, and handling exceptions.
  • System.Data: Provides access to classes that represent ADO.NET structures. With ADO.NET, you can generate components that can efficiently manage data from multiple data sources.
  • System.Xml : standard xml support
  • Microsoft.CSharp : Contains classes that support compiling and generating code using the C# language
  • System.Core : quote System.Core.dll
  • System.Xml.Linq : Contains classes for LINQ to XML. LINQ to XML is an in-memory XML programming interface that enables easy and efficient modification of XML documents.
  • System.Data.DataSetExtensions : 引用 DataSetExtensions.dll
  • System.Net.Http : Provides a programming interface for modern HTTP applications.
  • System.Xaml : Provides types related to XAML readers and XAML writers.
  • WindowsBase: Window base dll
  • PresentationCore : Dynamic link file library
  • PresentationFramework : Dynamic link file library

4. ItemGroup of the project file

The next few ItemGroups are the file indexes of the project, including:

  • ApplicationDefinition: The xaml and cs files of the project's Application
  • Page : Project UI interface
  • Files under the Properties directory
  • App.config file

5.2 App.config

Open the App.config file to see the following:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
</configuration>

The environment required for project startup is configured as .NETFramework 4.5

5.3 AssembleInfo.cs

This file is the configuration information of the assembly, which contains the following content:

using System.Reflection;
using System.Resources;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Windows;
 
// 有关程序集的一般信息由以下
// 控制。更改这些特性值可修改
// 与程序集关联的信息。
[assembly: AssemblyTitle("FirstWPF")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("FirstWPF")]
[assembly: AssemblyCopyright("Copyright ©  2020")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
 
// 将 ComVisible 设置为 false 会使此程序集中的类型
//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型
//请将此类型的 ComVisible 特性设置为 true。
[assembly: ComVisible(false)]
 
//若要开始生成可本地化的应用程序,请设置
//.csproj 文件中的 <UICulture>CultureYouAreCodingWith</UICulture>
//例如,如果您在源文件中使用的是美国英语,
//使用的是美国英语,请将 <UICulture> 设置为 en-US。  然后取消
//对以下 NeutralResourceLanguage 特性的注释。  更新
//以下行中的“en-US”以匹配项目文件中的 UICulture 设置。
 
//[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)]
 
 
[assembly: ThemeInfo(
    ResourceDictionaryLocation.None, //主题特定资源词典所处位置
                                     //(未在页面中找到资源时使用,
                                     //或应用程序资源字典中找到时使用)
    ResourceDictionaryLocation.SourceAssembly //常规资源词典所处位置
                                              //(未在页面中找到资源时使用,
                                              //、应用程序或任何主题专用资源字典中找到时使用)
)]
 
 
// 程序集的版本信息由下列四个值组成:
//
//      主版本
//      次版本
//      生成号
//      修订号
//
//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值
//通过使用 "*",如下所示:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

5.4 App.xaml和App.xaml.cs

This file is a subclass of Application, inherits the default WPF project class content, and can expand its own configuration, similar to the Application class in Android. Its content is as follows:

App.xaml

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

Project-level resource references can be configured in the Appli.Resources Tag.

App.xaml.cs

namespace FirstWPF
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : Application
    {
    }
}

In this class, you can monitor the application's startup, shutdown and other callbacks:

namespace FirstWPF
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
        }
 
        protected override void OnExit(ExitEventArgs e)
        {
            base.OnExit(e);
        }
    }
}

5.5 MainWindow file

This file is the code of the default first interface, and its layout file is an empty layout containing only Grid:

<Window x:Class="FirstWPF.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:FirstWPF"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
 
    </Grid>
</Window>

The corresponding code file contains initialization processing:

namespace FirstWPF
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
}

6. Epilogue

So far, the basic structure description of the WPF project is here, and you can add and modify files and content according to the needs of the project. For example, if you add multiple resource files, please refer to another article: Adding resource folders to WPF APP projects - DevWiki

Welcome to pay attention to my official account to get the latest articles, or move to my blog

WeChat public account

Guess you like

Origin blog.csdn.net/DevWiki/article/details/104863671
Recommended