Xamarin.Forms ImageSource=Device.OnPlatform过时
我正在使用Xamarin的一个名为“表格视图”的示例来测试一个应用程序,遇到了一个过时的部分,ImageSource=Device.OnPlatform,它现在被switch语句替换。这里没有问题,信息丰富。然而,我有一个特殊的问题,看不到这个问题 我正在添加的代码目前已在下面的源代码中注释掉,并将以这种方式编译,当然没有图像。如果删除已注释的部分,第35行将出现错误,missing}。 如果我高亮显示上一次打断下方的花括号,它会知道它的匹配,switch()。如果我突出显示它下面的大括号,它会认为它是顶部publicsearchPage()的一部分。 开关有问题,但我就是看不出来 我希望有人已经遇到了这一点,并可能有一个答案。如果你需要更多细节,请告诉我Xamarin.Forms ImageSource=Device.OnPlatform过时,xamarin,xamarin.forms,switch-statement,imagesource,Xamarin,Xamarin.forms,Switch Statement,Imagesource,我正在使用Xamarin的一个名为“表格视图”的示例来测试一个应用程序,遇到了一个过时的部分,ImageSource=Device.OnPlatform,它现在被switch语句替换。这里没有问题,信息丰富。然而,我有一个特殊的问题,看不到这个问题 我正在添加的代码目前已在下面的源代码中注释掉,并将以这种方式编译,当然没有图像。如果删除已注释的部分,第35行将出现错误,missing}。 如果我高亮显示上一次打断下方的花括号,它会知道它的匹配,switch()。如果我突出显示它下面的大括号,它会
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
}
};
}
}
}
我不认为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为表单库提供的示例-->“表单的TableView”的确切代码
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");
}
}
}
}
这正是它所需要的。我希望Xamarin清楚他们应用程序中的变化,但很高兴这解决了问题。谢谢