compile binding help

Eduardo Gomez Romero 1,235 Reputation points
2025-01-15T13:54:16.1466667+00:00

So I am trying to follow the docs

<HorizontalStackLayout BindingContext="{x:Reference slider}" x:DataType="Slider">
    <Label Text="{Binding Value}" />
    <Label Text="{Binding Text, Source={x:Reference entry}, x:DataType=Entry}" />
</HorizontalStackLayout>

I have this page

xmlns:vm="clr-namespace:METROWIND.ViewModel"
x:Name="stationsMapPage"
x:DataType="vm:ChargingStationsMapPageViewModel">

<Grid x:DataType="vm:ChargingStationsMapPageViewModel">
    <Border
        x:Name="MobileContent"
        Style="{x:StaticResource ChargeStationBorderStyle}">
        <expander:SfExpander
            AnimationDuration="200"
            IsExpanded="{x:Binding IsExpanded}">
            <expander:SfExpander.Header>
                <Grid RowDefinitions="48">
                    <Label Style="{x:StaticResource ChooseStationLabelStyle}" />
                </Grid>
            </expander:SfExpander.Header>
            <expander:SfExpander.Content>
                <Grid
                    Padding="10,0,0,0"
                    RowDefinitions="Auto">
                    <CollectionView ItemsSource="{x:Binding TurbinePins}">
                        <CollectionView.ItemTemplate>
                            <DataTemplate x:DataType="model:TurbinePin">
                                <Border Stroke="Transparent">
                                    <HorizontalStackLayout Spacing="10">
                                        <Label Style="{x:StaticResource ElectricBoltLabelStyle}" />
                                        <Label
                                            Style="{x:StaticResource AddressLabelStyle}"
                                            Text="{x:Binding Turbine.Name}" />
                                    </HorizontalStackLayout>
                                    <Border.Behaviors>
                                        <mct:TouchBehavior
                                            DefaultBackgroundColor="White"
                                            PressedBackgroundColor="CornflowerBlue" />
                                    </Border.Behaviors>
                                    <Border.GestureRecognizers>
                                        <TapGestureRecognizer
                                            Command="{x:Binding Source={x:Reference stationsMapPage},
                                                                Path=BindingContext.ItemSelectedCommand}"
                                            CommandParameter="{x:Binding Turbine}" />
                                    </Border.GestureRecognizers>

                                </Border>
                            </DataTemplate>
                        </CollectionView.ItemTemplate>
                    </CollectionView>
                </Grid>
            </expander:SfExpander.Content>
        </expander:SfExpander>
    </Border>

    <maps:Map
        x:Name="ChargingStationMap"
        ItemsSource="{x:Binding TurbinePins}"
        VerticalOptions="FillAndExpand">
        <maps:Map.ItemTemplate>
            <DataTemplate x:DataType="model:TurbinePin">
                <controls:CustomMapPin
                    Address="{x:Binding Turbine.Address}"
                    Images="{x:Binding Turbine.ImagesURLs}"
                    Label="{x:Binding Turbine.Name}"
                    Location="{x:Binding Turbine.Location}"
                    MarkerClickedCommand="{x:Binding PinClickedCommand}"
                    MarkerClickedCommandParameter="{x:Binding}" />
            </DataTemplate>
        </maps:Map.ItemTemplate>
    </maps:Map>

    <Button
        x:Name="mapLayers"
        Command="{x:Binding OpenMapLayerOptionsCommand}"
        Style="{x:StaticResource MapButtonStyle}">
        <Button.ImageSource>
            <FontImageSource
                FontFamily="ma"
                Glyph="{x:Static constant:MaterialFonts.Layers}" />
        </Button.ImageSource>
    </Button>

    <popup:SfPopup
        WidthRequest="{Binding Source={x:Reference mapLayers}, Path=Width}"
        x:Name="MapChangeTypuPopUp"
        RelativeView="{x:Reference mapLayers}"
        Style="{x:StaticResource ChargeStationPopupStyle}">
        <popup:SfPopup.PopupStyle>
            <popup:PopupStyle CornerRadius="8" />
        </popup:SfPopup.PopupStyle>
        <popup:SfPopup.ContentTemplate>
            <DataTemplate>
                <VerticalStackLayout Spacing="10">
                    <VerticalStackLayout BindableLayout.ItemsSource="{x:Binding MapTypeButtons}">
                        <BindableLayout.ItemTemplate>
                            <DataTemplate x:DataType="model:MapTypeButton">
                                <controls:CustomImageButton
                                    BorderColor="{Binding SelectedColor}"
                                    Caption="{Binding Caption}"
                                    ClickCommand="{x:Binding ChangeMapTypeCommand,
                                                             Source={x:RelativeSource AncestorType={x:Type vm:ChargingStationsMapPageViewModel}}}"
                                    ImageName="{Binding ImageName}"
                                    MapNumber="{x:Binding MapNumber}"
                                    Parameter="{Binding}" />
                            </DataTemplate>
                        </BindableLayout.ItemTemplate>
                    </VerticalStackLayout>
                </VerticalStackLayout>
            </DataTemplate>
        </popup:SfPopup.ContentTemplate>
    </popup:SfPopup>
