C# 如何缩放图像以适应标题视图

C# 如何缩放图像以适应标题视图,c#,xamarin.forms,navigationbar,C#,Xamarin.forms,Navigationbar,我使用标题视图布局在导航栏中使用图像。它是有效的,但是 我遇到了根据导航栏的高度设置图像大小的问题 如果我使用:HeightRequest=“{OnPlatform iOS=60,Android=35,Default=40}”,它就会工作 但这不起作用:BindingContext=“{x:Reference TitleLabel}”HeightRequest=“{Binding Height}” 为什么?!?这仍然是将图像高度链接到导航栏高度的最佳方式(以确保图像正确匹配),不是吗 我还尝

我使用标题视图布局在导航栏中使用图像。它是有效的,但是

我遇到了根据导航栏的高度设置图像大小的问题


如果我使用:HeightRequest=“{OnPlatform iOS=60,Android=35,Default=40}”,它就会工作

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

为什么?!?这仍然是将图像高度链接到导航栏高度的最佳方式(以确保图像正确匹配),不是吗


我还尝试将“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;
    }
}
<Image Source="flechegauche.png" HeightRequest="{Binding Source={x:Reference TitleLabel}, Path=Height, Converter={Helpers:dblMultiplier Default=10}}"/>
我在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;
    }
}
class dblMultiplier:IValueConverter,IMarkupExtension
{
公共双乘数{get;set;}=1.0f;
公共双精度默认值{get;set;}=double.NaN;
公共对象转换(对象值、类型targetType、对象参数、CultureInfo区域性)
{
双D值=System.Convert.ToDouble(值);
if(d值<0.0f&!double.IsNaN(默认))
返回默认值;
返回dvalue*乘数;
}
公共对象转换回(对象值、类型targetType、对象参数、CultureInfo区域性)
{
抛出新的NotSupportedException(“此转换器仅支持单向绑定”);
}
公共对象ProviderValue(IServiceProvider服务提供者)
{
归还这个;
}
}
我对XAML做了如下更改:

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;
    }
}
<Image Source="flechegauche.png" HeightRequest="{Binding Source={x:Reference TitleLabel}, Path=Height, Converter={Helpers:dblMultiplier Default=10}}"/>


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

即使使用像HeightRequest=“{binding Source={x:Reference TitleLabel},Path=Height}”这样的绑定,它也不会工作在
图像上不
VerticalOptions=“FillAndExpand”
工作吗?