maui

How to use MultiBinding in net maui?


To use compiled binding I have enabled this from .csproject file:<MauiEnableXamlCBindingWithSourceCompilation>true</MauiEnableXamlCBindingWithSourceCompilation>

<controls:Thumbnail
  IsVisible="{Binding ImageFilePath, Converter={StaticResource ImageAttachmentVisibilityConverter}, x:DataType=models:AttachmentViewModel}"
  Grid.Row="1"
  Grid.RowSpan="2"
  Margin="0,10"
  ItemTappedCommand="{Binding Source={x:Reference _attachmentCollectionControl},
                        Path=AttachmentViewCommand, x:DataType=local:AttachmentCollection}"
  ItemTappedCommandParameter="{Binding DeviceFilePath, x:DataType=models:AttachmentViewModel}">
  <controls:Thumbnail.Source>
      <MultiBinding x:DataType="models:AttachmentViewModel"
                    Converter="{StaticResource Key=ImageToThumbnailConverter}"
                    ConverterParameter="{x:Static enumFileType:FileTypeValue.Photo}">
          <Binding Path="."/>
          <Binding Source="100"/>
      </MultiBinding>
  </controls:Thumbnail.Source>
</controls:Thumbnail>
public class ImageToThumbnailConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        if (values is null || values.Length == 0)
        {
            return null;
        }

        FontImageSource emptyImageIcon = new()
        {
            FontFamily = Constants.MaterialDesignIcons,
            Glyph = MaterialFontHelper.Image,
            Color = Colors.Gray,
            Size = MaterialFontHelper.DefaultFontIconSize
        };

        if (values.Length > 1)
        {
            emptyImageIcon.Size = double.TryParse(values[1].ToString(), out double imageSize) ? imageSize : MaterialFontHelper.DefaultFontIconSize;
        }

        if (values[0] is not ISupportThumbnail thumbnailImage || string.IsNullOrEmpty(thumbnailImage.ImageFilePath))
        {
            return emptyImageIcon;
        }

        var storageService = Resolver.ServiceProvider.GetRequiredService<IFileStorageService>();

        // determine the full path to the image
        if (!storageService.ExistsInAppDirectory(thumbnailImage.ImageFilePath))
        {
            return emptyImageIcon;
        }

        // get the thumbnail of the local file
        var result = ImageSource.FromStream(() => AsyncHelper.RunSync(() => storageService.GetThumbnailStreamAsync(thumbnailImage.ImageFilePath)));
        return result;
    }

    /// <inheritdoc/>
    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Here, I have passed second parameter for MultiBinding like this

<Binding Source="100"/>

Still, in the converter getting second parameter as Null always. If I remove MauiEnableXamlCBindingWithSourceCompilation from .csproject file than it does work fine.

I have tried to binding value using xaml resource like this: <x:Int32 x:Key="ThumbnailSize">100</x:Int32>

Also tried using creating constant variable with default value I want, but that too not working if the MauiEnableXamlCBindingWithSourceCompilation is set to true.

Does anyone know how to handle this?


Solution

  • The problem is that you set x:DataType="models:AttachmentViewModel" in your MultiBinding which will be applied to BOTH inner Bindings. I can see that your second Binding is a number and not an AttachmentViewModel, so, you need to rectify it by telling the compiler what "100" is. From the looks of it, you're going to take in a string and convert it to a number in your converter, i.e.

    <Binding x:DataType="x:String" Source="100" />
    

    If you want it to be a double to begin with, you can declare it as follows, and that will eliminate the need to parse the string in the converter:

    <Binding x:DataType="x:Double" Source="{x:Double '100'}"/>