【问题标题】:xamarin.forms DataBinding and ImageSourcePropertyxamarin.forms 数据绑定和 ImageSource 属性
【发布时间】:2014-11-21 11:24:16
【问题描述】:

在从 Web 获取图像后,我遇到了 Xamarin 表单应用程序在 Android 中崩溃的问题。如果我使用此代码,一切正常:

Image myImage = new Image
{
   Source = ImageSource.FromUri(new Uri(myObj.ImagePath1)),
};

当我使用图像单元格的自定义渲染器获取列表视图的图像时,很快就会崩溃。

这是我的带有数据绑定的列表视图的代码

listView.ItemTemplate = new DataTemplate
  (typeof(MyImageCell))
    Bindings = {
        {TextCell.TextProperty,
            new Binding("MyTitle")},
        {TextCell.DetailProperty,
            new Binding("MyAddress")},
      {ImageCell.ImageSourceProperty,
         new Binding("MyURL")},
  }
};

我是 C# 和 Xamarin 的新手,我不知道如何像第一个示例一样声明它并且仍然在绑定代码中使用它。我尝试了许多不同的方法,但似乎没有任何效果。希望我缺少一些简单的东西。

干杯

【问题讨论】:

    标签: c# data-binding xamarin.forms imagesource


    【解决方案1】:

    您正在创建复杂的行,对。在这种情况下,您不会使用原始单元格,而是使用具有复杂布局(即 StackLayout)的 ViewCell。这是一个示例,它使用绑定的 Image 和未绑定的 Label 创建 StackLayout(您也可以绑定它)。 My ItemsSource 是一个 Src 实例数组(可能是您的 MyImageCell),其中 Src.Source 是一个包含图像 URL 的字符串。

            ListView lv = new ListView();
            lv.ItemTemplate = new DataTemplate(() =>
                {
                    Image image = new Image();
                    image.SetBinding(Image.SourceProperty, "Source");
    
                    return new ViewCell
                    {
                        View = new StackLayout
                        {
                            Children = {
                                image, new Label{ Text = "Tubo"}
                            }
                        }
                    };
                });
            lv.ItemsSource = new Src[] { new Src(), new Src() };
            ...
    
    public class Src
    {
        public string Source { get; set; }
    
        public Src()
        {
            Source = "http://blog.rthand.com//images/Logo_vNext.png";
        }
    }
    

    【讨论】:

    • 感谢您的回复。这似乎没有任何区别。似乎它需要被包装在 ImageSource.FromUri(new Uri(ImagePath)) 我尝试在对象中有一个图像,它使用对象的字符串 ImagePath 但无法让它工作
    • 路径作为字符串就足够了,因为存在隐式转换。也许你有另一个问题? developer.xamarin.com/guides/cross-platform/xamarin-forms/…
    • 必须。谢谢米哈。会更仔细地观察
    • 它正在为列表获取相当多的图像。你认为缓存它们会有帮助吗?
    • 我的问题好像和这里一样-stackoverflow.com/questions/25807196/…
    猜你喜欢
    • 2016-09-30
    • 1970-01-01
    • 1970-01-01
    • 2014-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-28
    • 1970-01-01
    相关资源
    最近更新 更多