C#XAMARIN绑定图像(ListView ImageCell)

时间:2018-02-07 14:15:13

标签: c# xamarin xamarin.forms

我是Xamarin和Xamarin表格的新手,我需要一些帮助。

我有一种带有文字和图片的新闻。已为此表单配置了图像传输。

我正在尝试创建一个带有新闻列表的新表单,其中包含靠近文本的小图像预览。我想使用相同的图像传输,但我无法将其绑定到XAML ImageCell。我怎么能这样做?

XAML选择了新闻表格

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="Project.Mobile.Client.Portable.Views.News.NewsReadPage"
             xmlns:ffimageloading="clr-namespace:FFImageLoading.Forms;assembly=FFImageLoading.Forms"
             Title="News">
    <ContentPage.Content>
        <ScrollView Padding="10">
            <StackLayout>
                <StackLayout x:Name="ImagesLayout"></StackLayout>
                <Label Text="{Binding Title}"
                       Font="Bold, 20"/>
                <Label Text="{Binding Body}"/>
            </StackLayout>
        </ScrollView>
    </ContentPage.Content>
</ContentPage>

XAML.CS

namespace Project.Mobile.Client.Portable.Views.News
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class NewsReadPage : ContentPage
    {
        public NewsReadPage()
        {
            InitializeComponent();

            var handler = new NativeMessageHandler();
            HttpClient client = new HttpClient(handler);
            var authData = string.Format("{0}:{1}", Settings.Username, Settings.Password);
            var authHeaderValue = Convert.ToBase64String(Encoding.UTF8.GetBytes(authData));
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", authHeaderValue);

            ImageService.Instance.Initialize(new Configuration
            {
                HttpClient = client
            });
        }
        protected override void OnBindingContextChanged()
        {
            base.OnBindingContextChanged();

            if (BindingContext != null)
            {
                this.ImagesLayout.Children.Clear();
                foreach (string path in (BindingContext as Models.News).Pictures.Split(';'))
                    this.ImagesLayout.Children.Insert(0, new CachedImage()
                    {
                        HorizontalOptions = LayoutOptions.Center,
                        VerticalOptions = LayoutOptions.Center,
                        CacheDuration = TimeSpan.FromDays(30),
                        DownsampleToViewSize = true,
                        Source = Settings.ServerUrl + "/api/File/" + path
                    });
            }
        }
    }
}

我如何在ImageSource="{Binding Pictures}"中使用它来获取新的新闻列表?

新的XAML列表

<StackLayout>
    <ListView x:Name="Lst"
                  HasUnevenRows="True"
                  ItemsSource="{Binding items}"
                  ItemTapped="OnItemTapped"
                  IsPullToRefreshEnabled="True">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ImageCell Height="100"
                         Text="{Binding Title}" 
                         Detail="{Binding Body}"
                         ImageSource="{Binding Pictures}"
                         TextColor="Red"
                         DetailColor="Gray">
                </ImageCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</StackLayout>

0 个答案:

没有答案