Mobile Xamarin表单:带有背景图像的条目

Mobile Xamarin表单:带有背景图像的条目,mobile,xamarin,xamarin.forms,Mobile,Xamarin,Xamarin.forms,我正在尝试在条目中设置背景图像 我知道我可以使用本机Android(Android:background=“@drawable/myImage”或mTextView.setBackgroundResource(R.drawable.myImage);)实现这一点。 可以用Xamarin的形式来做吗? 我在官方文件中找不到任何东西 感谢我认为本机控件没有背景图像选项,但是,您可以扩展Xamarin.Forms命名空间中的任何控件,并根据需要对其进行修改。如果您想要进行的更改超出了您通过继承所能完成

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

我知道我可以使用本机Android
(Android:background=“@drawable/myImage”或mTextView.setBackgroundResource(R.drawable.myImage);)实现这一点。

可以用Xamarin的形式来做吗? 我在官方文件中找不到任何东西


感谢

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

下面是一个扩展网格的示例,以创建具有附加绑定属性的自定义开关

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();
    }

}
使用系统;
使用Xamarin.Forms;
命名空间willfunkyouup.CustomControl
{
类CustomSwitch:Grid
{
已选择公共事件事件处理程序项;
私人按钮负片;
私人按钮正极;
public static readonly BindableProperty SelectedItemProperty=BindableProperty.Create(t=>t.SelectedItem,null,BindingMode.TwoWay,propertyChanged:OnSelectedItemChanged);
公共自定义交换机()
{
尝试
{
this.horizontalpoptions=LayoutOptions.Center;
this.VerticalOptions=LayoutOptions.Center;
负=新按钮();
否定。Text=“否”;
negative.Style=RecoveryConnect_XamForms.appstyleing.Style_按钮_开关;
否定。单击+=(o,s)=>OnSelectedItemChanged(this,ItemSelected,(int)Classes.Collections.Enums.SelectionStatus.False);
正=新按钮();
肯定。Text=“是”;
positive.Style=RecoveryConnect_XamForms.appstyleing.Style_按钮_开关;
正。单击+=(o,s)=>OnSelectedItemChanged(this,ItemSelected,(int)Classes.Collections.Enums.SelectionStatus.True);
this.Children.Add(负数,0,0);
这个.Children.Add(正,1,0);
}
catch(System.Exception-ex)
{
willfunkyouup.Classes.Helpers.Helper\u ErrorHandling.SendErrorToServer(例如,this.GetType().Name,System.Reflection.MethodBase.GetCurrentMethod().Name);
}
}
公共对象SelectedItem
{
得到
{
返回base.GetValue(SelectedItemProperty);
}
设置
{
如果(选择编辑项!=值)
{
base.SetValue(SelectedItemProperty,value);
InternalUpdateSelected();
}
}
}
私有void InternalUpdateSelected()
{
if((int)SelectedItem==(int)Classes.Collections.Enums.SelectionStatus.False)
{
negative.BorderColor=willfunkyouup.appstyleing.Color\u选中;
positive.BorderColor=willfunkyouup.AppStyleing.Color\u未选中;
正.Opacity=willfunkyouup.AppStyleing.opace\u高;
}
如果((int)SelectedItem==(int)Classes.Collections.Enums.SelectionStatus.True),则为else
{
negative.BorderColor=willfunkyouup.appstyleing.Color\u未选中;
负。不透明度=willfunkyouup.AppStyleing.不透明\u高;
positive.BorderColor=willfunkyouup.AppStyleing.Color\u选中;
}
其他的
{
negative.BorderColor=willfunkyouup.appstyleing.Color\u未选中;
负。不透明度=willfunkyouup.AppStyleing.不透明\u高;
positive.BorderColor=willfunkyouup.AppStyleing.Color\u未选中;
正.Opacity=willfunkyouup.AppStyleing.opace\u高;
}
}
SelectedItemChanged上的私有静态无效(BindableObject bindable、object oldValue、object newValue)
{
CustomSwitch boundSwitch=(CustomSwitch)可绑定;
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;
}
如果(boundSwitch.ItemSelected!=null)
{
boundSwitch.ItemSelected(boundSwitch,newselecteditemchangedeventargs(newValue));
}
boundSwitch.InternalUpdateSelected();
}
}
}