【问题标题】:Xamarin Forms: Entry with background imageXamarin Forms:带有背景图像的条目
【发布时间】:2015-04-06 13:20:45
【问题描述】:

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

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

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

谢谢

【问题讨论】:

    标签: mobile xamarin xamarin.forms


    【解决方案1】:

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

    }

    【讨论】:

      猜你喜欢
      • 2017-12-14
      • 1970-01-01
      • 1970-01-01
      • 2017-02-28
      • 2018-01-28
      • 1970-01-01
      • 2016-08-15
      • 2019-01-20
      • 2016-06-09
      相关资源
      最近更新 更多