question

kobi-8673 avatar image
0 Votes"
kobi-8673 asked ·

Xamarin Forms - BindingContext from XAML not working

Hi,

I have app in Xamarin.Forms and i have TabbedPage:

 <?xml version="1.0" encoding="utf-8" ?>
 <TabbedPage  xmlns="http://xamarin.com/schemas/2014/forms"
              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
              x:Class="nMscoutApp.Views.ReportPage"
              xmlns:pages="clr-namespace:nMscoutApp.Views"
              xmlns:vm="clr-namespace:nMscoutApp.ViewModels">
    
     <TabbedPage.Children>
         <pages:ReportBasicPage Title="Start" BindingContext="{Binding ReportBasicPageVM}"/>
         <pages:ReportBasicPage Title="Wywiad"/><!-- To change-->
         <pages:ReportBasicPage Title="Finansowe"/><!-- To change-->
         <pages:ReportBasicPage Title="Oględziny"/><!-- To change-->
         <pages:ReportBasicPage Title="Zdjęcia"/><!-- To change-->
     </TabbedPage.Children>
 </TabbedPage>

In CodeBehind I set this VM:

 [XamlCompilation(XamlCompilationOptions.Compile)]
     [QueryProperty(nameof(VisitId), "visitId")]
     public partial class ReportPage : TabbedPage
     {
         private int id = 0;
    
         public ReportBasicPageViewModel ReportBasicPageVM { get; set; }
         public string VisitId
         {
             set
             {
                 id = Convert.ToInt32(value);
                 ReportBasicPageVM = new ReportBasicPageViewModel(id);
             }
         }
    
         public ReportPage()
         {
             InitializeComponent();
         }
     }

But when i Try Click in some button in ReportBasicPage it's not happening.

My ReportBasicPage XAML:

 <?xml version="1.0" encoding="utf-8" ?>
 <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
              x:Class="nMscoutApp.Views.ReportBasicPage"
              xmlns:response="clr-namespace:Models.Responses;assembly=SharedModels" 
              xmlns:vm="clr-namespace:nMscoutApp.ViewModels"
              Title="{Binding Title}">
    
     <ContentPage.BindingContext>
         <vm:ReportBasicPageViewModel />
     </ContentPage.BindingContext>
    
     <ContentPage.ToolbarItems>
         <ToolbarItem Text="Odśwież" Command="{Binding RefreshCommand}" />
     </ContentPage.ToolbarItems>
    
     <StackLayout>
         <Grid Padding="10">
                     <Grid.RowDefinitions>
                         <RowDefinition Height="Auto"></RowDefinition>
                         <RowDefinition Height="Auto"></RowDefinition>
                         <RowDefinition Height="Auto"></RowDefinition>
                         <RowDefinition Height="Auto"></RowDefinition>
                         <RowDefinition Height="1"></RowDefinition>
                         <RowDefinition Height="Auto"></RowDefinition>
                         <RowDefinition Height="Auto"></RowDefinition>
                         <RowDefinition Height="Auto"></RowDefinition>
                         <RowDefinition Height="Auto"></RowDefinition>
                         <RowDefinition Height="1"></RowDefinition>
                         <RowDefinition Height="Auto"></RowDefinition>
                         <RowDefinition Height="Auto"></RowDefinition>
                         <RowDefinition Height="Auto"></RowDefinition>
                         <RowDefinition Height="Auto"></RowDefinition>
                         <RowDefinition Height="Auto"></RowDefinition>
                     </Grid.RowDefinitions>
                     <Grid.ColumnDefinitions>
                         <ColumnDefinition Width="*"></ColumnDefinition>
                         <ColumnDefinition Width="120"></ColumnDefinition>
                     </Grid.ColumnDefinitions>
                     <Label Grid.Row="0" Grid.Column="0" FontSize="Medium">Dłużnik:</Label>
                     <Label Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" FontSize="Large" Text="{Binding VisitData.DebtorData.Display}"/>
                     <Label Grid.Row="2" Grid.Column="0" FontSize="Medium">Adres:</Label>
                     <Label Grid.Row="3" Grid.Column="0" FontSize="Large" Text="{Binding VisitData.AddressData.ForSearch}"/>
                     <Label Grid.Row="3" Grid.Column="1" FontSize="Large">Włącz nawigację</Label>
                     <BoxView Grid.Row="4" Grid.ColumnSpan="2" HeightRequest="1" Color="Black"/>
                     <Label Grid.Row="5" Grid.Column="0" FontSize="Medium">Pierwotny wierzyciel:</Label>
                     <Label Grid.Row="6" Grid.Column="0" Grid.ColumnSpan="2" FontSize="Large" Text="{Binding VisitData.CreditorData.OriginalCreditorName}" />
                     <Label Grid.Row="7" Grid.Column="0" FontSize="Medium">Wierzyciel:</Label>
                     <Label Grid.Row="8" Grid.Column="0" Grid.ColumnSpan="2" FontSize="Large" Text="{Binding VisitData.CreditorData.CreditorName}" />
                     <BoxView Grid.Row="9" Grid.ColumnSpan="2" HeightRequest="1" Color="Black"/>
                 </Grid>
                
                
             <Grid Padding="10">
                 <Grid.RowDefinitions>
                     <RowDefinition Height="Auto"></RowDefinition>
                     <RowDefinition Height="Auto"></RowDefinition>
                 </Grid.RowDefinitions>
                 <Grid.ColumnDefinitions>
                     <ColumnDefinition Width="*"></ColumnDefinition>
                 </Grid.ColumnDefinitions>
    
                 <Button Grid.Row="0"
                         Grid.Column="0"
                         Margin="0,10,0,0"
                         FontSize="Small"
                         Text="Zrób zdjęcia"
                         Command="{Binding MakePhoto}"
                         BackgroundColor="{StaticResource Primary}"
                         TextColor="White" />
    
                 <Button Grid.Row="0"
                         Grid.Column="1"
                         Margin="0,10,0,0"
                         Text="Rozpocznij wizytę"
                         Command="{Binding StartVisit}"
                         BackgroundColor="{StaticResource Primary}"
                         TextColor="White" IsEnabled="{Binding AllowStartVisit}" />
             </Grid>
     </StackLayout>
 </ContentPage>

