Xamarin表格:带背景图片的条目

时间:2015-04-06 13:20:45

标签: mobile xamarin xamarin.forms

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

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

来做到这一点

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

谢谢

1 个答案:

答案 0 :(得分:0)

我不相信具有背景图像选项的本机控件,但是,您可以扩展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();
    }

}

}