gpt4 book ai didi

wpf - 将 3D 对象数据移动到单独的 XAML 文件中

转载 作者:行者123 更新时间:2023-12-02 11:57:27 24 4
gpt4 key购买 nike

我有某个 View 的 XAML 文件,其中定义了复杂的 3D 模型。问题是定义模型的部分相当大,超过 0.5 MB,浏览该文件确实很困难。有什么办法可以移动例如 Model3DGroup到另一个文件中,然后将其包含在我的主 XAML 文件中?我使用 SketchUp 到 XAML 转换器,它使用 <Model3DGroup xmlns="..." xmlns:x="..."> 创建文件。作为它们的根源,所以我几乎确信它可以以一种方便的方式完成。

我当前的 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>

@编辑

McGarnagle 的回答总体来说还可以,但就我而言,我也有一些 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>

最佳答案

我想您应该能够为此使用资源字典。添加一个文件,例如“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>

然后在 UserControl 中引用该文件,并使用 StaticResource 关键字引用模型:

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

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

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

</UserControl>

关于wpf - 将 3D 对象数据移动到单独的 XAML 文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19718775/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com