Forum Xamarin.iOS

Displaying an image to the user using c#

Hi all. I've been struggling with this for two days now, and its totally stumped me. I'm new to Xamarin(although quite well versed in c#) But I'm having a major issue displaying an image to the user using c#. Now I've managed to achieve this with ease using Xaml, but my issue is I need to create images dynamically to show to the user, as they are going to be representing the cover of books, and each one of the books are going to have their own properties. My issue, currently, is getting even a single image to show to the user. Here is my current code-

` public partial class MainPage : ContentPage
{
Image bookcover;

    public MainPage()
    {
        bookcover = new Image();
        bookcover.Source = "/Users/sydthecoderkid/Projects/BooksForAll/BooksForAll/BooksForAll.iOS/Resources/BookCover.jpg";
        InitializeComponent();
        bookcover.IsVisible = true;
    }

    async void OnButtonClicked(object sender, EventArgs args)

    {

        await Navigation.PushModalAsync(new MyTabbedPage());
    }
}

}
`
Right now, everything runs fine, but nothing is showing up when I try and run this. I can confirm off the bat that my image is in the resources folder, and that the path is correct, as I've done it easily with Xaml. I just feel like I'm missing a chunk of code that will fix this. Any and all help is much appreciated.

Tagged:

Best Answer

  • Accepted Answer
    @ColeX Ah, I actually solved it a bit before you posted your response- I ended up doing something similar to that though! I made a stacklayout and added children to it, then set the content of the page to the children.

Answers

  • ColeXColeX Member, Xamarin Team Xamurai

    Take a look at Setting Page Content , Content is a property of ContentPage, and is generally set to a single view or a layout with child views , the more important is : this tag can be hidden .

    So the code in xaml

     <Label Text="Hello, XAML!"" />
    

    Equals to

    <ContentPage.Content>
        <Label Text="Hello, XAML!"" />
    </ContentPage.Content>
    

    You have to do the same stuff in code .

    public MainPage()
    {
        bookcover = new Image();
        bookcover.Source = "/Users/sydthecoderkid/Projects/BooksForAll/BooksForAll/BooksForAll.iOS/Resources/BookCover.jpg";
        InitializeComponent();
        bookcover.IsVisible = true;
        Content = bookcover ;
    }
    
  • sydthecoderkidsydthecoderkid Member ✭✭
    Accepted Answer
    @ColeX Ah, I actually solved it a bit before you posted your response- I ended up doing something similar to that though! I made a stacklayout and added children to it, then set the content of the page to the children.
  • ColeXColeX Member, Xamarin Team Xamurai

    @sydthecoderkid If problem solved ,please mark the solution as answer to help others.

  • sydthecoderkidsydthecoderkid Member ✭✭
    @ColeX gotcha, thanks! I’ve marked it.
Sign In or Register to comment.