</Grid>

and the vm

 public partial class ChargingStationsMapPageViewModel: AppShellViewModel
 {
     public SfPopup? MapDialogPopUp;

     public Map? MapView;

     [ObservableProperty]
     bool isExpanded;

     public ChargingStationsMapPageViewModel(
         ITurbineService turbineService, IAppService
         appService, IServiceProvider serviceProvider,
         NoInternetPopUp noInternetPopUp,
         ICommandHandler commandHandler, IConnectivity connectivity)
         : base(turbineService, appService, serviceProvider, commandHandler, connectivity, noInternetPopUp)
     {
         MapDialogButtons();

     }


     public ObservableCollection<MapTypeButton> MapTypeButtons { get; set; } = [];

     private void MapDialogButtons()
     {
         MapTypeButtons.Add(new MapTypeButton
         {
             Caption = AppResource.Default,
             ImageName = MaterialFonts.Map,
             Selected = true,
             MapNumber = 1
         });
         MapTypeButtons.Add(new MapTypeButton
         {
             Caption = AppResource.Satelite,
             MapNumber = 2,
             ImageName = MaterialFonts.Satellite,
         });
     }

     [RelayCommand]
     void ItemSelected(Turbine Turbine)
     {
         if (Turbine == null || MapView == null)
         {
             return;
         }
         var mapSpan = MapSpan.FromCenterAndRadius(Turbine.Location,
             Distance.FromKilometers(2));

         MapView!.MoveToRegion(mapSpan);
     }

     [RelayCommand]

     void OpenMapLayerOptions()
     {
         MapDialogPopUp!.IsOpen = true;
     }

     [RelayCommand]
     void ChangeMapType(MapTypeButton mapTypeCustomButton)
     {
         if (mapTypeCustomButton == null)
         {
             return;
         }

         foreach (var mapTypeButton in MapTypeButtons)
         {
             mapTypeButton.Selected = false;
         }

         mapTypeCustomButton.Selected = true;

         switch (mapTypeCustomButton.MapNumber)
         {
             case 1:
                 MapView!.MapType = MapType.Street;
                 break;
             case 2:
                 MapView!.MapType = MapType.Satellite;
                 break;
         }

         MapDialogPopUp!.IsOpen = false;
     }
 }

and because of this change that Microsoft made, nothing works, if I click on the pin it doesn't do anything

also, in the turbinecollectiopage

x:Name="CollectionPage"
x:DataType="vm:TurbinesCollectionPageViewModel"
HideSoftInputOnTapped="True">

