繁体   English   中英

如何使 xamarin 自定义控件中的 2 个可绑定属性相互识别?

[英]How to make 2 bindable properties in a xamarin custom control aware of each other?

我在 xamarin 中创建了一个自定义控件并连接了一些可绑定的属性。 但是,我希望他们彼此了解。 例如,我有一个想要影响字符串属性的类型属性。 以下是目前 2 个可绑定的属性:

public string ApprovalType { get; set; }
        public static readonly BindableProperty ApprovalTypeProperty = BindableProperty.Create(
           propertyName: "ApprovalType",
           returnType: typeof(string),
           declaringType: typeof(CustomApprovalView),
           defaultValue: "",
           defaultBindingMode: BindingMode.TwoWay,
           propertyChanged: ApprovalTypePropertyChanged);

            private static void ApprovalTypePropertyChanged(BindableObject bindable, object oldvalue, object newValue)
            {
                //Do Nothing
            }

public string MessageString { get; set; }
        public static readonly BindableProperty MessageStringProperty = BindableProperty.Create(
           propertyName: "MessageString",
           returnType: typeof(string),
           declaringType: typeof(CustomApprovalView),
           defaultValue: "",
           defaultBindingMode: BindingMode.TwoWay,
           propertyChanged: MessageStringPropertyChanged);
        private static void MessageStringPropertyChanged(BindableObject bindable, object oldvalue, object newValue)
        {
            var control = (CustomApprovalView)bindable;
            //This isn't possible as this method is static
            //control.Message.Text = this.ApprovalType
        }

我试过在 Get, set 中这样做,但是当在那里放置断点时,它们不会被调用并且标签为空:

 public string RequestedDateString
        {
            get { return (string)GetValue(RequestedDateStringProperty); }
            set
            {
                SetValue(RequestedDateStringProperty, value);
                string type = (string)GetValue(ApprovalTypeProperty);
                if (type.ToLower() == "open")
                {
                    this.RequestedDate.Text = $"Open on ... {value}";
                }
                else
                {
                    this.RequestedDate.Text = $"Closed on ... {value}";
                }

            }
        }
        public static readonly BindableProperty RequestedDateStringProperty = BindableProperty.Create(
           propertyName: "RequestedDateString",
           returnType: typeof(string),
           declaringType: typeof(CustomApprovalView),
           defaultValue: "",
           defaultBindingMode: BindingMode.TwoWay);

以下是完整代码 - Xaml:

<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             xmlns:pancake="clr-namespace:Xamarin.Forms.PancakeView;assembly=Xamarin.Forms.PancakeView"
             xmlns:animations="clr-namespace:XamarinUITests.Behaviours"
             x:Class="XamarinUITests.CustomApprovalView">
    <pancake:PancakeView BackgroundColor="white">
        <pancake:PancakeView.Resources>
            <ResourceDictionary>
                <Color x:Key="lightPink">#D96CB3</Color>
                <Color x:Key="darkPink">#8C4F7C</Color>
                <Color x:Key="offWhite">#F2F2F2</Color>
                <Color x:Key="darkGrey">#454859</Color>
                <Color x:Key="lightGrey">#737B8C</Color>
                <Color x:Key="orange">#F2B05E</Color>
                <Color x:Key="green">#42BD94</Color>
                <Color x:Key="red">#F86B7B</Color>
            </ResourceDictionary>
        </pancake:PancakeView.Resources>
        <StackLayout Orientation="Horizontal">
            <BoxView x:Name="ApprovalColourBox" WidthRequest="5"/>
            <StackLayout Orientation="Vertical" Padding="5" HorizontalOptions="FillAndExpand" Spacing="5">
                <Label x:Name="RequestedDate" TextColor="{StaticResource lightGrey}" FontSize="Small"/>
                <Label x:Name="RequestTitle" TextColor="{StaticResource darkGrey}" FontSize="Medium"/>
                <Label x:Name="DatesRequested" TextColor="{StaticResource lightGrey}" FontSize="Small"/>
                <pancake:PancakeView BackgroundColor="{StaticResource offWhite}" HorizontalOptions="FillAndExpand" Margin="10,5,20,10" Padding="10,5,10,5" CornerRadius="5">
                    <Label x:Name="Message" TextColor="{StaticResource darkGrey}" FontSize="Small" LineBreakMode="WordWrap"/>
                </pancake:PancakeView>
            </StackLayout>
        </StackLayout>
    </pancake:PancakeView>
</ContentView>

C#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using Xamarin.Forms.PancakeView;

