【问题标题】:Xamarin.Forms ImageSource = Device.OnPlatform obsoleteXamarin.Forms ImageSource = Device.OnPlatform 已过时
【发布时间】:2017-10-16 16:27:20
【问题描述】:

我正在使用来自 Xamarin 的名为“用于表单的 TableView”的示例来测试应用程序,并遇到了一个过时的部分 ImageSource = Device.OnPlatform 现在已替换为 switch 语句。这里没有问题并且有大量信息但是,我有一个特殊的问题并且看不到问题。

我正在添加的代码目前在下面的源代码中被注释掉,并且可以这样编译,当然没有图像。如果我删除注释部分,我会在第 35 行收到错误,缺少 }。 如果我突出显示最后一个中断正下方的花括号,它就会知道它的匹配,switch()。如果我突出显示正下方的开放花括号,它认为它是顶部的 Public SearchPage() 的一部分。 开关中的某些东西引起了问题,但我看不到它。

我希望有人遇到过这个问题并可能有答案。如果您需要更多详细信息,请告诉我。

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
                }
            };
        }
    }
}

【问题讨论】:

    标签: xamarin xamarin.forms switch-statement imagesource


    【解决方案1】:

    我不认为 c# 支持 switch 这样的对象初始化程序中的语句。解决此问题的最佳方法是将 switch 语句重构为一个方法并使用它来初始化ImageCell

    ImageSource GetSource()
    {
        switch (Device.RuntimePlatform)
        {
            case Device.iOS:
                return ImageSource.FromUri(new Uri("http://xamarin.com/images/index/ide-xamarin-studio.png"));
            case Device.Android:
                return ImageSource.FromFile("waterfront.jpg");
            case Device.WinPhone:
                return ImageSource.FromFile("Images/waterfront.jpg");
            default:
                return ImageSource.FromFile("Images/waterfront.jpg");
        }
    }
    

    并在初始化器中使用它:

    new ImageCell
    {   
        ImageSource = GetSource()
    }
    

    【讨论】:

    • 这正是它所需要的。我希望 Xamarin 清楚他们的应用程序的变化,但很高兴这解决了它。谢谢
    【解决方案2】:

    回答:感谢 Scryptique

    我想发布我用来替换 Xamarin 为 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");
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-31
      • 1970-01-01
      • 1970-01-01
      • 2014-11-21
      • 2021-07-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多