如何创建同一个xaml文件的多个部分类

Zea*_*hah 1 .net c# wpf xaml win-universal-app

我需要将代码从单个代码后面分离到多个部分类,但不知道如何实现这一点,我在一个 msdn 代码示例中看到这是可能的,请告诉我如何实现这一点。

我知道我可以转移到 MVVM,但现在我只需要通过部分类来组织代码。

请看下面的截图。

部分类截图

Nko*_*osi 6

您可以通过在您喜欢的命名约定中创建附加文件并将部分类添加到它们来实现。代码将像以前一样编译和工作。

MainPage.xaml.cs:

public partial class MainPage : Page {...}
Run Code Online (Sandbox Code Playgroud)

MainPage.Flash.xaml.cs:

public partial class MainPage {...}
Run Code Online (Sandbox Code Playgroud)

要获得您在附加图像中显示的内容,将涉及手动编辑项目文件。在文本编辑器中打开你的项目文件,看看它是如何为你当前的代码完成的,过程是一样的

这是您提供的图像在项目文件中的外观示例

<ItemGroup>
    <Page Include="MainPage.xaml">
      <SubType>Designer</SubType>
      <Generator>MSBuild:Compile</Generator>
    </Page>
</ItemGroup>
<ItemGroup>
    <Compile Include="MainPage.ExposureValue.xaml.cs">
      <DependentUpon>MainPage.xaml</DependentUpon>
    </Compile>
    <Compile Include="MainPage.Flash.xaml.cs">
      <DependentUpon>MainPage.xaml</DependentUpon>
    </Compile>
    <Compile Include="MainPage.Focus.xaml.cs">
      <DependentUpon>MainPage.xaml</DependentUpon>
    </Compile>
    <Compile Include="MainPage.IsoSpeed.xaml.cs">
      <DependentUpon>MainPage.xaml</DependentUpon>
    </Compile>
    <Compile Include="MainPage.Shutter.xaml.cs">
      <DependentUpon>MainPage.xaml</DependentUpon>
    </Compile>
    <Compile Include="MainPage.WhiteBalance.xaml.cs">
      <DependentUpon>MainPage.xaml</DependentUpon>
    </Compile>
    <Compile Include="MainPage.xaml.cs">
      <DependentUpon>MainPage.xaml</DependentUpon>
    </Compile>
    <Compile Include="MainPage.Zoom.xaml.cs">
      <DependentUpon>MainPage.xaml</DependentUpon>
    </Compile>
</ItemGroup>
Run Code Online (Sandbox Code Playgroud)