C# 在引用处使用MS Build手动生成失败
我正在学习C#和XAML中的WPF结构。我相信最好的学习方法是不使用任何工具,除了编译器和基本的文本编辑器。不幸的是,由于我拒绝使用VisualStudio,我在运行代码时遇到了相当大的困难。 我的问题:我有一个基本的WPF应用程序,其结构如下:C# 在引用处使用MS Build手动生成失败,c#,.net,wpf,xaml,msbuild,C#,.net,Wpf,Xaml,Msbuild,我正在学习C#和XAML中的WPF结构。我相信最好的学习方法是不使用任何工具,除了编译器和基本的文本编辑器。不幸的是,由于我拒绝使用VisualStudio,我在运行代码时遇到了相当大的困难。 我的问题:我有一个基本的WPF应用程序,其结构如下: +HelloWorld |-app.xaml |-app.proj |-compile.proj |+main |-mainWindow.xaml |+obj |+Debug |-app.g.cs |+main |-mainwindo
+HelloWorld
|-app.xaml
|-app.proj
|-compile.proj
|+main
|-mainWindow.xaml
|+obj
|+Debug
|-app.g.cs
|+main
|-mainwindow.g.cs
感兴趣的文件有mainwindow.xaml
、app.xaml
、app.proj
和compile.proj
。
我首先用以下代码将app.xaml和mainwindow.xaml编译成生成的c#代码:
msbuild compile.proj /tv:4.0
msbuild app.proj /tv:4.0
其中compile.proj
如下所示:
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<UsingTask
TaskName="Microsoft.Build.Tasks.Windows.MarkupCompilePass1"
AssemblyFile="C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\PresentationBuildTasks.dll" />
<Target Name="MarkupCompilePass1Task">
<MarkupCompilePass1
AssemblyName="HelloWorld"
Language="C#"
OutputType="WinExe"
OutputPath="obj\Debug\"
ApplicationMarkup="App.xaml"
PageMarkup="main\mainwindow.xaml"
References="C:\Windows\Microsoft.NET\Framework64\v4.0.30319\System.dll;C:\Windows\Microsoft.NET\Framework64\v4.0.30319\System.Xaml.dll;C:\Windows\Microsoft.NET\Framework64\v4.0.30319\WPF\PresentationCore.dll;C:\Windows\Microsoft.NET\Framework64\v4.0.30319\WPF\PresentationFramework.dll;C:\Windows\Microsoft.NET\Framework64\v4.0.30319\WPF\WindowsBase.dll" />
</Target>
</Project>
app.proj
的代码如下:
<Project DefaultTargets="Compile"
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<UsingTask
TaskName="Microsoft.Build.Tasks.Windows.MarkupCompilePass1"
AssemblyFile="C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\PresentationBuildTasks.dll" />
<PropertyGroup>
<appname>HelloWorld</appname>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
</PropertyGroup>
<ItemGroup>
<Reference Include="C:\Windows\Microsoft.NET\Framework64\v4.0.30319\System.Core.dll" />
<Reference Include="C:\Windows\Microsoft.NET\Framework64\v4.0.30319\System.Xaml.dll" />
<Reference Include="C:\Windows\Microsoft.NET\Framework64\v4.0.30319\WPF\WindowsBase.dll" />
<Reference Include="C:\Windows\Microsoft.NET\Framework64\v4.0.30319\WPF\PresentationCore.dll" />
<Reference Include="C:\Windows\Microsoft.NET\Framework64\v4.0.30319\WPF\PresentationFramework.dll" />
</ItemGroup>
<ItemGroup>
<ApplicationDefinition Include="app.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</ApplicationDefinition>
<Compile Include="G:\Docs\Programming\work\WPF\HelloWorld2\obj\debug\App.g.cs">
<DependentUpon>app.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
<Page Include="main/mainwindow.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Compile Include="G:\Docs\Programming\work\WPF\HelloWorld2\obj\debug\main\mainwindow.g.cs">
<DependentUpon>main\mainwindow.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
</ItemGroup>
<Target Name="Compile">
<CSC
Sources = "@(Compile)"
References="@(Reference)"
OutputAssembly ="$(appname).exe">
<Output
TaskParameter = "OutputAssembly"
ItemName = "EXEFile" />
</CSC>
</Target>
</Project>
在此之后,它列出了以以下内容结尾的功能跟踪:
at HelloWorld.app.Main()
另外两个编写文件是app.xaml
和mainwindow.xaml
app.xaml:
<Application x:Class="HelloWorld2.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="main/mainwindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>
mainwindow.xaml:
<Window x:Class="HelloWorld.mainwindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="mainwindow" Height="350" Width="525">
<Grid>
<TextBox Height="23" Width="120" Margin="78,89,319,207" Name="MainTextBox" TextWrapping="Wrap" VerticalAlignment="Top" HorizontalAlignment="Left"></TextBox>
</Grid>
</Window>
我不知道为什么它找不到合适的xaml文件。我是否使用了不正确的文件结构?我是否在任一msbuild调用上缺少编译选项?感谢您的帮助。MsBuild使用MarkupCompilePass1任务执行初步生成。为什么?因为为了让XamlCompilePass成功,它需要引用已编译二进制文件中的本地类型 与其重新发明轮子,不如检查一下VisualStudio创建的WPF项目文件的结构。您会注意到以下内容,如默认二进制引用和默认属性组值:
<ProjectGuid>{CAFEFCAD-429A-4C61-BD3A-157F042E1FE7}</ProjectGuid>
<OutputType>WinExe</OutputType>
{CAFEFCAD-429A-4C61-BD3A-157F042E1FE7}
温克斯
这将帮助您开始使用MsBuild构建WPF项目
关于Visual Studio和MsBuild的问题,您知道MsBuild现在随Visual Studio而不是.NET Framework提供吗?如果您试图自己完成MsBuild为WPF项目所做的工作,则需要完全克隆,而不是部分克隆。如果您检查Microsoft实现的所有与C#/WPF相关的目标和任务,就会发现原因。这个问题不值得回答,因为对大多数人来说,重新发明这个轮子并不有趣。我会尽量保持我的评论客观公正,以免永远被禁止。我不知道你目前的WPF知识水平如何,但如果你是从零开始,你应该真正专注于学习基于数据绑定的WPF思维,如何处理ItemsControl、DataTemplates、DataTrigger和所有WPF特定的非自动化概念和实践,以及MVVM,不要担心IDE为您做的事情,例如,您的XAML远远不是最佳的。您已经为文本框定义了固定的
宽度
、长度
和边距
,这表明您使用Visual Studio designer创建了该文本框。这本身就是一种不好的做法,您应该手动执行XAML,并学习如何在WPF中正确地进行布局。任何MSBuild技能都不会改变这一点。更不用说您还为它指定了一个名称
,除非您有strong
的理由(例如使用ElementName
绑定或故事板或触发器或另一个以元素为目标的概念),否则不鼓励使用该名称。
<ProjectGuid>{CAFEFCAD-429A-4C61-BD3A-157F042E1FE7}</ProjectGuid>
<OutputType>WinExe</OutputType>