I have a simple issue. I'm trying to create my cross-platform app in xamarin, here's the scenario.
I have a page, and on it a list of accommodations. My idea is that when I click an accommodation, it opens a new page with more details about it. It's covered with this event:
private void ListView_ItemTapped(object sender, ItemTappedEventArgs e)
{
var Odabrani = e.Item;
PropertyInfo pi = Odabrani.GetType().GetProperty("SmjestajId");
int id = Convert.ToInt32(pi.GetValue(Odabrani,null).ToString());
Application.Current.MainPage = new SmjestajViewPage(id);
}
SmjestajViewPage would be the Page that is opened, and obviously I need that accommodation's Id to fetch the relevant information about it. This works fine, I'm getting the right id.
This is what I have so far for the xaml.cs file:
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class SmjestajViewPage : ContentPage
{
private int _Id;
SmjestajVM viewmodel = null;
public SmjestajViewPage()
{
InitializeComponent();
}
public SmjestajViewPage(int id)
{
InitializeComponent();
_Id = id;
BindingContext = viewmodel = new SmjestajVM(id);
}
}
This is how SmjestajVM looks so far:
public class SmjestajVM : BaseViewModel
{
int _SmjestajId;
public SmjestajVM(int Id)
{
_SmjestajId = Id;
}
}
However, unless I add a parameter-less constructor, this option does not appear as one of the options in the xaml editing process. This is how my xaml file looks so far:
<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"
xmlns:ViewModels="clr-namespace:RS2_Booking.MobileApp.ViewModels"
mc:Ignorable="d"
x:Class="RS2_Booking.MobileApp.Views.SmjestajViewPage"
>
<ContentPage.BindingContext>
<ViewModels:SmjestajVM></ViewModels:SmjestajVM>
</ContentPage.BindingContext>
<ContentPage.Content>
<StackLayout>
<Label Text="Welcome to Xamarin.Forms!"
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand" />
</StackLayout>
</ContentPage.Content>
</ContentPage>
The issue is with this line:
<ContentPage.BindingContext>
<ViewModels:SmjestajVM></ViewModels:SmjestajVM>
</ContentPage.BindingContext>
How do I write this so it works properly? I tried SmjestajVM(Id) and SmjestajVM("Id") but neither worked.