6

学习WPF之解决方案和项目结构

 3 years ago
source link: http://blog.devwiki.net/index.php/2020/03/14/study-wpf-solution-project-construction.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

古人云: 工欲善其事,必先利其器.

学习一种新的开发, 就要先了解其开发工具 和 项目结构. 目前WPF 项目的开发工具 常用的是 Visual Studio, 也有其他开发工具如: Rider. 这里使用微软官方的开发工具 -- Visual Studio(以下简称VS)

2. 新建项目/解决方案

打开 VS, 新建项目, 页面如下:

image-20200314123658627.png

选择 C#语言 Windows平台 桌面项目, 这里选择创建 .Framework 应用:

image-20200314125329618
image-20200314125329618

点击 下一步, 填写 项目名称, 选择框架版本. 如果是单独项目解决方案 就勾选 放置在同一个目录, 如果是多项目的解决方案 就不勾选.

下图为 解决方案和项目放在同一个目录

image-20200314125617589
image-20200314125617589

下图为 不勾选 解决方案和项目放在同一个目录中

image-20200314142634492
image-20200314142634492

点击创建, VS 会自动创建新项目.

3. 解决方案和项目的目录结构

创建WPF 项目以后, 切换到 解决方案资源管理查看项目结构:

下图为 解决方案和项目不同目录的默认目录结构图

image-20200314142824358.png

下图为解决方案和项目同一个目录的目录结构图

image-20200314143000240
image-20200314143000240

从上图基本看不出差别, 唯一的差别在于解决方案的标题提: "解决方案"FirstWPF"" 后面的说明:

  • 解决方案"FirstWPF"(1个项目/共1个)
  • 解决方案"FirstWPF"(1个项目)

但是从资源管理器查看文件夹,可以看出不同:

image-20200314143535448
image-20200314143535448
image-20200314143704983
image-20200314143704983
image-20200314143946380
image-20200314143946380

从上图可以看出区别是 将 .vs文件夹FirstWPF.sh 文件移动到 和 FirstWPF项目文件夹同一个层级. 其中:

  • FirstWPF.sh : 此文件为解决方案的配置文件
  • FirstWPF.csproj : 此文件为 项目的配置文件

我们打开两个不同创建方式的 解决方案配置文件查看内容, 发现不同的地方:

## 同一个目录的 解决方案部分内容
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. WPF项目的目录结构

在VS 的 解决方案资源管理器中 , 可以切换显示模式:

image-20200314145305593
image-20200314145305593

上图中我们可以看到 WPF 工程的项目结构, 其用途如下:

  • FirstWPF : 解决方案的名字

    • FirstWPF : 项目的名字

      • bin/ : 用来保存项目生成后的程序集, 一般有 Debug 和 Release 两个版本.我们可以通过:项目属性—>配置属性—>输出路径来修改

        • Debug
        • Release
      • obj/ : 保存每个模块的编译结果, 对应的会有 Debug 和 Release两个目录

        • Debug
        • Release
      • Properties : 项目属性文件夹

        • AssemblyInfo.cs : 程序集配置信息文件, 可以修改
        • Resources.Designer.cs : VS 自动生成的资源设计视图, 打开如下面的"资源管理视图"
        • Resources.resx : 和上面的 Resources.Designer.cs 一起组成资源管理视图
        • Settings.Designer.cs : VS自动生成项目配置视图
        • Settings.settings : 应用程序设置允许动态存储和索引应用程序的属性设置和其他信息.
        • App.config : 项目的属性配置文件
      • App.xaml : 项目入口 Application 类的子类的xaml 文件
      • App.xaml.cs : 项目的入口 Application 类的子类
      • FirstWPF.csproj : 项目结构配置文件
      • MainWindow.xaml : 默认的第一个界面的设计文件
      • MainWindow.xaml.cs : 默认的第一个界面的代码文件

在解决方案资源管理器 切换到工程模式显示如下:

image-20200314150625780
image-20200314150625780

双击 Properties 会显示 项目配置的 UI 界面:

image-20200314150704278
image-20200314150704278

双击 Resources.resx会显示资源管理视图: 资源管理视图

image-20200314145847987
image-20200314145847987

双击 Settings.settings 会显示设置视图:

image-20200314150842149
image-20200314150842149

5. WPF项目的接口默认内容

5.1 项目文件 FirstWPF.csproj

打开FristWPF.csproj文件, 内容如下 :

<?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

此属性的含义是 MSBuild 的版本, 此部分说明在官方的 MSBuild 说明文档中:

image-20200314151600460
image-20200314151600460

2. PropertyGroup

接着的三个 PropertyGroup 分别配置了项目的构建类型: Debug | Release, 包括了:

  • Configuration : 配置名称
  • Platform : 运行平台
  • ProjectGuid : 项目的guid
  • OutputType: 输出类型 , win exe
  • RootNamespace : 根命名空间的名字
  • AssemblyName : 程序集名称

等等一些配置信息. 我们打开配置管理器可以看到 UI界面:

image-20200314151839971
image-20200314151839971
image-20200314151915149
image-20200314151915149

3. Reference ItemGroup

下面的 ItemGroup 是引用信息, 默认引用了以下内容:

  • System : 包含用于定义常用值和引用数据类型、事件和事件处理程序、接口、特性以及处理异常的基础类和基类。
  • System.Data: 提供对表示 ADO.NET 结构的类的访问。 通过 ADO.NET,可以生成可有效管理多个数据源的数据的组件。
  • System.Xml : 标准的 xml 支持
  • Microsoft.CSharp : 包含支持使用 C# 语言编译和生成代码的类
  • System.Core : 引用 System.Core.dll
  • System.Xml.Linq : 包含 LINQ to XML 的类。 LINQ to XML 是内存中的 XML 编程接口,可以轻松有效地修改 XML 文档。
  • System.Data.DataSetExtensions : 引用 DataSetExtensions.dll
  • System.Net.Http : 提供用于现代 HTTP 应用程序的编程接口。
  • System.Xaml : 提供与 XAML 读取器和 XAML 编写器相关的类型。
  • WindowsBase: Window 基础dll
  • PresentationCore : 动态链接文件库
  • PresentationFramework : 动态链接文件库

4. 项目文件的 ItemGroup

接着的几个 ItemGroup为 项目的文件索引,包含:

  • ApplicationDefinition : 项目的Application的 xaml和cs文件
  • Page : 项目UI 界面
  • Properties 目录下的文件
  • App.config 文件

5.2 App.config

打开 App.config 文件 可以看到以下内容:

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

配置了 项目启动时所需的 环境为 .NETFramework 4.5

5.3 AssembleInfo.cs

此文件为程序集的配置信息, 包含内容如下:

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

此文件为 Application 的子类, 继承默认的WPF项目类内容, 同时可以扩展自己的配置, 类似于 Android 中的 Application 类. 其内容如下:

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>

在 Appli.Resources Tag里面可以配置 项目级别的资源引用.

App.xaml.cs

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

此类中可以监听应用的启动, 关闭等回调:

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 文件

此文件 为默认的第一个界面的代码, 其布局文件为只包含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>

对应的代码文件包含了初始化处理:

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

至此, WPF项目的基本结构说明就到此, 后续根据项目需要可以增加修改文件和内容, 如添加 多个资源文件参见另一篇文章: WPF APP项目添加资源文件夹 - DevWiki

欢迎关注我的公众号获取最新的文章, 或者 移步我的博客

微信公共号
微信公共号

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK