How to use MultiBinding in net maui? - Stack Overflow

To use compiled binding I have enabled this from .csproject file:<MauiEnableXamlCBindingWithSourceCo

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?

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?

Share asked Mar 4 at 12:11 DivyeshDivyesh 2,41727 silver badges47 bronze badges 3
  • I can't understand your problem clearly. All of the MultiBinding's sub binding should be the same type. But in your code, they are different types. And I can't get the meaning of <Binding Source="100"/>. – Liyun Zhang - MSFT Commented Mar 5 at 4:09
  • The second binding (Source="100") is intended to pass a constant value (100) as the default thumbnail size to the converter. This approach works fine when MauiEnableXamlCBindingWithSourceCompilation is disabled, but with it enabled, the second value is always null. I've also tried using StaticResource and a constant property in the ViewModel, but they don't seem to work either. Do you know if there's a limitation in MAUI's compiled bindings with using Source for constant values in MultiBinding? – Divyesh Commented Mar 5 at 5:09
  • If I not make this MauiEnableXamlCBindingWithSourceCompilation enable than it gives XC0025 warnings. Here is the open bug link for the same github/dotnet/maui/issues/27631 – Divyesh Commented Mar 5 at 5:21
Add a comment  | 

1 Answer 1

Reset to default 0

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'}"/>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745043934a4607978.html

相关推荐

  • How to use MultiBinding in net maui? - Stack Overflow

    To use compiled binding I have enabled this from .csproject file:<MauiEnableXamlCBindingWithSourceCo

    7小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信