【问题标题】:Gerneral Transform InvalidOperation Exception was unhandled by user code用户代码未处理常规转换 InvalidOperationException
【发布时间】:2012-11-08 21:54:10
【问题描述】:

我遇到了一个我完全不知道的问题。我收到以下错误消息:

指定的 Visual 和 Visual 不共享一个共同的祖先,因此两个视觉之间没有有效的转换。

当用户点击地图上的一个点时调用它。任何帮助,将不胜感激。我附上了错误的sn-ps。如果您需要更多代码示例,请告诉我。

这是我的相关 XAML:

(

<Grid>

<Grid>
 <DataTemplate x:Key="MyFeatureLayerInfoWindowTemplate">
                    <TextBlock Text="Content" Foreground="White" FontSize="12" />
                </DataTemplate>
                <esri:SimpleRenderer x:Key="MySimpleRenderer">
                    <esri:SimpleRenderer.Symbol>
                        <esri:SimpleFillSymbol Fill="#01FFFFFF" BorderBrush="#88000000" BorderThickness="2" />
                    </esri:SimpleRenderer.Symbol>
                </esri:SimpleRenderer>
 </Grid.Resources>

            <esri:Map x:Name="MyMap" Background="#FFE3E3E3" WrapAround="True" MouseClick="MyMap_MouseClick" MouseRightButtonDown="MyMap_MouseRightButtonDown" Extent="-9834972.92753924,4441899.425293319,-9833977.88119163, 4442762.485358352">
                <esri:ArcGISTiledMapServiceLayer ID="MyLayer" Url="http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer" />
                <!--<esri:ArcGISLocalDynamicMapServiceLayer ID="Calvert_City" Path="C:\Users\jessical\Documents\ArcGIS\CalvertCity_Test.mpk"/>-->
                <esri:ArcGISDynamicMapServiceLayer  ID="Calvert_City" Url="http://localhost:6080/arcgis/rest/services/CalvertCity_Test_2/MapServer"/>
                <esri:FeatureLayer ID="MyFeatureLayer"  Url="http://localhost:6080/arcgis/rest/services/CalvertCity_Test_2/MapServer/0"
                            Renderer="{StaticResource MySimpleRenderer}" />
</esri:Map>

            <Border Background="{StaticResource PanelGradient}" BorderThickness="1" CornerRadius="5"
            HorizontalAlignment="Left"  VerticalAlignment="Top"
            Margin="20" Padding="5" BorderBrush="Black" >
                <esri:Legend Map="{Binding ElementName=MyMap}"
                         LayerIDs="Calvert_City"
                         LayerItemsMode="Tree"
                         ShowOnlyVisibleLayers="False"
                         Refreshed="Legend_Refreshed">
                    <esri:Legend.MapLayerTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <CheckBox Content="{Binding Label}"
                                    IsChecked="{Binding IsEnabled, Mode=TwoWay}"
                                    IsEnabled="{Binding IsInScaleRange}" >
                                </CheckBox>
                                <Slider Maximum="1" Value="{Binding Layer.Opacity, Mode=TwoWay}" Width="50" />
                            </StackPanel>
                        </DataTemplate>
                    </esri:Legend.MapLayerTemplate>
                    <esri:Legend.LayerTemplate>
                        <DataTemplate>
                            <CheckBox Content="{Binding Label}"
                            IsChecked="{Binding IsEnabled, Mode=TwoWay}"
                            IsEnabled="{Binding IsInScaleRange}" >
                            </CheckBox>
                        </DataTemplate>
                    </esri:Legend.LayerTemplate>
                </esri:Legend>
            </Border>
            <esri:InfoWindow x:Name="MyInfoWindow"
                         Padding="2"
                         CornerRadius="20"
                         Background="{StaticResource PanelGradient}"
                         Map="{Binding ElementName=MyMap}"
                         ContentTemplate="{StaticResource MyFeatureLayerInfoWindowTemplate}"
                         MouseLeftButtonUp="MyInfoWindow_MouseLeftButtonUp" />

【问题讨论】:

    标签: c# wpf xaml exception gis


    【解决方案1】:

    我在使用 SciChart 的第三方控件时遇到了这个错误。我发现错误被掩盖了,调试器看起来好像没有处理错误。但是,当我刚刚关闭此类异常的 break 并在我的 catch 语句中放置一个断点时,异常并没有落入我的 catch 块中。我对这一切感到有些困惑,但它似乎奏效了。

    所以我的建议是让调试器不要中断这种类型的异常,看看它是如何工作的。

    【讨论】:

    • 嗨 Mike,这里是来自 SciChart 的 Andrew :) 我实际上正在尝试修复 scichart 中的这个错误并登陆了这个页面。我们正在使用此代码GeneralTransform transform = element.TransformToVisual(otherElement);,有时它会抛出。我们尝试捕获并吞下异常。如果您或任何人知道一种方法来测试两个视觉对象是否有共同的祖先,我们可以解决这个问题!
    【解决方案2】:

    您正在使用 TransformToVisual。应该是 TransformToAncestor。

    【讨论】:

    • 然后我收到错误“指定的视觉对象不是祖先。”
    • 这个可视化树是否在应用程序的主窗口中?
    • 嗯。我实际上认为它可能在 DialogWindowBase 中。
    • 然后您将不得不更改代码以转换为该 UIElement 的祖先的任何窗口。尝试使用 VisualTreeHelper 查找包含该元素的窗口,而不是对其进行硬编码。
    • 即使是这样,最好不要将代码绑定到 UI 元素的特定实例(例如引用 Application.Current.MainWindow),而是使用 VisualTreeHelper 查找任何窗口可能是您的控件的容器。
    猜你喜欢
    • 2023-03-10
    • 1970-01-01
    • 2014-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-12
    相关资源
    最近更新 更多