在Visual Studio解决方案中重新链接Xaml和代码后端

4

我有一个名为SomePage.xaml的XAML文件。

我犯了一个错误,将其代码后台类从SomePage重构为CardsPage类。

现在XAML文件不再与代码后台链接。

Separate File

如何再次将这些文件链接在一起?

1
它们在.csproj文件中链接,打开文本编辑器并查找一对已链接的副本,然后复制这两个未链接的文件的示例(它只是xml)。 - Charleh
谢谢,我会尝试的。 - Ask Too Much
3个回答

6

如果您打开包含CardsPage的项目文件,您可以将它们重新链接在一起。

您应该在其中有2个部分,其中一个应该是这样的:

<Page Include="CardsPage.xaml">
  <Generator>MSBuild:Compile</Generator>
  <SubType>Designer</SubType>
</Page>

另一部分应该长成这样:

<Compile Include="CardsPage.xaml.cs">
  <DependentUpon>CardsPage.xaml</DependentUpon>
</Compile>

DependentUpon非常重要,它将使xaml文件在IDE中嵌套在下面。
注意:在您的截图中,CardsPage看起来像是重命名为CardsPage.cs了,我建议您将其重新命名为CardsPage.xaml.cs。

希望这能有所帮助,我不知道除了在项目文件中修改外是否还有其他解决方法。


2

0
如果您正在使用Xamarin Forms,请尝试编辑位于Xamarin项目的.xaml文件所在目录中的“.projitems”文件,其中包含.cs文件和.xaml文件之间的关联,如下所示。
<Compile Include="$(MSBuildThisFileDirectory)MyPage.xaml.cs">
  <DependentUpon>MyPage.xaml</DependentUpon>
  <SubType>Code</SubType>
</Compile>


<ItemGroup>
 <EmbeddedResource Include="$(MSBuildThisFileDirectory)MyPage.xaml">
  <SubType>Designer</SubType>
   <Generator>MSBuild:Compile</Generator>
 </EmbeddedResource>
</ItemGroup>

<Compile Update="C:\Projects\MyPage.xaml.cs">
  <DependentUpon>MyPage.xaml</DependentUpon>
</Compile>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接