Wpf 将三维对象数据移动到单独的XAML文件中

Wpf 将三维对象数据移动到单独的XAML文件中,wpf,xaml,viewport3d,Wpf,Xaml,Viewport3d,我有一个XAML文件,其中定义了一个复杂的3D模型。问题是定义模型的部分相当大,超过0.5MB,很难浏览文件。是否有一种方法可以将例如Model3DGroup移动到另一个文件中,然后将其包含在我的主XAML文件中?我使用SketchUp-to-XAML转换器创建以为根的文件,因此我几乎可以肯定它可以以一种方便的方式完成 我当前的XAML文件如下所示: <UserControl x:Class="BigAtom.Views.WorkspaceView" xmlns=

我有一个XAML文件,其中定义了一个复杂的3D模型。问题是定义模型的部分相当大,超过0.5MB,很难浏览文件。是否有一种方法可以将例如
Model3DGroup
移动到另一个文件中,然后将其包含在我的主XAML文件中?我使用SketchUp-to-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>

我想你应该可以使用资源字典来做这件事。添加一个包含以下定义的文件,例如“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> 

不幸的是,它不起作用。VisualStudio说
ResourceDictionary
条目需要
x:Key
属性。当我试图给它与另一个XAML中的
Model3DGroup
相同的键时,R#说
ResourceDictionary
“从Models.XAML隐藏资源”。我将
移动到
Viewport3D.Resources
,现在似乎没有
x:key
(可能是因为我的
UserControl.resources
)中有一些键控资源。现在“Content=“{…}”有一个问题,它说ResourceDictionary类型的对象不能应用于需要Model3D的属性。@PiotrK是的,谢谢。我可能应该提到需要“MergedDictionary”的部分。
<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> 
<UserControl>
    <UserControl.Resources> 
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Models.xaml" />
            </ResourceDictionary.MergedDictionaries>

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

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

</UserControl>