I\'m trying to navigate between pages and bind data at same time.
This is what I have tried :
public ICommand GetIdeasCommand
{
get
{
Your problem is obvious, you are passing the data to the ContentPage but you do nothing with it. Generally speaking passing a parameter from one ViewModel
to another ViewModel
is a very simple problem.
Here is an illustration without XAML
:
public class MyFirstPage : ContentPage
{
public MyFirstPage()
{
this.BindingContext = new MyFirstPageViewModel();
}
}
public class MyFirstPageViewModel : INotifyPorpertyChanged
{
public ICommand<List<string>> DownloadDataCmd { get; }
public MyFirstPageViewModel()
{
DownloadDataCmd = new Command<List<string>>(async () => {
var data = await dataService.DownloadData();
await navService.PushAsync(new MySecondPage(data));
});
}
}
public class MySecondPage : ContentPage
{
public MySecondPage(List<string> downloadedData)
{
this.BindingContext = new MySecondPageViewModel(downloadedData);
}
}
public class MySecondPageViewModel : INotifyPropertyChanged
{
public List<string> Data { get; }
public MySecondPageViewModel(List<string> downloadedData)
{
// Do whatever is needed with the data
Data = downloadedData;
}
}
Now, looking at this solution there are few questions:
1. Why not to download the data directly on the second page?
2. Why not to store the data in cache or db if you need it across the app?
Your understanding of BindingContext is lacking. Usually you bind a ViewModel to a BindingContext. What you're doing here
this.BindingContext = new IdeasSinglePage(ideas); //the app breaks here
doesn't make sense.
You are passing as context the page you want to load ? Just delete this line completely. Since in your recent comments you said you didn't want a ViewModel to begin with, what you will do in your CodeBehind is:
public partial class IdeasSinglePage : ContentPage
{
public IdeasSinglePage(List<Models.Ideas> ideas)
{
InitializeComponent();
listViewName.ItemsSource = ideas;
}
}
And in your xml you give your listView a Name. You need this Name for referencing the list on code behind.
Hope it helps