Image 是否可以从xamarin表单中的文件和uri设置图像源?

Image 是否可以从xamarin表单中的文件和uri设置图像源?,image,xaml,xamarin,caching,Image,Xaml,Xamarin,Caching,我正在尝试构建xamarin图像上传应用程序。 我想在上传到服务器之前显示本地图像, 之后,我想显示来自服务器url的图像。 如果我使用图像源,它在本地和服务器上都能正常工作。 但是服务器映像缓存出现问题 <Image Grid.Row="0" Source="{Binding Url}" Aspect="AspectFill" HeightRequest="60" Backgr

我正在尝试构建xamarin图像上传应用程序。 我想在上传到服务器之前显示本地图像, 之后,我想显示来自服务器url的图像。 如果我使用图像源,它在本地和服务器上都能正常工作。 但是服务器映像缓存出现问题

<Image Grid.Row="0"
    Source="{Binding Url}"
    Aspect="AspectFill"
    HeightRequest="60"
    BackgroundColor="black"
    WidthRequest="60"/>

所以,为了禁用缓存,我使用UriImageSource,它在服务器url上工作,但不显示本地映像

<Image Grid.Row="0"
    Aspect="AspectFill"
    HeightRequest="60"
    BackgroundColor="black"
    WidthRequest="60">
    <Image.Source>
        <UriImageSource Uri="{Binding Url}"
                        CachingEnabled="False"/>
        <!--FileImageSource File="{Binding Url}"/-->
    </Image.Source>
</Image>

是否可以在此图像中同时使用文件和uri图像源?
感谢您的帮助。

选项1:-使用绑定Url

创建转换器(图像转换器)。以下ImageConverter具有用于测试的虚拟代码。它使用静态字段(请不要使用静态字段)。您可以绑定Url属性,并根据Url查找图像源是远程Url还是本地图像。并返回URIMAGESOURCE或直接字符串(本地图像)。 在ViewModel中,Uri属性的初始值将是本地映像,一旦收到远程映像,将Uri设置为远程映像并引发属性更改事件

//ImageConverter.cs
 public class ImageConverter : IValueConverter
    {
        public static int count = 1;
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            count++;
            var imageUri = System.Convert.ToString(value);
            if (imageUri.Contains("http") && (count % 2 == 0))
            {
                UriImageSource uriImageSource = new UriImageSource();
                uriImageSource.Uri = new Uri(imageUri);
                uriImageSource.CachingEnabled = false;
                return uriImageSource;
            }
            else
            {
                return imageUri;//Local image
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

//Xaml header

xmlns:local="clr-namespace:Monkeys.Converter"

<ContentPage.Resources>
     <ResourceDictionary>
        <local:ImageConverter x:Key="imgConv" />
     </ResourceDictionary>
</ContentPage.Resources>

//Xaml content
<Image Grid.Row="0"
         Source="{Binding  Url, Converter={StaticResource imgConv}}"
         Aspect="AspectFill" HeightRequest="60"
         BackgroundColor="black" WidthRequest="60"
         HorizontalOptions="CenterAndExpand">
</Image>

我用它来测试

您可以在代码中轻松更改图像源代码。我不知道是否有可能使用BDIGIN我使用GaldCopyVIEW视图,所以需要使用绑定来考虑使用转换器吗?如何使用它?请参阅。您可以传递参数以在本地映像和服务器映像之间切换。
 public class ImageConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var imageUri = System.Convert.ToString(value);
            var isRemoteUri = (bool)parameter;
            if (isRemoteUri)
            {
                var uriImageSource = new UriImageSource();
                uriImageSource.Uri = new Uri(imageUri);
                uriImageSource.CachingEnabled = false;
                return uriImageSource;
            }
            else
            {
                return imageUri;//Local image
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }