【问题标题】:App.xaml.cs UnhandledException when Binding Image URL in Image Source在图像源中绑定图像 URL 时出现 App.xaml.cs UnhandledException
【发布时间】:2014-05-18 20:13:09
【问题描述】:

我有这个 XAML

(...)
<ListBox Height="Auto" Margin="0,0,-20,0" x:Name="post_Images_Grid_list" VerticalAlignment="Top" HorizontalContentAlignment="Stretch" ScrollViewer.VerticalScrollBarVisibility="Disabled" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
                            <ListBox.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <toolkit:WrapPanel />
                                </ItemsPanelTemplate>
                            </ListBox.ItemsPanel>
                            <ListBox.ItemTemplate>
                                <DataTemplate>
                                    <Grid Width="Auto" Height="Auto" Background="#FF#247722" Margin="0,0,10,20">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="19*"/>
                                        </Grid.ColumnDefinitions>
                                        <StackPanel HorizontalAlignment="Left" VerticalAlignment="Top" Orientation="Horizontal" Margin="0">
                                            <Image Source="{Binding image, Mode=OneWay}" Width="110" Height="110" Margin="5" Stretch="UniformToFill" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                                        </StackPanel>
                                    </Grid>
                                </DataTemplate>
                            </ListBox.ItemTemplate>
                        </ListBox> 
(...)

MyPage.xaml.cs 中的这个 C# 代码:

(...)
post_Images_Grid_list.ItemsSource = thePost.images;
(...)

thePost.images 的定义:

public List<BitmapImages> images = new List<BitmapImages>();

还有 BitmapImages 类:

public class BitmapImages
{
    public BitmapImage image { get; set; }

    public BitmapImages(string image_url)
    {
        this.image = new BitmapImage(new Uri(image_url, UriKind.RelativeOrAbsolute));
    }
}

但是xaml页面显示完成后,debug跳转到App.xaml.cs中的Application_UnhandledException方法,是什么问题?

这是List images在运行时的数据:

在我看来一切都是正确的!每个 URL 都存在并且有一个图像,我在网络浏览器中检查了。

感谢您的帮助,我不知道发生了什么!。

谢谢你,Rohit Vats,这里是 e.Exception:

  • e.ExceptionObject {MS.Internal.WrappedException:错误 HRESULT E_FAIL 已从对 COM 组件的调用中返回。 ---> System.Exception:对 COM 组件的调用已返回错误 HRESULT E_FAIL。 在 MS.Internal.XcpImports.CheckHResult(UInt32 小时) 在 MS.Internal.XcpImports.UIElement_Measure(UIElement 元素,Size availableSize) 在 System.Windows.UIElement.Measure(可用大小) 在 Microsoft.Phone.Controls.WrapPanel.MeasureOverride(大小约束) 在 System.Windows.FrameworkElement.MeasureOverride(IntPtr nativeTarget,双 inWidth,双 inHeight,Double& outWidth,Double& outHeight) --- 内部异常堆栈跟踪结束 ---} System.Exception {MS.Internal.WrappedException}

好像和xaml图片元素的大小有关?

【问题讨论】:

  • 在这里发布错误消息。
  • > MSFTInsider.DLL!MSFTInsider.App.Application_UnhandledException(object sender = {MSFTInsider.App}, System.Windows.ApplicationUnhandledExceptionEventArgs e = {System.Windows.ApplicationUnhandledExceptionEventArgs}) Línea 127 C#
  • App.xaml.cs 中的Debugger.Break()。应用代码中没有异常
  • 调试器在 App.xaml.cs 中中断,因为存在一些未处理的错误并使其成为未处理的异常方法。检查e.Exception 的错误消息。
  • 添加到问题中,对于 cmets 来说太长了。

标签: c# wpf xaml exception binding


【解决方案1】:

好吧,我觉得自己很傻:

<Grid Width="Auto" Height="Auto" Background="#FF#247722" (...) />

此处复制粘贴错误:

<Grid Width="Auto" Height="Auto" Background="#FF247722" (...) />

感谢您的帮助。

【讨论】:

    【解决方案2】:

    这通常意味着您的 XAML 存在无法在设计时验证但会在运行时出现的问题。鉴于您的情况,我猜测您添加的行可能会导致 Datagrid 列模板之一以这种方式出错。

    这通常是由于对不存在或不在 XAML 上下文中的样式或事件处理程序的引用。根据我的经验,这在自定义控件开发中很常见。

    您应该从控件中删除样式,它将被确认处理。

    【讨论】:

      猜你喜欢
      • 2013-12-14
      • 2013-01-05
      • 2016-09-15
      • 1970-01-01
      • 2018-10-17
      • 1970-01-01
      • 1970-01-01
      • 2010-10-19
      • 1970-01-01
      相关资源
      最近更新 更多