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

问题描述:

我正在尝试在条目中设置背景图像。Xamarin表单:具有背景图像的条目

我知道我能做到这一点使用原生Android (android:background="@drawable/myImage" or mTextView.setBackgroundResource(R.drawable.myImage);).

可以做到这一点与Xamarin形式? 我在官方文档中找不到任何东西。

感谢

本机控制,我不相信有一个背景图像选项但是,可以延长在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(); 
    } 

} 

}