Xamarin 图像未从给定的本地计算机路径加载
Xamarin Image not loading from a given local machine path
我正在存储图片并将其路径绑定到名为 ImageUrl 的 属性。当设置 url 时,表单中的图像不会加载。可能是什么问题呢 ?这是一些代码
public class ImageRecord{
public string ImagePath { get; set; }
public Image Image { get; set; }
public void SetImage( string path) {
ImagePath = path;
var bitmap = ImageSource.FromFile(ImagePath );
Image = new Image() {
Source = bitmap,
Margin = new Thickness(10)
};
}
这是XAML
中的设计师
<Image Grid.Column="0" WidthRequest="80" HeightRequest="80" x:DataType="DataObjects:ImageRecord">
<Image.Source>
<FileImageSource File="{Binding ImagePath}" />
</Image.Source>
</Image>
</Grid>
我也尝试过直接从图像标签设置 Source="the path of the image"
但没有成功。
图片下载为byte[],然后存储在设备上(Android).
如果你从Xamarin.Forms中的byte[] array
加载图片,你可以试试下面的代码:
c#代码:
byte[] bitmapData = ...;
ImageSource imageSource= ImageSource.FromStream(() => new MemoryStream(bitmapData));
PlayerImage.Source = imageSource;//binding in code
xaml代码:
<Image x:Name="PlayerImage" WidthRequest="25" HeightRequest="25"/>
或绑定在xaml
<image Source="{Binding imageSource}"/>
此外,您还可以使用Xamarin Community Toolkit ByteArrayToImageSourceConverter来实现这一点,这是一个转换器,允许用户将传入的值从字节数组和returns类型的对象转换为ImageSource。然后可以将该对象用作图像控件的源。
请参考以下代码:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:xct="http://xamarin.com/schemas/2020/toolkit"
x:Class="MyLittleApp.MainPage">
<ContentPage.Resources>
<ResourceDictionary>
<xct:ByteArrayToImageSourceConverter x:Key="ByteArrayToImageSourceConverter" />
</ResourceDictionary>
</ContentPage.Resources>
<StackLayout>
<Image Source="{Binding MyByteArray, Converter={StaticResource ByteArrayToImageSourceConverter}}" />
</StackLayout>
</ContentPage>
您还可以查看以下主题:
我正在存储图片并将其路径绑定到名为 ImageUrl 的 属性。当设置 url 时,表单中的图像不会加载。可能是什么问题呢 ?这是一些代码
public class ImageRecord{
public string ImagePath { get; set; }
public Image Image { get; set; }
public void SetImage( string path) {
ImagePath = path;
var bitmap = ImageSource.FromFile(ImagePath );
Image = new Image() {
Source = bitmap,
Margin = new Thickness(10)
};
}
这是XAML
中的设计师 <Image Grid.Column="0" WidthRequest="80" HeightRequest="80" x:DataType="DataObjects:ImageRecord">
<Image.Source>
<FileImageSource File="{Binding ImagePath}" />
</Image.Source>
</Image>
</Grid>
我也尝试过直接从图像标签设置 Source="the path of the image"
但没有成功。
图片下载为byte[],然后存储在设备上(Android).
如果你从Xamarin.Forms中的byte[] array
加载图片,你可以试试下面的代码:
c#代码:
byte[] bitmapData = ...;
ImageSource imageSource= ImageSource.FromStream(() => new MemoryStream(bitmapData));
PlayerImage.Source = imageSource;//binding in code
xaml代码:
<Image x:Name="PlayerImage" WidthRequest="25" HeightRequest="25"/>
或绑定在xaml
<image Source="{Binding imageSource}"/>
此外,您还可以使用Xamarin Community Toolkit ByteArrayToImageSourceConverter来实现这一点,这是一个转换器,允许用户将传入的值从字节数组和returns类型的对象转换为ImageSource。然后可以将该对象用作图像控件的源。
请参考以下代码:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:xct="http://xamarin.com/schemas/2020/toolkit"
x:Class="MyLittleApp.MainPage">
<ContentPage.Resources>
<ResourceDictionary>
<xct:ByteArrayToImageSourceConverter x:Key="ByteArrayToImageSourceConverter" />
</ResourceDictionary>
</ContentPage.Resources>
<StackLayout>
<Image Source="{Binding MyByteArray, Converter={StaticResource ByteArrayToImageSourceConverter}}" />
</StackLayout>
</ContentPage>
您还可以查看以下主题: