Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Load Platform-specific Bitmap Icon

axbeitaxbeit Member ✭✭

Hey there. I cant see an Icon. The Label say "Rendered size = 0 x 0". I dont know where my mistake is. For the purpose of an successful test Im currently running the iOS emulator. Here is the code:

public class PlatformBitmapsPageCode : ContentPage
    {
        string icon;

        public PlatformBitmapsPageCode()
        {

            switch (Device.RuntimePlatform)
            {
                case Device.iOS:
                    icon = "Icon40.png";
                    break;
                case Device.Android:
                    icon = "icon.png";
                    break;
            }

            Image image = new Image
            {
                Source = new FileImageSource
                {

                    File = icon
                   // File = "Icon-Small-40.png"
                },
                HorizontalOptions = LayoutOptions.Center,
                VerticalOptions = LayoutOptions.CenterAndExpand
            };

            Label label = new Label
            {
                FontSize = Device.GetNamedSize(NamedSize.Medium, typeof(Label)),
                HorizontalOptions = LayoutOptions.Center,
            VerticalOptions = LayoutOptions.CenterAndExpand
        };

        image.SizeChanged += (sender, args) =>
        {
            label.Text = String.Format("Rendered size = {0} x {1}", image.Width, image.Height);
        };

        Content = new StackLayout
        {
            Children =
            {
                image,
                label

            }
        };
    }
}

So basically this code should load an Icon and give me the rendered Size of it. But the Icon does not load hence the rendered size is at 0x0.

In the tutorial I got this code from the iOS Icon name should be "Icon-Small-40.png" which should be stored in the Resources folder.
I noticed that this .png is not there. But there are a few Icons in Assets.xcassets/AppIcon.appiconset.
I copied Icon40.png from there into the Resources folder but it did not help.

What can I do to make it work?

Best Answer

Answers

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Kind of an odd, all work, C# sort of approach. Very... 1990's.

    Usually we'd just do this through binding of a string property and the use of OnPlatform.

    Also... You're making all the UI in C# code behind? Again very 1990's.

  • axbeitaxbeit Member ✭✭

    @ClintStLaurent said:
    Kind of an odd, all work, C# sort of approach. Very... 1990's.

    Usually we'd just do this through binding of a string property and the use of OnPlatform.

    Also... You're making all the UI in C# code behind? Again very 1990's.

    Sorry about that.

    Im currently working myself trough this book because I heard it is quite nice for beginning with xamarin.forms. Looks like it is not.

  • seanydaseanyda GBMember ✭✭✭✭✭
    edited January 2019

    Try using HeightRequest and WidthRequest instead

    label.Text = String.Format("Rendered size = {0} x {1}", image. WidthRequest, image. HeightRequest);
    
  • axbeitaxbeit Member ✭✭

    @seanyda said:
    Try using HeightRequest and WidthRequest instead

    label.Text = String.Format("Rendered size = {0} x {1}", image. WidthRequest, image. HeightRequest);
    

    Did not work.

    I would even assume the label.Text has nothing to do with the solution. The problem I have is that the Icon is not loaded (hence the size is 0 x 0)

Sign In or Register to comment.