【问题标题】:Warning - The parent file 'Views\file.xaml', for the file 'Views\file.xaml.cs' cannot be found in the project file警告 - 在项目文件中找不到文件“Views\file.xaml.cs”的父文件“Views\file.xaml”
【发布时间】:2014-02-27 05:02:33
【问题描述】:

首先,我查看了 StackOverflow.com 上的其他相关帖子,更改 csproj 文件中的路径对这个警告没有任何影响,如下所示。 参考:why do i get this warning .cs file cannot be found in project file 并且: How can I connect xaml and xaml.cs files

我有 MainWindow.xaml 和 MainWindow.xaml.cs。我决定将这两个都移到 Views 文件夹中,因为我不喜欢我的主窗口视图随 csproj 文件和 App.xaml/App.xaml.cs 文件浮动。

基本上:

视图/MainWindow.xaml

视图/MainWindow.xaml.cs

所以我的第一个问题:这是不允许的吗?我们是否要求 MainWindow.xaml 和 MainWindow.xaml.cs 文件与 csproj 文件位于同一位置?

第二个问题:如果不是,那么我还缺少什么,因为我很确定我正确设置了所有路径?

<Page Include="Views\TimersHost.xaml">
  <Generator>MSBuild:Compile</Generator>
  <SubType>Designer</SubType>
</Page>
  <Compile Include="Views\TimersHost.xaml.cs">
  <DependentUpon>Views\TimersHost.xaml</DependentUpon>
  <SubType>Code</SubType>
</Compile>

我什至将它们放在相同的标签中,这样编译器就没有混淆的余地,但它仍然抱怨警告: 在项目文件中找不到文件“Views\TimersHost.xaml.cs”的父文件“Views\TimersHost.xaml”。

警告实际上似乎表明它的 MainWindow.xaml 的父级是找不到的,但技术上没有父级,除非它指的是 csproj 文件本身,这就是为什么我问我们是否需要让 MainWindow.xaml 与 csproj 文件位于同一位置。

例如,在这篇文章中提到 MainWindow.xaml 应该放在 Views 文件夹中,这就是我想要做的: Project structure for MVVM in WPF

所以我很确定我只是错过了一些东西,但不知道它可能是什么,甚至不知道从哪里开始寻找。 甚至我在 App.xaml 中的 StartupUri 也被正确设置为指向该文件:

<Application 
x:Class="TimersXP.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Views/TimersHost.xaml"
/>

也许我需要在 TimersHost.xaml 文件中指定类的路径? x:Class="Views/TimersXP.TimersHost"....不,这没有意义,而且无论如何也不起作用。

这是一个开源项目,所以这里是完整的 csproj 文件源: http://timersxp.codeplex.com/SourceControl/latest#VS2013/TimersXP/TimersXP.csproj (最新版本是当前的)

【问题讨论】:

    标签: c# wpf xaml mvvm code-behind


    【解决方案1】:

    没有对此进行测试,我的回答是推测性的。然而:

    我想DependentUpon 路径是相对于当前文件的路径 - 而不是你假设的项目根目录。尝试将其更改为:

    <DependentUpon>TimersHost.xaml</DependentUpon>
    

    【讨论】:

    • 是的,就是这样! 踢自己 谢谢!好吧,从现在开始,至少其他任何遇到这种情况的人都会知道! :-D
    猜你喜欢
    • 2012-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-23
    • 1970-01-01
    • 2017-12-07
    相关资源
    最近更新 更多