首页 > 解决方案 > 找不到类型或命名空间名称“应用程序”(您是否缺少 using 指令或程序集引用?)

问题描述

我正在尝试从命令行构建 WPF 应用程序。我正在使用 msbuild 但编译时构建失败。我得到这个错误。

在此处输入图像描述

我试图添加参考程序集,但仍然不起作用。我需要做什么。我不想使用 Visual Studio。

这是我的代码

using System;
using System.Collections.Generic;
using System.Windows;

namespace WpfTutorialSamples
{
    public partial class App : Application
    {

        private void Application_Startup(object sender, StartupEventArgs e)
        {
            // Create the startup window
            MainWindow wnd = new MainWindow();
            // Do stuff here, e.g. to the window
            wnd.Title = "Something else";
            // Show the window
            wnd.Show();
        }
    }
}

这是 xaml 代码

<Application x:Class="WpfTutorialSamples.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             Startup="Application_Startup">
    <Application.Resources></Application.Resources>
</Application>

这是 msbuild 代码

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <ItemGroup>
        <Compile Include="App.xaml.cs" />
    </ItemGroup>

    <Target Name="Build">
        <Csc Sources="@(Compile)"/>
    </Target>
</Project>

我尝试了几个站点和参考资料来学习,也许每个人都喜欢 Visual Studio,但我还没有找到可以使用命令行开发的参考资料。

标签: wpfcompiler-errorsreferencemsbuild.net-assembly

解决方案


通常要从命令行编译项目,我建议使用由 Visual Studio 创建的项目文件。尝试为框架库添加引用,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <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>
    <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

有关使用 an 的其他信息在MSBuild这里:使用 MSBuild


推荐阅读