如何修改MSVS WPF项目.csproj让App.xaml最后编译?
WPF项目中让App.xaml最后编译的解决方法
问题背景
WPF项目里,App.xaml的资源字典中引用了Views和ViewModels中的类(比如数据模板绑定视图模型与视图),但Visual Studio会优先编译App.xaml,导致清理解决方案后重建时出现大量依赖错误。移除数据模板后编译正常,重新添加也能通过,但清理重建必炸。必须让App.xaml在所有其他代码编译完成后再处理,否则后续新增视图/视图模型会非常麻烦。使用Visual Studio Community 2019,曾尝试修改构建目标但未成功。
依赖关系示例
XAML命名空间声明
xmlns:views="clr-namespace:ProjectName.Views" xmlns:viewmodels="clr-namespace:ProjectName.ViewModels"
数据模板实现
<DataTemplate DataType="{x:Type viewmodels:HomeViewModel}"> <views:HomeView/> </DataTemplate>
解决方法:修改.csproj文件
核心是调整MSBuild的编译顺序,让App.xaml依赖所有其他代码的编译结果,确保最后编译。
操作步骤
- 右键项目 → 卸载项目 → 编辑.csproj文件
- 找到原本的App.xaml编译节点(通常是
ApplicationDefinition标签),删除它 - 添加以下配置:
<!-- 移除默认的App.xaml编译配置 --> <ApplicationDefinition Remove="App.xaml" /> <!-- 手动添加App.xaml,指定依赖所有编译任务 --> <ApplicationDefinition Include="App.xaml"> <Generator>MSBuild:Compile</Generator> <SubType>Designer</SubType> <!-- 让App.xaml在所有常规编译完成后再编译 --> <DependsOn>$(DependsOn);Compile</DependsOn> </ApplicationDefinition>
备选方案(更稳定)
如果上面的方法不生效,试试这种方式,自定义编译目标确保App.xaml在Compile之后执行:
<PropertyGroup> <!-- 将自定义目标加入编译依赖链 --> <CompileDependsOn>$(CompileDependsOn);CompileAppXaml</CompileDependsOn> </PropertyGroup> <Target Name="CompileAppXaml" AfterTargets="Compile"> <!-- 单独编译App.xaml --> <MSBuild Projects="$(MSBuildProjectFile)" Targets="CompileXaml" Properties="ApplicationDefinition=App.xaml" /> </Target> <!-- 移除默认的App.xaml编译项 --> <ApplicationDefinition Remove="App.xaml" />
修改完成后,保存.csproj文件,重新加载项目,清理解决方案后重建即可解决依赖问题。
代码上下文参考
App.xaml
<Application x:Class="ProjectName.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:ProjectName;assembly=ProjectName" xmlns:views="clr-namespace:ProjectName.Views" xmlns:viewmodels="clr-namespace:ProjectName.ViewModels" StartupUri="MainWindow.xaml"> <Application.Resources> <!-- <DataTemplate DataType="{x:Type viewmodels:MainViewModel}"> <views:MainView/> </DataTemplate> --> <DataTemplate DataType="{x:Type viewmodels:NGViewModel}"> <views:NGView/> </DataTemplate> <DataTemplate DataType="{x:Type viewmodels:HomeViewModel}"> <views:HomeView/> </DataTemplate> <!----> </Application.Resources> </Application>
HomeView.xaml.cs
using System; using System.Collections.Generic; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace ProjectName.Views { /// <summary> /// Interaction logic for HomeView.xaml /// </summary> public partial class HomeView : UserControl { public HomeView() { InitializeComponent(); } } }
HomeView.xaml
<UserControl x:Class="ProjectName.Views.HomeView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:ProjectName.Views" mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800"> <Grid Background="Black"> </Grid> </UserControl>
HomeViewModel.cs
using System; using System.Collections.Generic; using System.Text; namespace ProjectName.ViewModels { public class HomeViewModel : BaseViewModel { } }
BaseViewModel.cs
using System; using System.Collections.Generic; using System.Text; namespace Prototype7022.ViewModels { public class BaseViewModel { } }
内容的提问来源于stack exchange,提问作者JoseAyeras
相关产品推荐
相关产品推荐