<VerticalStackLayout Spacing="-20">
    <inputLayout:SfTextInputLayout
        x:Name="MobileContent"
        Style="{x:StaticResource TurbineCollectionComboBoxStyle}">
        <editors:SfComboBox
            x:Name="combobox"
            DisplayMemberPath="Turbine.Name"
            ItemsSource="{x:Binding TurbinePins}"
            Style="{x:DynamicResource ComboBoxStyle}">
            <editors:SfComboBox.Behaviors>
                <mct:EventToCommandBehavior
                    Command="{x:Binding SelectedItemChangeCommand}"
                    EventName="SelectionChanged" />
            </editors:SfComboBox.Behaviors>
        </editors:SfComboBox>
    </inputLayout:SfTextInputLayout>

    <CollectionView
        x:Name="TurbineCollection"
        Grid.Row="1"
        ItemsSource="{x:Binding TurbinePins}"
        SelectionMode="None"
        Style="{x:StaticResource TurbineCollectionViewStyle}">
        <CollectionView.ItemTemplate>
            <DataTemplate x:DataType="model:TurbinePin">
                <Border BackgroundColor="#578D57">
                    <Border.StrokeShape>
                        <RoundRectangle CornerRadius="8" />
                    </Border.StrokeShape>
                    <Grid
                        ColumnDefinitions="*,Auto"
                        RowDefinitions="Auto,Auto,Auto">
                        <Label
                            Style="{x:StaticResource TurbineDataLabelStyleStyle}"
                            Text="{x:Binding Turbine.Name}" />
                        <Label
                            Grid.Row="1"
                            Style="{x:StaticResource TurbineDataLabelStyleStyle}"
                            Text="{x:Binding Turbine.Address}" />
                        <Label
                            Grid.Row="2"
                            Style="{x:StaticResource C02LabelStyle}"
                            Text="{x:Binding Turbine.FinalCo2Removed,
                                             StringFormat='{0:F5} kg CO₂'}" />

                        <Button
                            Grid.RowSpan="3"
                            Grid.Column="1"
                            BackgroundColor="#63B62B"
                            Command="{x:Binding GotoLocationCommand,
                                                Source={x:RelativeSource AncestorType={x:Type vm:TurbinesCollectionPageViewModel}}}"
                            CommandParameter="{x:Binding}"
                            FontFamily="ma"
                            FontSize="Large"
                            Text="{x:Static fonts:MaterialFonts.Directions}" />
                    </Grid>

                    <Border.GestureRecognizers>
                        <TapGestureRecognizer
                            Command="{x:Binding PinClickedCommand,
                                                Source={x:RelativeSource AncestorType={x:Type vm:TurbinesCollectionPageViewModel}}}"
                            CommandParameter="{x:Binding}" />
                    </Border.GestureRecognizers>
                </Border>
            </DataTemplate>
        </CollectionView.ItemTemplate>
    </CollectionView>

</VerticalStackLayout>

the pinclickcommend doesn't work

