Используя библиотеку FFImageLoading
, ваш подход правильный с Success
, но если у вас есть изображения в папке Resource, возможно, вы можете использовать подход / идею ниже:
PLC / стандарт:
using Xamarin.Forms;
namespace YourProject.Utils
{
public interface IImageResource
{
Size GetSize(string fileName);
}
}
Android:
using Android.Graphics;
using YourProject.Droid.Utils;
using YourProject.Utils;
using System;
using Xamarin.Forms;
[assembly: Dependency(typeof(ImageResource))]
namespace YourProject.Droid.Utils
{
public class ImageResource : Java.Lang.Object, IImageResource
{
public Size GetSize(string fileName)
{
var options = new BitmapFactory.Options
{
InJustDecodeBounds = true
};
fileName = fileName.Replace('-', '_').Replace(".png", "").Replace(".jpg", "");
var resId = Forms.Context.Resources.GetIdentifier(fileName, "drawable", Forms.Context.PackageName);
BitmapFactory.DecodeResource(Forms.Context.Resources, resId, options);
return new Size((double)options.OutWidth, (double)options.OutHeight);
}
}
}
IOS:
using YourProject.iOS.Utils;
using YourProject.Utils;
using System;
using UIKit;
using Xamarin.Forms;
[assembly: Dependency(typeof(ImageResource))]
namespace YourProject.iOS.Utils
{
public class ImageResource : IImageResource
{
public Size GetSize(string fileName)
{
UIImage image = UIImage.FromFile(fileName);
return new Size((double)image.Size.Width, (double)image.Size.Height);
}
}
}
Использование с Xamarin.Forms.DependencyService:
var imageSize = DependencyService.Get<IImageResource>().GetSize("ic_launcher.png");
System.Diagnostics.Debug.WriteLine(imageSize);
Другая опция через XAML:
<ContentView.Resources>
<ResourceDictionary>
<Style x:Key="CachedImageStyle" TargetType="{x:Type ffimageloading:CachedImage}">
<Setter Property="HorizontalOptions" Value="Center" />
<Setter Property="VerticalOptions" Value="Center" />
<Setter Property="HeightRequest">
<Setter.Value>
<OnIdiom x:TypeArguments="x:Double" Tablet="800" Phone="400" />
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
</ContentView.Resources>
<ContentView.Content>
<!-- ... -->
<ffimageloading:CachedImage
x:Name="cachedImg"
Style="{StaticResource CachedImageStyle}">
</ffimageloading:CachedImage>
<!-- ... -->
</ContentView.Content>
И затем, вы можете создать конвертер для установки размера планшета / телефона, возможно, работает.
Надеюсь, это поможет вам.