Вы можете создать пользовательский рендерер, например, следующий код.
[assembly: ExportRenderer(typeof(Xamarin.Forms.Button), typeof(CustomButton.Droid.CustomButton))]
namespace CustomButton.Droid
{
public class CustomButton:ButtonRenderer
{
Context mcontext;
public CustomButton(Context context) : base(context)
{
mcontext = context;
}
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Button> e)
{
base.OnElementChanged(e);
AssetManager assets = mcontext.Assets;
Stream input = assets.Open("main.png");
var mydraw=Drawable.CreateFromStream(input, null);
Control.Background = mydraw;
}
}
}
использовать его в формах xamarin.
<StackLayout>
<!-- Place new controls here -->
<Label Text="Welcome to Xamarin.Forms!"
HorizontalOptions="Center"
VerticalOptions="CenterAndExpand" />
<Button Text="Button"/>
</StackLayout>
Вот скриншот.
Обновление
Если бы я использовал Control.SetCompoundDrawablesWithIntrinsicBounds(null, mydraw, null, null);
, это могло бы показать изображение над текстом.
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Button> e)
{
base.OnElementChanged(e);
AssetManager assets = mcontext.Assets;
Stream input = assets.Open("person.jpg");
var mydraw = Drawable.CreateFromStream(input, null);
Control.SetCompoundDrawablesWithIntrinsicBounds(null, mydraw, null, null);
}
Здесь работает скриншот.