我在业余时间开发了一个 UWP 项目,以掌握 UWP、MVVM 和 Prism。该项目最初非常经典,没有使用 MVVM 和 Prism,我一直在努力将这两个纳入该项目。我一直依赖着https://msdn.microsoft.com/en-us/library/gg405484(v=pandp.40).aspx https://msdn.microsoft.com/en-us/library/gg405484(v=pandp.40).aspx, https://msdn.microsoft.com/en-us/library/gg405494(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 https://msdn.microsoft.com/en-us/library/ff921098(v=pandp.40).aspx努力克服它。
一些背景:我最初有一个来自我的直接函数调用Mainpage.xaml
to the MainPage.xaml.cs
代码隐藏,但在转换为 MVVM 和单独的用户控件期间,我删除了该函数调用,以便稍后可以使用命令绑定。删除该错误后,我在 GameRouletteView.g.i.cs 中的某处收到一个错误,该错误是此删除的函数调用的残余部分,其中 g.i.cs 文件假设它仍然被绑定。我重建了我的项目,那些 g.i.cs 文件显然被删除了。
我将以下几行添加到我的 Usercontrol View 中,以便添加我的 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.xaml
andMainPage.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'.
我已经排除的事情:
- 我的命名空间是正确的;
- 我试过了https://stackoverflow.com/a/27260580/1770430 https://stackoverflow.com/a/27260580/1770430,没有工作;
- 我已经删除了 bin、obj 文件夹和 .suo 文件,但没有修复它;
- 我已经关闭并重新打开解决方案,但没有修复它。
- 通过添加/修复/删除程序窗口修复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 并没有真正提供太多帮助,而且它所提供的也不起作用。
我正在使用带有 Update 1 的 Visual Studio 2015 Community Edition。该项目可以在以下位置找到:https://github.com/nzall/GameRoulette https://github.com/nzall/GameRoulette.