Search code examples
xamarinxamarin.formsswitch-statementimagesource

Xamarin.Forms ImageSource = Device.OnPlatform obsolete


I am using a sample from Xamarin called "TableView for a form" to test an app and came across an obsolete section, ImageSource = Device.OnPlatform which is now replaced with a switch statement. No problems here and plenty of information however, I have a peculiar problem and can not see the issue.

The code I am adding is currently commented out in below source and will compile fine this way, without the image of course. If I remove the commented section, I get an error on line 35, missing }. If I highlight the curly brace just below the last break it is aware of its match, switch(). If I highlight the open curly brace just below that, it thinks it is part of Public SearchPage() at the top. Something in the switch is causing problems but I just can't see it.

I am hoping someone has run into this and may have an answer. Let me know if you need more detail.

using System;
using System.Collections.Generic;
using System.Text;

using Xamarin.Forms;
//using static System.Net.Mime.MediaTypeNames;

namespace MiddleMeeter
{
    class SearchPage : ContentPage
    {
        public SearchPage()
        {
            Label header = new Label
            {
                Text = "TableView for a form",
                FontSize = 30,
                FontAttributes = FontAttributes.Bold,
                HorizontalOptions = LayoutOptions.Center
            };

            TableView tableView = new TableView
            {
                Intent = TableIntent.Form,
                Root = new TableRoot("TableView Title")
                {
                    new TableSection("Table Section")
                    {
                        new TextCell
                        {
                            Text = "Text Cell",
                            Detail = "With Detail Text",
                        },
                        new ImageCell
                        {   
                            /**********************************************************************************************
                            switch (Device.RuntimePlatform)
                            {
                                case Device.iOS:
                                ImageSource.FromUri(new Uri("http://xamarin.com/images/index/ide-xamarin-studio.png"));
                                    break;
                                case Device.Android:
                                    ImageSource.FromFile("waterfront.jpg");
                                    break;
                                case Device.WinPhone:
                                    ImageSource.FromFile("Images/waterfront.jpg");
                                    break;
                                default:
                                    ImageSource.FromFile("Images/waterfront.jpg");
                                    break;
                            },
                            *///////////////////////////////////////////////////////////////////////////////////////////////

                            Text = "Image Cell",
                            Detail = "With Detail Text",
                        },
                         new SwitchCell
                        {
                            Text = "Switch Cell"
                        },
                        new EntryCell
                        {
                            Label = "Entry Cell",
                            Placeholder = "Type text here"
                        },
                        new ViewCell
                        {
                            View = new Label
                            {
                                Text = "A View Cell can be anything you want!"
                            }
                        }
                    },
                }
            };

            // Build the page.
            this.Content = new StackLayout
            {
                Children =
                {
                    header,
                    tableView
                }
            };
        }
    }
}

Solution

  • Answered: Thanks Scryptique

    I wanted to post the exact code I am using to replace the sample provided by Xamarin for the Forms Gallery --> 'TableView for a form'.

    using System;
    using System.Collections.Generic;
    using System.Text;
    
    using Xamarin.Forms;
    //using static System.Net.Mime.MediaTypeNames;
    
    namespace MiddleMeeter
    {
        class SearchPage : ContentPage
        {
            public SearchPage()
            {
                Label header = new Label
                {
                    Text = "TableView for a form",
                    FontSize = 30,
                    FontAttributes = FontAttributes.Bold,
                    HorizontalOptions = LayoutOptions.Center
                };
    
                TableView tableView = new TableView
                {
                    Intent = TableIntent.Form,
                    Root = new TableRoot("TableView Title")
                    {
                        new TableSection("Table Section")
                        {
                            new TextCell
                            {
                                Text = "Text Cell",
                                Detail = "With Detail Text",
                            },
                            new ImageCell
                            {
                                // This is the call to method getSource() 
                                ImageSource = getSource(),
                                Text = "Image Cell",
                                Detail = "With Detail Text",
                            },
                             new SwitchCell
                            {
                                Text = "Switch Cell"
                            },
                            new EntryCell
                            {
                                Label = "Entry Cell",
                                Placeholder = "Type text here"
                            },
                            new ViewCell
                            {
                                View = new Label
                                {
                                    Text = "A View Cell can be anything you want!"
                                }
                            }
                        },
                    }
                };
    
                // Build the page.
                this.Content = new StackLayout
                {
                    Children =
                    {
                        header,
                        tableView,
                    }
                };
    
    
            }
    
            // Method to get the format to retreive the image for Platform specific detaisl
            private ImageSource getSource()
            {
                switch (Device.RuntimePlatform)
                {
                    case Device.iOS:
                        return ImageSource.FromUri(new Uri("https://www.xamarin.com/content/images/pages/branding/assets/xamagon.png"));
                    case Device.Android:
                        return ImageSource.FromFile("Icon.png");
                    case Device.WinPhone:
                        return ImageSource.FromFile("Images/waterfront.jpg");
                    default:
                        return ImageSource.FromFile("Images/waterfront.jpg");
                }
            }
        }
    }