Reputation: 474
I am working on passing data from a View
to a Page
, to be more exact from RecetasView
to RecetaView
, showing the data in RecetaView
works just fine using {Binding}
in my xaml
file, but i want to get the same data in C#.
The way I'm sharing the data:
RecetasView.xaml.cs
var selectedItem = e.Item as RecetasModel;
var recetaView = new RecetaView();
recetaView.bindingContext = selectedItem;
await Navigation.PushAsync(recetaView);
This is what I have tried to get the data:
RecetaView.xaml.cs
var receta = this.BindingContext;
But this crashes my app.
I can't really tell what's the error, because for some reason I can't debug the app in my device, I'm doing this on release.
Is there any other way to get the shared data in C#?
Upvotes: 1
Views: 45
Reputation: 16572
The property BindingContext is of the type object and hence when you get it from the property the RecetasModel
data is actually of the type object and hence you need to cast it to the exact type before using it
var receta = this.BindingContext as RecetasModel;
if(receta != nulll)
{ //use receta }
Upvotes: 1
Reputation: 89204
you need to properly cast BindingContext
var receta = (RecetasModel)this.BindingContext;
Upvotes: 1