Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 58056

Data Binding to an Embedded Resource Image

$
0
0

I'm trying to get some images to display in a listView with a label and an image using the embedded images in the shared project.

If I use something like this the image appears with no issues

calendarImg.Source = ImageSource.FromResource("OWst.Images.calendar.png");

Here's the key pieces of code that I'm using to generate my ListView menu with images. Any tips on what my disconnect here might be would be appreciated.

`class MenuCell : ViewCell
{
public MenuCell()
{
var imageImg = new Image
{
HorizontalOptions = LayoutOptions.Start,
HeightRequest = 40
};
imageImg.SetBinding(Image.SourceProperty, "ImageUri");
var textLbl = new Label
{
HorizontalOptions = LayoutOptions.FillAndExpand,
BackgroundColor = Color.FromHex("EEEEEE"),
TextColor = Color.FromHex("80A7D8"),
VerticalOptions = LayoutOptions.FillAndExpand
};
textLbl.SetBinding(Label.TextProperty, "ItemText");
var layout = new StackLayout()
{
Orientation = StackOrientation.Horizontal,
Children ={
imageImg,
textLbl
}

            };
            View = layout;
        }



    }`

The List DataSource

List<MenuItems> testMenu = new List<MenuItems>()
            {
                new MenuItems("Conference Schedule", "OWst.Images.calendar.png"),
                new MenuItems("Personal Schedule", "OWst.Images.testImg.png"),
            };

The assignment to the ListView

menu.ItemsSource = testMenu;
menu.ItemTemplate = new DataTemplate(typeof(MenuCell));

The Class for the Menu Items Strings

` class MenuItems
{

        public MenuItems(string itemText, string imageUri)
        {
            ItemText = itemText;
            ImageUri = imageUri;
        }
        public string ItemText { get; set; }
        public string ImageUri { get; set; }
    }

}`

Viewing all articles
Browse latest Browse all 58056

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>