2013-11-01 39 views
3

我有一个视图的XAML文件,其中定义了复杂的3D模型。问题是定义模型的部分非常大,它有0.5 MB以上的文件,并且很难浏览这个文件。有什么方法可以将Model3DGroup移动到另一个文件中,然后将其包含在我的主XAML文件中?我使用SketchUp to XAML转换器,它创建了以<Model3DGroup xmlns="..." xmlns:x="...">作为其根源的文件,因此我几乎可以确定它可以通过一种方便的方式完成。将3D对象数据移动到单独的XAML文件中

我现在的XAML文件看起来像这样:

<UserControl x:Class="BigAtom.Views.WorkspaceView" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:local="clr-namespace:TestProj" 
      mc:Ignorable="d" 
      d:DesignHeight="278" d:DesignWidth="274"> 

    <Grid x:Name="RootVisual" Background="Beige" ClipToBounds="True"> 
     <Canvas Name="DrawingCanvas" Grid.Row="0" Grid.Column="0" Width="0" Height="0" RenderTransform="1,0,0,-1,0,0" Margin="10"> 
      <Viewport3D Name="Object3D" Width="50" Height="50" Margin="-25,-25" ClipToBounds="False"> 
       <Viewport3D.Camera> 
        <OrthographicCamera x:Name="CamMain" Position="0 60 100" LookDirection="0 -60 -100"></OrthographicCamera> 
       </Viewport3D.Camera> 
       <ModelVisual3D> 
        <ModelVisual3D.Content> 
         <DirectionalLight x:Name="DirLightMain" Direction="-1,-1,-1"/> 
        </ModelVisual3D.Content> 
       </ModelVisual3D> 
       <ModelVisual3D x:Name="MyModel"> 
        <ModelVisual3D.Content> 
         <Model3DGroup> 
          <GeometryModel3D> 
           <GeometryModel3D.Geometry> 
            <MeshGeometry3D Positions="-5.6166 10.4050 153.1136 [very long line]" TextureCoordinates="-5.616612 -10.405044 -3.582495 [very long line]" TriangleIndices="164 17 57 [very long line]"/> 
           </GeometryModel3D.Geometry> 
           <GeometryModel3D.Material> 
            <DiffuseMaterial Color="White" Brush="#ffffff"/> 
           </GeometryModel3D.Material> 
           <GeometryModel3D.BackMaterial> 
            <DiffuseMaterial Color="White" Brush="White"/> 
           </GeometryModel3D.BackMaterial> 
          </GeometryModel3D> 
         </Model3DGroup> 
        </ModelVisual3D.Content> 
       </ModelVisual3D> 
      </Viewport3D> 
     </Canvas> 
    </Grid> 
</UserControl> 

,我想实现这样的事情:

<UserControl x:Class="BigAtom.Views.WorkspaceView" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:local="clr-namespace:TestProj" 
      mc:Ignorable="d" 
      d:DesignHeight="278" d:DesignWidth="274"> 

    <Grid x:Name="RootVisual" Background="Beige" ClipToBounds="True"> 
     <Canvas Name="DrawingCanvas" Grid.Row="0" Grid.Column="0" Width="0" Height="0" RenderTransform="1,0,0,-1,0,0" Margin="10"> 
      <Viewport3D Name="Object3D" Width="50" Height="50" Margin="-25,-25" ClipToBounds="False"> 
       <Viewport3D.Camera> 
        <OrthographicCamera x:Name="CamMain" Position="0 60 100" LookDirection="0 -60 -100"></OrthographicCamera> 
       </Viewport3D.Camera> 
       <ModelVisual3D> 
        <ModelVisual3D.Content> 
         <DirectionalLight x:Name="DirLightMain" Direction="-1,-1,-1"/> 
        </ModelVisual3D.Content> 
       </ModelVisual3D> 
       <ModelVisual3D x:Name="MyModel"> 
        <ModelVisual3D.Content> 
         <Model3DGroup --- some kind of reference to the file which keeps the 3D model --- /> 
        </ModelVisual3D.Content> 
       </ModelVisual3D> 
      </Viewport3D> 
     </Canvas> 
    </Grid> 
</UserControl> 

@edit

McGarnagle的回答一般是OK,但在我的情况下,我也有一些DataTemplate定义在我的UserControl.Resources。正因为如此,我不得不创建这样的结构:

<UserControl.Resources> 
    <ResourceDictionary> 
     <DataTemplate DataType="{x:Type local:SomeType}" x:Key="SomeTypeTemplate"> 
      .... 
     </DataTemplate> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="Models.xaml" /> 
     </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</UserControl.Resources> 

回答

2

我想你应该能够为此使用资源字典。添加一个文件,说:“Models.xaml”与定义:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 

    <Model3DGroup x:Key="TheModelContent"> 
     ... 
    </Model3DGroup> 
</ResourceDictionary> 

然后引用您的用户控件文件,并使用StaticResource关键字参考型号:

<UserControl> 
    <UserControl.Resources> 
     <ResourceDictionary> 
      <ResourceDictionary.MergedDictionaries> 
       <ResourceDictionary Source="Models.xaml" /> 
      </ResourceDictionary.MergedDictionaries> 

      <!-- other resources here --> 
     </ResourceDictionary> 
    </UserControl.Resources> 
    ... 

    <ModelVisual3D Content="{StaticResource TheModelContent}"> 
    ... 

</UserControl> 
+0

不幸的是,这不是没有工作。 Visual Studio说'ResourceDictionary'条目需要'x:Key'属性。当我试图给它与另一个XAML中的Model3DGroup相同的密钥时,R#说'ResourceDictionary'“从Models.xaml隐藏了资源。 – PiotrK

+0

我把''移到了'Viewport3D.Resources',现在看起来没有'x:Key'就好了(可能是因为我在'UserControl.Resources中有一些键控资源')。现在“Content =”{...}“存在问题,它表示ResourceDictionary类型的对象无法应用于期望Model3D的属性。 – PiotrK

+0

它的工作原理,请参阅我的答案中的编辑以了解详细信息 – PiotrK

相关问题