And CodeBehind:

     [XamlCompilation(XamlCompilationOptions.Compile)]
     public partial class ReportBasicPage : ContentPage
     {
         public ReportBasicPage()
         {
             InitializeComponent();
         }
     }

And ViewModel Constructor:

 public ReportBasicPageViewModel(int id)
         {
             visitId = id;
    
             routeService = App.Container.Resolve<IRouteService>();
             userService = App.Container.Resolve<IUserService>();
             photoService = App.Container.Resolve<IPhotoService>();
             geoLocationService = App.Container.Resolve<IGeoLocation>();
    
             Title = "Wizyta";
    
             PhotoPathList = new ObservableCollection<string>();
    
             MakePhoto = new Command(OnMakePhotoClick);
             StartVisit = new Command(OnStartVisitClick);
             RefreshCommand = new Command(OnRefreshClick);
             LoadItemsCommand = new Command(OnRefreshClick);
             RemovePicture = new Command<string>((x) => OnRemovePictureClick(x));
         }

What is wrong? Please help.


dotnet-xamarinformsdotnet-xamarinforms-xaml
· 3
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I think this issue is related to <pages:ReportBasicPage Title="Start" BindingContext="{Binding ReportBasicPageVM}"/> this line, If you want to make a bindingContext, you added it by following code in ReportBasicPage XAML , that is correct.

<ContentPage.BindingContext>
         <vm:ReportBasicPageViewModel />
     </ContentPage.BindingContext>

0 Votes 0 ·

Hi, thanks for response.

But I Have many pages in ReportPage and i have different ViewModel for any page.

0 Votes 0 ·

Agree with @VasanthakumarM-3635, You can create different viewmodel in the Contentpage‘s background code

2 Votes 2 ·

1 Answer

VasanthakumarM-3635 avatar image
2 Votes"
VasanthakumarM-3635 answered ·

@kobi-8673 ,

You can create different viewmodel as per your requirement.


You the bindingcontext = Viewmodel ;

·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.