c# - 警告 - 在项目文件中找不到文件 'Views\file.xaml' 的父文件 'Views\file.xaml.cs'

标签 c# wpf xaml mvvm code-behind

首先,我查看了 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 文件夹中,因为我不喜欢我的主窗口 View 随 csproj 文件和 App.xaml/App.xaml.cs 文件一起 float 。

基本上:

Views/MainWindow.xaml

View /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 (最新版本为当前)

最佳答案

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

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

<DependentUpon>TimersHost.xaml</DependentUpon>

关于c# - 警告 - 在项目文件中找不到文件 'Views\file.xaml' 的父文件 'Views\file.xaml.cs',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21531950/

相关文章:

c# - 将 DataTemplate 中的 WPF RelayCommand 绑定(bind)到 UserControl 内的按钮

wpf - 如何更改 TreeViewItem 上三角形图标的颜色/不透明度?

c# - Service Fabric,什么微服务最适合从服务总线进行连续轮询

c# - Linq2Twitter - 什么是 OAuthToken?

c# - 如果类型不同(如 List<T> 和 List<G>),是否可以连接两个列表?

c# - MVVM:将参数从 View 模型传递到 View

c# - 绑定(bind)到列表框时出现 XamlParseException

c# - 允许用户编辑/添加/移动图像中的项目

.net - 有没有办法在不命名目标控件的情况下使用 Wpf 标签的目标?

c# - MVVM 获取/设置与 OOP