我正在与Xamarin合作创建一个应用程序,并且正在从api获取数据并尝试在我的视图中显示它们,更具体地在我的ListView
这是我的xaml
文件
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="Reciper2.View.RecetaView">
<ContentPage.Content>
<StackLayout>
<Label Text="Prueba de compartir datos xamarin"
VerticalOptions="Start"
HorizontalOptions="Center">
</Label>
<Label Text="{Binding nombreReceta, StringFormat='Recetas de {0:F0}'}"
VerticalOptions="Center"
HorizontalOptions="CenterAndExpand" />
<Image Source="{Binding fotoReceta}"
VerticalOptions="Center"
HorizontalOptions="CenterAndExpand">
</Image>
<ListView x:Name="listadoRecetas"
VerticalOptions="Center"
HorizontalOptions="StartAndExpand"
ItemsSource="{Binding RecetasList}">
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Detail="{Binding nombre}"
Text="{Binding calorias}">
</TextCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage.Content>
和我的xaml.cs
文件是这个。
ObservableCollection<RecetaModel> recetasList = new ObservableCollection<RecetaModel>();
public ObservableCollection<RecetaModel> RecetasList { get { return recetasList; } }
public RecetaView()
{
InitializeComponent();
this.BindingContext = this;
listadoRecetas.ItemsSource = recetasList;
getRecipes();
}
/* protected async override void OnAppearing()
{
getRecipes();
base.OnAppearing();
}*/
public async void getRecipes()
{
var tipoReceta = this.BindingContext as RecetasModel;
if(tipoReceta != null)
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri(url));
request.ContentType = "application/json";
request.Method = "GET";
using (WebResponse response = await request.GetResponseAsync())
{
using (Stream stream = response.GetResponseStream())
{
JsonValue jsonDoc = await Task.Run(() => JsonObject.Load(stream));
var jsonString = jsonDoc.ToString();
dynamic objetoRecetas = JsonConvert.DeserializeObject(jsonString);
var listadoRecetas = objetoRecetas.hits;
foreach(dynamic receta in listadoRecetas)
{
dynamic rec = receta.recipe;
string nombre = rec.label;
string imagen = rec.image;
string calorias = rec.calories;
recetasList.Add(new RecetaModel { nombre = nombre, imagen= imagen, calorias = calorias});
}
// await DisplayAlert("Alert", jsonDoc, "OK");
}
}
}
}
这是我的RecetaModel
班。
public class RecetaModel
{
public string nombre { get; set; }
public string imagen { get; set; }
public string calorias { get; set; }
}
但我认为没有任何显示。 任何我做错事的想法吗? 预先感谢。
答案 0 :(得分:0)
由于某种原因,当在<%= ui.select(:gender_type, options_for_select([['Male', 1], ['female', 2]]), {prompt: true}, {class: ''}) %>
事件中调用我的方法时,此方法起作用了
OnAppearing