Xamarin Forms:带有背景图片的条目

Xamarin Forms: Entry with background image

我正在尝试在条目中设置背景图片。

我知道我可以使用原生 Android (android:background="@drawable/myImage" or mTextView.setBackgroundResource(R.drawable.myImage);).

可以用 Xamarin Forms 做到这一点吗? 我在官方文档中找不到任何内容。

谢谢

我认为本机控件没有背景图像选项,但是,您可以扩展 Xamarin.Forms 命名空间中的任何控件并根据需要对其进行修改。如果你想做的改变超出了你可以通过继承完成的范围,也许你应该 Google 自定义渲染器。

下面是扩展 Grid 以创建具有附加绑定属性的自定义开关的示例。

using System;
using Xamarin.Forms;

namespace willfunkyouup.CustomControl
{
class CustomSwitch : Grid
{

    public event EventHandler<SelectedItemChangedEventArgs> ItemSelected;
    private Button negative;
    private Button positive;

    public static readonly BindableProperty SelectedItemProperty = BindableProperty.Create<CustomSwitch, Object>(t => t.SelectedItem, null, BindingMode.TwoWay, propertyChanged: OnSelectedItemChanged);

    public CustomSwitch()
    {

        try
        {
            this.HorizontalOptions = LayoutOptions.Center;
            this.VerticalOptions = LayoutOptions.Center;

            negative = new Button();
            negative.Text = "No";
            negative.Style = RecoveryConnect_XamForms.AppStyling.Style_Button_Switch;
            negative.Clicked += (o,s) => OnSelectedItemChanged(this, ItemSelected, (int)Classes.Collections.Enums.SelectionStatus.False);

            positive = new Button();
            positive.Text = "Yes";
            positive.Style = RecoveryConnect_XamForms.AppStyling.Style_Button_Switch;
            positive.Clicked += (o, s) => OnSelectedItemChanged(this, ItemSelected, (int)Classes.Collections.Enums.SelectionStatus.True);               

            this.Children.Add(negative, 0,0);
            this.Children.Add(positive, 1,0);
        }
        catch(System.Exception ex)
        {
            willfunkyouup.Classes.Helpers.Helper_ErrorHandling.SendErrorToServer(ex, this.GetType().Name, System.Reflection.MethodBase.GetCurrentMethod().Name);
        }

    }

    public Object SelectedItem
    {
        get
        {
            return base.GetValue(SelectedItemProperty);
        }
        set
        {
            if (SelectedItem != value)
            {
                base.SetValue(SelectedItemProperty, value);
                InternalUpdateSelected();
            }
        }
    }

    private void InternalUpdateSelected()
    {
        if((int)SelectedItem == (int)Classes.Collections.Enums.SelectionStatus.False)
        {
            negative.BorderColor = willfunkyouup.AppStyling.Color_Selected;
            positive.BorderColor = willfunkyouup.AppStyling.Color_UnSelected;
            positive.Opacity = willfunkyouup.AppStyling.Opaque_High;
        }
        else if ((int)SelectedItem == (int)Classes.Collections.Enums.SelectionStatus.True)
        {
            negative.BorderColor = willfunkyouup.AppStyling.Color_UnSelected;
            negative.Opacity = willfunkyouup.AppStyling.Opaque_High;
            positive.BorderColor = willfunkyouup.AppStyling.Color_Selected;
        }
        else
        {
            negative.BorderColor = willfunkyouup.AppStyling.Color_UnSelected;
            negative.Opacity = willfunkyouup.AppStyling.Opaque_High;
            positive.BorderColor = willfunkyouup.AppStyling.Color_UnSelected;
            positive.Opacity = willfunkyouup.AppStyling.Opaque_High;
        }
    }

    private static void OnSelectedItemChanged(BindableObject bindable, object oldValue, object newValue)
    {
        CustomSwitch boundSwitch = (CustomSwitch)bindable;

        if((int)newValue != (int)Classes.Collections.Enums.SelectionStatus.Unselected)
        {
            boundSwitch.SelectedItem = (int)newValue == (int)Classes.Collections.Enums.SelectionStatus.False ? (int)Classes.Collections.Enums.SelectionStatus.False : (int)Classes.Collections.Enums.SelectionStatus.True;
        }


        if (boundSwitch.ItemSelected != null)
        {
            boundSwitch.ItemSelected(boundSwitch, new SelectedItemChangedEventArgs(newValue));
        }
        boundSwitch.InternalUpdateSelected();
    }

}

}