namespace XamarinUITests
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class CustomApprovalView : ContentView
    {
        public CustomApprovalView()
        {
            InitializeComponent();
        }

        #region ApprovalColour (Bindable Color)
        public Color ApprovalColour { get; set; }
        public static readonly BindableProperty ApprovalColourProperty = BindableProperty.Create(
           propertyName: "ApprovalColour",
           returnType: typeof(Color),
           declaringType: typeof(CustomApprovalView),
           defaultValue: Color.Transparent,
           defaultBindingMode: BindingMode.TwoWay,
           propertyChanged: ApprovalColourPropertyChanged);
        private static void ApprovalColourPropertyChanged(BindableObject bindable, object oldvalue, object newValue)
        {
            var control = (CustomApprovalView)bindable;
            control.ApprovalColourBox.BackgroundColor = (Color)newValue;
        }
        #endregion ApprovalColour (Bindable Color)

        #region RequestTitleString (Bindable string)
        public string RequestTitleString { get; set; }
        public static readonly BindableProperty RequestTitleStringProperty = BindableProperty.Create(
           propertyName: "RequestTitleString ",
           returnType: typeof(string),
           declaringType: typeof(CustomApprovalView),
           defaultValue: "",
           defaultBindingMode: BindingMode.TwoWay,
           propertyChanged: RequestTitleStringPropertyChanged);
        private static void RequestTitleStringPropertyChanged(BindableObject bindable, object oldvalue, object newValue)
        {
            var control = (CustomApprovalView)bindable;
            control.RequestTitle.Text = newValue.ToString();
        }
        #endregion RequestTitleString (Bindable string)

        #region DatesRequestedString (Bindable string)
        public string DatesRequestedString { get; set; }
        public static readonly BindableProperty DatesRequestedStringProperty = BindableProperty.Create(
           propertyName: "DatesRequestedString",
           returnType: typeof(string),
           declaringType: typeof(CustomApprovalView),
           defaultValue: "",
           defaultBindingMode: BindingMode.TwoWay,
           propertyChanged: DatesRequestedStringPropertyChanged);
        private static void DatesRequestedStringPropertyChanged(BindableObject bindable, object oldvalue, object newValue)
        {
            var control = (CustomApprovalView)bindable;
            control.DatesRequested.Text = newValue.ToString();
        }
        #endregion DatesRequestedString (Bindable string)

        #region MessageString (Bindable string)
        public string MessageString { get; set; }
        public static readonly BindableProperty MessageStringProperty = BindableProperty.Create(
           propertyName: "MessageString",
           returnType: typeof(string),
           declaringType: typeof(CustomApprovalView),
           defaultValue: "",
           defaultBindingMode: BindingMode.TwoWay,
           propertyChanged: MessageStringPropertyChanged);
        private static void MessageStringPropertyChanged(BindableObject bindable, object oldvalue, object newValue)
        {
            var control = (CustomApprovalView)bindable;
            control.Message.Text = newValue.ToString();
        }
        #endregion MessageString (Bindable string)

        #region RequestedDateString (Bindable string)
        public string RequestedDateString
        {
            get { return (string)GetValue(RequestedDateStringProperty); }
            set
            {
                SetValue(RequestedDateStringProperty, value);
                string type = (string)GetValue(ApprovalTypeProperty);
                if (type.ToLower() == "open")
                {
                    this.RequestedDate.Text = $"Open on ... {value}";
                }
                else
                {
                    this.RequestedDate.Text = $"Closed on ... {value}";
                }

            }
        }
        public static readonly BindableProperty RequestedDateStringProperty = BindableProperty.Create(
           propertyName: "RequestedDateString",
           returnType: typeof(string),
           declaringType: typeof(CustomApprovalView),
           defaultValue: "",
           defaultBindingMode: BindingMode.TwoWay);
           //propertyChanged: RequestedDateStringPropertyChanged);
        //private static void RequestedDateStringPropertyChanged(BindableObject bindable, object oldvalue, object newValue)
        //{
        //    var control = (CustomApprovalView)bindable;
        //    control.RequestedDate.Text =  $"{newValue.ToString()}";
        //}
        #endregion RequestedDateString (Bindable string)

        #region ApprovalType (Bindable string)
        public string ApprovalType { get; set; }
        public static readonly BindableProperty ApprovalTypeProperty = BindableProperty.Create(
           propertyName: "ApprovalType",
           returnType: typeof(string),
           declaringType: typeof(CustomApprovalView),
           defaultValue: "",
           defaultBindingMode: BindingMode.TwoWay,
           propertyChanged: ApprovalTypePropertyChanged);

            private static void ApprovalTypePropertyChanged(BindableObject bindable, object oldvalue, object newValue)
            {
            //Do Nothing
            }

        #endregion ApprovalType (Bindable string)

    }
}

内容页.xaml:

<Label Text="Pending Approvals" HorizontalOptions="Start" TextColor="{StaticResource darkGrey}" Margin="10,10,0,5"/>
                            <ListView x:Name="pendingApprovalList" ItemsSource="{Binding PastApprovals}" HasUnevenRows="True" SeparatorVisibility="None" BackgroundColor="{StaticResource offWhite}">
                                <ListView.ItemTemplate>
                                    <DataTemplate>
                                        <ViewCell>
                                            <local:CustomApprovalView Margin="0,0,0,10"
                                                                      ApprovalType="Open"
                                                                      MessageString="{Binding Message}" 
                                                                      ApprovalColour="{Binding Colour}" 
                                                                      RequestedDateString="{Binding RequestedDate}" 
                                                                      RequestTitleString="{Binding RequestTitle}" 
                                                                      DatesRequestedString="{Binding DatesRequested}"/>
                                        </ViewCell>
                                    </DataTemplate>
                                </ListView.ItemTemplate>

您可以在MessageString的 set 方法中处理逻辑

public string MessageString 
{
  get { return (string)GetValue (MessageStringProperty ); }
  set { 
        // do something you want
        SetValue (MessageStringProperty, value); 
      }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM