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

时间:2016-05-09 05:23:50

标签: c# .net wpf xaml win-universal-app

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

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

请查看以下屏幕截图。

Partial Classes ScreenShot

1 个答案:

答案 0 :(得分:3)

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

<强> MainPage.xaml.cs中:

public partial class MainPage : Page {...}

<强> MainPage.Flash.xaml.cs:

public partial class MainPage {...}

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

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

<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>