and my news page

  xmlns:rex="clr-namespace:METROWIND.Resources"
  xmlns:vm="clr-namespace:METROWIND.ViewModel"
  x:DataType="vm:NewsPageViewModel">

  <Grid
      Padding="5,0,5,0"
      RowDefinitions="Auto,*">

      <CollectionView
          x:Name="NewsList"
          Grid.Row="1"
          ItemsSource="{x:Binding ArticleList}"
          SelectionMode="None">

          <CollectionView.ItemsLayout>
              <LinearItemsLayout
                  ItemSpacing="10"
                  Orientation="Vertical" />
          </CollectionView.ItemsLayout>

          <CollectionView.EmptyViewTemplate>
              <DataTemplate>
                  <VerticalStackLayout
                      HorizontalOptions="CenterAndExpand"
                      VerticalOptions="CenterAndExpand">
                      <ActivityIndicator IsRunning="True" />
                      <Label
                          HorizontalOptions="Center"
                          Text="{x:Static rex:AppResource.Loading}" />
                  </VerticalStackLayout>
              </DataTemplate>
          </CollectionView.EmptyViewTemplate>

          <CollectionView.ItemTemplate>
              <DataTemplate x:DataType="models:Article">
                  <Border
                      Padding="10"
                      StrokeShape="RoundRectangle 20"
                      StrokeThickness="1">
                      <Grid
                          Padding="10"
                          ColumnDefinitions="Auto,*"
                          RowDefinitions="250,Auto,Auto,Auto,Auto"
                          RowSpacing="10">
                          <ffimageloading:CachedImage
                              Grid.ColumnSpan="2"
                              Aspect="AspectFill"
                              BitmapOptimizations="True"
                              CacheDuration="30"
                              ErrorPlaceholder="no_image.png"
                              LoadingPlaceholder="loading.gif"
                              Source="{x:Binding UrlToImage,
                                                 TargetNullValue=no_image.png}" />
                          <!--  Title  -->
                          <Label
                              Grid.Row="1"
                              Grid.ColumnSpan="2"
                              FontAttributes="Bold"
                              FontSize="Large"
                              Text="{x:Binding Title}" />
                          <!--  Source and Icon  -->
                          <HorizontalStackLayout
                              Grid.Row="2"
                              Grid.ColumnSpan="2"
                              Spacing="10">
                              <Label
                                  FontFamily="ma"
                                  FontSize="Large"
                                  Text="{x:Static constants:MaterialFonts.Newspaper}"
                                  VerticalTextAlignment="Center" />
                              <Label
                                  Text="{x:Binding Source.Name}"
                                  VerticalTextAlignment="Center" />
                          </HorizontalStackLayout>
                          <!--  Author and Date  -->
                          <Grid
                              Grid.Row="3"
                              Grid.ColumnSpan="2"
                              ColumnDefinitions="Auto,*,Auto"
                              ColumnSpacing="10">
                              <Label
                                  FontFamily="ma"
                                  FontSize="Large"
                                  Text="{x:Static constants:MaterialFonts.Person}" />
                              <Label
                                  Grid.Column="1"
                                  Text="{x:Binding Author,
                                                   TargetNullValue='Author unknown'}"
                                  VerticalTextAlignment="Center" />
                              <Label
                                  Grid.Column="2"
                                  Text="{x:Binding PublishedAt}" />
                          </Grid>
                      </Grid>
                      <Border.GestureRecognizers>
                          <TapGestureRecognizer
                              Command="{x:Binding ShowNewsDetailCommand,
                                                  Source={x:RelativeSource AncestorType={x:Type vm:NewsPageViewModel}}}"
                              CommandParameter="{x:Binding .}" />
                      </Border.GestureRecognizers>
                  </Border>
              </DataTemplate>
          </CollectionView.ItemTemplate>
      </CollectionView>


vm

public ObservableCollection<Article> ArticleList { get; set; } = [];

readonly IHttpService _httpService;
readonly IDeviceLanguageService _deviceLanguageService;
readonly IAppService _appService;

public NewsPageViewModel(IDeviceLanguageService deviceLanguageService,
    IHttpService httpService,
    IAppService appService)
{
    _deviceLanguageService = deviceLanguageService;
    _httpService = httpService;
    _appService = appService;
    LoadNews();

}

public async void LoadNews()
{
    var language = _deviceLanguageService.GetDeviceCultureInfo();
    var newsUrl = AppConstants.GetNewsUrl(language.TwoLetterISOLanguageName);

    var newsObj = await _httpService.GetAsync<News>(newsUrl);

    if (newsObj != null && newsObj.Articles != null)
    {
        foreach (var article in newsObj.Articles)
        {
            ArticleList.Add(article);
        }
    }
}

[RelayCommand]
async Task ShowNewsDetail(Article article)
{
    if (article != null)
    {
        await _appService.NavigateToPage($"{nameof(ArticleDetailsPage)}",
             new Dictionary<string, object> {{ "articleObj", article }
        });
    }
}

it docent work when I click the news

I know I am probably asking too much, but the Docs are not very clear to me

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,877 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 79,156 Reputation points Microsoft Vendor
    2025-01-20T01:01:34.25+00:00

    Hello,

    because of this change that Microsoft made, nothing works, if I click on the pin it doesn't do anything

    this issue is related to this line <DataTemplate x:DataType="model:TurbinePin"> in TurbinesCollectionPage.xaml. If you set x:DataType=viewmodels:OverdueInquiringViewModel in the Command, the click command will be working.

     Command="{Binding Source={RelativeSource AncestorType={x:Type viewmodels:OverdueInquiringViewModel}}, Path=EditPersonCommand, x:DataType=viewmodels:OverdueInquiringViewModel}"
    
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.