I'm trying to navigate between pages and bind data at same time.
This is what I have tried :
public ICommand GetIdeasCommand
{
get
{
return new Command(async () =>
{
Ideas = await _apiServices.GetIdeasAsync();
await Application.Current.MainPage.Navigation.PushAsync(new IdeasSinglePage(Ideas));
});
}
}
It is supposed the Ideas is a list of arrays I get from the json. But this approach is not helping me since I get a blank page. Also if I call this function inside the page everything is fine.
This post gave me an idea : How to pass a parameter from one Page to another Page in Xamarin.Forms?
My view :
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Ideas.Pages.IdeasSinglePage"
xmlns:vm="clr-namespace:Ideas.ViewModel;assembly=Ideas"
Title="My Page">
<ContentPage.BindingContext>
<vm:IdeasViewModel/>
</ContentPage.BindingContext>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Padding="20, 10">
<Label Text="{Binding Ideas}"
FontSize="12"
TextColor="RoyalBlue"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Code behind :
public partial class IdeasSinglePage : ContentPage
{
public IdeasSinglePage(List<Models.Ideas> ideas)
{
InitializeComponent();
this.BindingContext = new IdeasSinglePage(ideas); //the app breaks here
}
}
Thanks.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…