首页 > 解决方案 > 如何从 Xamarin Forms 中的自定义控件创建图像?

问题描述

我的应用程序中有一个自定义控件,效果很好。现在我想从那个视图中制作一个图像。

我的自定义控件:

<?xml version="1.0" encoding="UTF-8"?>
<Grid xmlns="http://xamarin.com/schemas/2014/forms"
     xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
     xmlns:controls="clr-namespace:Project.Controls"
     xmlns:ffimageloading="clr-namespace:FFImageLoading.Forms;assembly=FFImageLoading.Forms"
     xmlns:fftransformations="clr-namespace:FFImageLoading.Transformations;assembly=FFImageLoading.Transformations"
     xmlns:yummy="clr-namespace:Xamarin.Forms.PancakeView;assembly=Xamarin.Forms.PancakeView"
     x:Name="ParentView"
     x:Class="Project.Controls.CustomControl">


     <Label x:Name = "myLabelInMyXaml" BackgroundColor = "{Binding CustomColorViewModel}"/>

它背后的代码:

public partial class CustomControl : Grid
{
   public static readonly BindableProperty ItemProperty = BindableProperty.Create(nameof(Item), typeof(Item), typeof(CustomGrid), propertyChanged: ItemChanged);
public static readonly BindableProperty CustomColorProperty = BindableProperty.Create(nameof(IsAnonymousSubColor), typeof(Color), typeof(CustomGrid), propertyChanged: CustomColorChanged);

public Color CustomColor
{
    get { return (Color)GetValue(CustomColorProperty); }
    set { SetValue(CustomColorProperty, value); }
}

static void CustomColorChanged (object bindable, object oldValue, object newValue)
{
    var x = bindable as CustomGrid;

    if (x.Item != null)
    {
         x.myLabelInMyXaml.BackgroundColor = x.CustomColor;
    }
}

static void ItemChanged (object bindable, object oldValue, object newValue)
{
    var x = bindable as CustomGrid;
}

public Item Item
{
    get { return (Item) GetValue(ItemProperty); }
    set { SetValue(ItemProperty, value); }
}

public CustomGrid()
{
    InitializeComponent();
}

}

所以这段代码运行良好,并且在应用程序中显示良好。现在我准备了一个按钮来通过 Xamarin.Essentials 插件共享它。https://docs.microsoft.com/en-us/xamarin/essentials/share?tabs=android

await Share.RequestAsync(new ShareFileRequest
{
     Title = Title,
     File = new ShareFile("" /* this is where the image will go*/)
});

现在我的问题是,我怎样才能把这个自定义控件变成一个图像源?我将其转换为 HTTP 地址之后的部分已经是我准备好的。所以澄清一下;问题是关于如何将自定义控件转换为图像源/字节 []。

标签: c#xamarinxamarin.formsxamarin.androidxamarin.ios

解决方案


推荐阅读