【问题标题】:UnhandledException when Binding Image URL in LongListSelector在 LongListSelector 中绑定图像 URL 时出现 UnhandledException
【发布时间】:2013-12-14 10:24:12
【问题描述】:
            List<string> images = new List<string>();
            // add some image url to list
            DataContext = images;

我有一个图像 URL 列表,我想使用 LongListSelector 显示它

            <phone:LongListSelector ItemsSource="{Binding}" LayoutMode="List">
                <phone:LongListSelector.ItemTemplate>
                    <DataTemplate>
                        <Image Source="{Binding}" Stretch="UniformToFill" />
                    </DataTemplate>
                </phone:LongListSelector.ItemTemplate>
            </phone:LongListSelector>

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

【问题讨论】:

    标签: c# xaml binding windows-phone-8 longlistselector


    【解决方案1】:

    试试这个代码:
    在xml中:

     <phone:LongListSelector Name="myLLS" LayoutMode="List" Height="300">
                <phone:LongListSelector.ItemTemplate>
                    <DataTemplate>
                        <Image Source="{Binding}" Height="60"/>
                    </DataTemplate>
                </phone:LongListSelector.ItemTemplate>
     </phone:LongListSelector>
    

    在后面的代码中:

     ObservableCollection<Uri> myList = new ObservableCollection<Uri>();
    
      public MainPage()
      {
         InitializeComponent();
    
         myLLS.ItemsSource = myList;
         myList.Add(new Uri("Resources/Image1.png", UriKind.RelativeOrAbsolute));
         myList.Add(new Uri("Resources/Image2.png", UriKind.RelativeOrAbsolute));
      }
    

    我已将 LLS itemssource 绑定到 Uri 的 ObservableCollection。它正在工作。 ObservableCollection 在这里也更好,因为您可以在添加或删除图像时动态更新 LLS。固定 LLS 的高度也很好,没有我有时会出现异常。

    【讨论】:

      【解决方案2】:

      你不绑定到图片 URL,你应该绑定到 BitmapImage

      【讨论】:

        猜你喜欢
        • 2014-05-18
        • 2014-05-30
        • 1970-01-01
        • 2014-03-21
        • 2018-10-17
        • 1970-01-01
        • 1970-01-01
        • 2013-01-05
        • 1970-01-01
        相关资源
        最近更新 更多