.netmvvmmauiprism

.NET Compiled Bindings and EventToCommand Behaviour


I have a .Net 9 MAUI project with a simple View\ViewModel setup and using Compiled Bindings...

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="MAUI.Pages.Views.PageView" 

...

         xmlns:viewModels="clr-namespace:MAUI.Pages.ViewModels"           
         x:DataType="viewModels:MainPageViewModel"
       
         >

In the view I have an entry control used for filtering a list that I want to trigger the DelegateCommand in the ViewModel when text is entered...

   <Entry x:Name="filterText"
      Grid.Column="1"
      IsVisible="true"
      Placeholder="Search here"
      ClearButtonVisibility="WhileEditing"
      TextColor="White"
      PlaceholderColor="White"
      HorizontalOptions="FillAndExpand">
   <Entry.Behaviors>
       <prism:EventToCommandBehavior EventName="TextChanged"
                                     Command="{Binding FilterTextChangedCommand}"
                                     CommandParameter="{Binding Path=Text, Source={x:Reference filterText}, x:DataType={x:Null}}"
                                     />

   </Entry.Behaviors>

In the ViewModel I define the event,

  private async Task onFilterTextChangedCommand(object obj)
  {

      _logger.Debug("");
      if (obj != null)
      {
          searchText = obj as string;

          ApplyFilters(searchText);
      }
      else
      {
          await RefreshList();
      }

  }

If I don't specify x:DataType={x:Null} on the CommandParameter then VS shows the warning "Binding: Property "Text" not found on "MAUI.Pages.ViewModels.MainPageViewModel" which I understand given the compiled binding of the page is to the MainPageViewModel and the Text path is not a property of it.

However when I do specify x:DataType={x:Null} I get the following warning, "Binding could be compiled to improve runtime performance if x:DataType is not explicitly null...."

My question is how do I specify an x:DataType for the entry control's Text property in the CommandParameter to stop VS from warning about the compiled binding (and "improving performance") whilst still keeping the Command binding itself to the DelegateCommand the ViewModel?

I am using PrismLibrary for the EventToCommandBehavior but the MAUI Community Toolkit has the same thing and throws the same warning.

The page works with the x:DataType={x:Null} defined for the CommandParamter but I would like to know what the "correct" way is in this situation.


Solution

  • As previously mentioned in the comments, the correct DataType is x:DataType="Entry".