g.i.cs 个文件丢失,类 不再包含 InitializeComponent 的定义

g.i.cs files missing, classes no longer contain a definition for InitializeComponent

我一直在业余时间开发一个UWP项目,以掌握UWP、MVVM和Prism。该项目最初非常经典,没有使用 MVVM 和 Prism,我一直在努力将这 2 个纳入项目。我一直依靠 https://msdn.microsoft.com/en-us/library/gg405484(v=pandp.40).aspx, https://msdn.microsoft.com/en-us/library/gg405494(v=pandp.40).aspx and https://msdn.microsoft.com/en-us/library/ff921098(v=pandp.40).aspx 来完成它。

一些背景:我最初有一个从我的 Mainpage.xamlMainPage.xaml.cs 代码隐藏的直接函数调用,但是在转换为 MVVM 和一个单独的用户控件的过程中,我删除了那个函数调用,这样我就可以稍后使用命令绑定。在我删除它之后,我在 GameRouletteView.g.i.cs 中的某个地方得到了一个错误,这是这个已删除的函数调用的残余,其中 g.i.cs 文件假定它仍然是绑定的。我重建了我的项目,那些 g.i.cs 文件显然被删除了。

我将以下行添加到我的 Usercontrol 视图中,以便添加我的 ViewModel:

xmlns:gameRoulette="using:GameRoulette.DesignViewModels"
xmlns:prism="using:Prism.Windows.Mvvm" 
d:DataContext="{d:DesignInstance gameRoulette:GameRouletteDesignViewModel, IsDesignTimeCreatable=True}"
prism:ViewModelLocator.AutoWireViewModel="True"

完整代码:

<UserControl
    x:Class="GameRoulette.Views.GameRouletteView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:GameRoulette.Views"
    xmlns:gameRoulette="using:GameRoulette.DesignViewModels"
    xmlns:prism="using:Prism.Windows.Mvvm" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300"
    d:DesignWidth="400"
    d:DataContext="{d:DesignInstance gameRoulette:GameRouletteDesignViewModel, IsDesignTimeCreatable=True}"
    prism:ViewModelLocator.AutoWireViewModel="True">
    <Grid Background="White">
        <Button x:Name="btnSelectGames" Content="Click here to select your games"
                HorizontalAlignment="Left" Margin="110,50,0,0"
                VerticalAlignment="Top" Height="40" Width="240"
                Click="{Binding SelectCommand}"/>
        <Button x:Name="btnChooseGame" Content=""
                HorizontalAlignment="Left" Margin="110,150,0,0"
                VerticalAlignment="Top" Width="240" Height="40"
                Click="{Binding ChooseCommand}" IsEnabled="True"/>
        <ProgressRing HorizontalAlignment="Left" Margin="200,100,0,0" 
                      VerticalAlignment="Top" RenderTransformOrigin="1.05,1.983"
                      Height="45" Width="45" IsActive="True" Visibility="{Binding }"/>
        <Image x:Name="imgFileIcon" HorizontalAlignment="Left"
               Height="64" Margin="110,224,0,0"
               VerticalAlignment="Top" Width="64" />
        <TextBlock x:Name="lblFileName" HorizontalAlignment="Left"
                   Margin="179,224,0,0" TextWrapping="Wrap"
                   Text="" VerticalAlignment="Top" Width="171" Height="64"/>
    </Grid>
</UserControl>

它给出了以下错误:

The name "GameRouletteDesignViewModel" does not exist in the namespace "using:GameRoulette.DesignViewModels".

我重建了项目,然后它为我的 3 个 .xaml 文件中的每一个都给出了以下错误:GameRouletteView, App.xamlMainPage.xaml:

'GameRouletteView' does not contain a definition for 'InitializeComponent' and no extension method 'InitializeComponent' accepting a first argument of type 'GameRouletteView' could be found (are you missing a using directive or an assembly reference?)

此外,当我第一次打开项目时,我收到 Intellisense 错误:

[Failure] Could not find file 'C:\Users\username\Source\Repos\GameRoulette\GameRoulette\GameRoulette\obj\ARM\Debug\MainPage.g.i.cs'.
[Failure] Could not find file 'C:\Users\username\Source\Repos\GameRoulette\GameRoulette\GameRoulette\obj\ARM\Debug\Views\GameRouletteView.g.i.cs'.
[Failure] Could not find file 'C:\Users\username\Source\Repos\GameRoulette\GameRoulette\GameRoulette\obj\ARM\Debug\App.g.i.cs'.

我排除的事情:

  1. 我的命名空间是正确的;
  2. 我试过,没用;
  3. 我已经删除了 bin、obj 文件夹和 .suo 文件,但没有修复它;
  4. 我已经关闭并重新打开解决方案,但没有修复它。
  5. 通过 add/repair/remove 程序 window 修复 Visual Studio,没有结果。

我用谷歌搜索了这个错误,但我真的找不到任何我还没有尝试过的东西。

我还注意到我的 NuGet 包丢失了,而且我的包管理器控制台不再识别 NuGet。我也收到此错误:

Microsoft.NETCore.Portable.Compatibility 1.0.0 provides a compile-time reference assembly for mscorlib on UAP,Version=v10.0, but there is no run-time assembly compatible with win10.

我感觉所有这些问题都是相关的,但我无法弄清楚它有什么问题。如上所述,Google 并没有真正提供多少帮助,它提供的帮助也不起作用。

我正在使用 Visual Studio 2015 Community Edition with Update 1。可以在 https://github.com/nzall/GameRoulette.

找到该项目

我已经为这个问题苦苦挣扎了几天,而且,至少就我而言,我很确定它取决于我在 xaml 代码中无意中引入的一些细微错误.或者,更好的是,不完全是错误,而是 xaml 版本不支持我们目标平台的某些内容。在这种情况下,xaml 解析器会失败,并且 g.i.cs 文件(包含由解析器生成的页面和应用 类 的部分,包括 InitializeComponent 方法)不会按预期生成. 我为什么这么认为?

  1. 一开始以为是Visual Studio的问题,所以清了组件缓存,然后把C:\Users{myname}\AppData\Local\Microsoft\VisualStudio.0这个文件夹彻底删除,然后卸载了然后从头开始重新安装VS。我仍然有错误。
  2. 当我尝试构建解决方案时,在错误 windows 中我只看到了一个泛型 'Object reference not set to an instance of an object',但是如果我检查构建输出 window,我可以看出问题所在实际上与 xaml 解析器相关(准确地说:Xaml 内部错误 WMC9999)。
  3. 互联网上关于这个问题的内容不多,一部分是针对面临相同情况的人的一些帮助请求,但我设法找到的仅有的两个页面确实提供了解决方案,都将这个问题与在 xaml 中使用(不同的)不受支持的功能(语法正确但目标平台不支持的代码):see here and here.
  4. 我终于设法通过撤消我的源代码控制存储库的最后更改来消除错误。不幸的是,我对代码做了很多更改,所以我不能说是哪一行代码导致了我的问题。但这个故事的要点是,所有证据都表明它与我的 xaml 代码有关。因此,即使不能排除 xaml 解析器有时可能会出现重大的系统范围的问题,我还是会建议其他面临此问题的人首先快速撤消最后的更改并看看问题是否消失。