如何缩放图像以适合 TitleView

How to scale image to fit TitleView

我使用 TitleView 布局在导航栏中使用图像。它有效但是...

我在根据导航栏的高度设置图片大小时遇到​​问题。

<NavigationPage.TitleView>
    <StackLayout Orientation="Horizontal"
                 VerticalOptions="Center"
                 Spacing="10">

        <Image Source="flechegauche.png" BindingContext="{x:Reference TitleLabel}" HeightRequest="{Binding Height}"/>

        <Label x:Name="TitleLabel" Text="Evènements" FontSize="16" TextColor="White" 
               VerticalTextAlignment="Center" VerticalOptions="FillAndExpand"
               HorizontalTextAlignment="Center" HorizontalOptions="FillAndExpand" />

        <Image Source="filtre.png" HeightRequest="{OnPlatform iOS=60, Android=35, Default=40}"/>
        <Image Source="flechedroite.png" HeightRequest="{OnPlatform iOS=60, Android=35, Default=40}"/>

    </StackLayout>
</NavigationPage.TitleView>

如果我使用:HeightRequest="{OnPlatform iOS=60, Android=35, Default=40}"

但这不起作用:BindingContext="{x:Reference TitleLabel}" HeightRequest="{Binding Height}"

为什么?!?它仍然是最好的方法 link 图像的高度到导航栏的高度(以确保图像正确适合),不是吗?

我也试过将 "Aspect" 设置为 "AspectFit" 但它没有任何改变。

我不知道为什么,但我发现了这个。 我创建了一个这样的转换器:

class dblMultiplier : IValueConverter, IMarkupExtension
{
    public double Multiplier { get; set; } = 1.0f;

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return System.Convert.ToDouble(value) * Multiplier;
    }
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException("Only one way bindings are supported with this converter");
    }

    public object ProvideValue(IServiceProvider serviceProvider)
    {
        return this;
    }
}

我在 XAML:

中使用了这个绑定
<Image Source="flechegauche.png" HeightRequest="{Binding Source={x:Reference TitleLabel}, Path=Height, Converter={Helpers:dblMultiplier Multiplier=0.99}}"/>

转换器被调用了两次。当标签的高度尚未固定时,默认值为 -1。第二个是在计算布局时。所以它完美地工作。

但是如果我使用相同的转换器与 1.0 而不是 0.99,它不起作用 !!!图片大小不适应...?!?是不是很奇怪?

好的,我找到了核心问题...

(1)我的图片在第一次出现时太大(在计算布局之前),所以没有显示缩放图像所引用的标签,因此标签不能用于缩放图像,并且图像未显示预期大小!

所以,我做了一个转换器,在开始时强制使用一个小的默认值:

class dblMultiplier : IValueConverter, IMarkupExtension
{
    public double Multiplier { get; set; } = 1.0f;
    public double Default { get; set; } = double.NaN;

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        double dvalue = System.Convert.ToDouble(value);

        if (dvalue < 0.0f && !double.IsNaN(Default) )
            return Default;

        return dvalue * Multiplier;
    }
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException("Only one way bindings are supported with this converter");
    }

    public object ProvideValue(IServiceProvider serviceProvider)
    {
        return this;
    }
}

我把 XAML 改成了这样:

<Image Source="flechegauche.png" HeightRequest="{Binding Source={x:Reference TitleLabel}, Path=Height, Converter={Helpers:dblMultiplier Default=10}}"/>

(2) 我还必须确保标签不适合 TitleView 的整个高度。否则不会发生缩放。