SKSharp 未加载位图 Xamarin 表单 [英] SKSharp not loading Bitmap Xamarin Forms

查看:24
本文介绍了SKSharp 未加载位图 Xamarin 表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试加载png"到使用 Android 和 UWP 项目的 XamarinForms 应用中的 ListView.

I'm trying to load "png's" into a ListView in a XamarinForms app using Android and UWP projects.

这是我的 ListView 的 xaml.

Here is my xaml of the ListView.

   <ListView x:Name="TemplateListView"
        ItemsSource="{Binding TemplateData}">
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <StackLayout Orientation="Vertical">
                                    <Image Margin="20,0,0,0" Source="{Binding ImageData}" Aspect="AspectFit" HeightRequest="120"></Image>
                                    <Label Text="{Binding Title}"  FontSize="16" />
                                </StackLayout>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>

我的对象类是一个非常简单的类.

My object class is a very simple one.

public class TemplateData
{
    public string Title { get; set; }
    public SKBitmap ImageData { get; set; }
}

这里是加载位图并作为 ListViews ItemSource 分配给集合的 SKSharp 代码.

And here is the SKSharp code to load the bitmaps and assign to the collection as the ListViews ItemSource.

//load the image and assisgn it as an Item
     this.TemplateListView.ItemsSource = new TemplateData[]
 {
               new TemplateData{Title="A", ImageData=LoadImage("A.jpg")},
               new TemplateData{Title="B", ImageData=LoadImage("B.jpg")},
          
 };

//对于此代码图片需要是项目资源

// for this code image needs to be a project resource

    private SKBitmap LoadImage(string filename)
    {
      //  string resourceID = filename;
        string resourceID = "CWON_App.Images."+filename;
        Assembly assembly = GetType().GetTypeInfo().Assembly;

        using (Stream stream = assembly.GetManifestResourceStream(resourceID))
        {
            resourceBitmap = SKBitmap.Decode(stream);
        }

        return resourceBitmap;

    }

我的图片在 UWP 项目的 Images 文件夹中,并标记为 Embedded Resource.

My images are in the Images folder of the UWP project and marked as Embedded Resource.

应用运行时没有错误,但 ListView 中没有显示任何内容.

The app runs with no errors but nothing is displayed in the ListView.

推荐答案

Xamarin 图片控件无法加载 SKBitmap,建议使用 ImageSource 替换 SKBitmap.

Xamarin image control can't load SKBitmap, for this scenario we suggest you use ImageSource to replace SKBitmap.

如果你已经把图片放在uwp项目文件夹中,你可以使用ImageSource.FromFile方法来加载图片,请注意图片构建动作需要是Content.

If you have place the image in the uwp project folder, you could use ImageSource.FromFile method to load image, and please note the image build action need to be Content.

private ImageSource LoadImage(string filename)
{
    var source = Device.RuntimePlatform == Device.UWP ? ImageSource.FromFile($"Assets/{filename}") : ImageSource.FromFile("waterfront.jpg");
    return source;
}

有关更多信息,请参阅 Xamarin 处理图像.

For more please refer Xamarin Working with image.

这篇关于SKSharp 未加载位图 Xamarin 表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