1. 概要
古人云: 工欲善其事,必先利其器.
學習一種新的開發, 就要先了解其開發工具 和 項目結構. 目前WPF 項目的開發工具 常用的是 Visual Studio, 也有其他開發工具如: Rider. 這裏使用微軟官方的開發工具 – Visual Studio(以下簡稱VS)
2. 新建項目/解決方案
打開 VS, 新建項目, 頁面如下:
選擇 C#語言 Windows平臺 桌面項目, 這裏選擇創建 .Framework 應用:
點擊 下一步, 填寫 項目名稱, 選擇框架版本. 如果是單獨項目解決方案 就勾選 放置在同一個目錄, 如果是多項目的解決方案 就不勾選.
下圖爲 解決方案和項目放在同一個目錄
下圖爲 不勾選 解決方案和項目放在同一個目錄中
點擊創建, VS 會自動創建新項目.
3. 解決方案和項目的目錄結構
創建WPF 項目以後, 切換到 解決方案資源管理查看項目結構:
下圖爲 解決方案和項目不同目錄的默認目錄結構圖
下圖爲解決方案和項目同一個目錄的目錄結構圖
從上圖基本看不出差別, 唯一的差別在於解決方案的標題提: “解決方案"FirstWPF”" 後面的說明:
- 解決方案"FirstWPF"(1個項目/共1個)
- 解決方案"FirstWPF"(1個項目)
但是從資源管理器查看文件夾,可以看出不同:
從上圖可以看出區別是 將 .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 的 解決方案資源管理器中 , 可以切換顯示模式:
上圖中我們可以看到 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 : 默認的第一個界面的代碼文件
- bin/ : 用來保存項目生成後的程序集, 一般有 Debug 和 Release 兩個版本.我們可以通過:項目屬性—>配置屬性—>輸出路徑來修改
- FirstWPF : 項目的名字
在解決方案資源管理器 切換到工程模式顯示如下:
雙擊 Properties
會顯示 項目配置的 UI 界面:
雙擊Resources.resx
會顯示資源管理視圖: 資源管理視圖
雙擊 Settings.settings
會顯示設置視圖:
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 說明文檔中:
2. PropertyGroup
接着的三個 PropertyGroup 分別配置了項目的構建類型: Debug | Release, 包括了:
- Configuration : 配置名稱
- Platform : 運行平臺
- ProjectGuid : 項目的guid
- OutputType: 輸出類型 , win exe
- RootNamespace : 根命名空間的名字
- AssemblyName : 程序集名稱
等等一些配置信息. 我們打開配置管理器可以看到 UI界面:
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();
}
}
}
6. 後記
至此, WPF項目的基本結構說明就到此, 後續根據項目需要可以增加修改文件和內容, 如添加 多個資源文件參見另一篇文章: WPF APP項目添加資源文件夾 - DevWiki
歡迎關注我的公衆號獲取最新的文章, 或者 移步我的博客