C# 检测控制';财产变动

C# 检测控制';财产变动,c#,xaml,xamarin,xamarin.forms,C#,Xaml,Xamarin,Xamarin.forms,我目前正在基础项目的内容页上开发一个Xamarin Forms应用程序 在这个页面中,我有一个StackLayout,其中包含一些自定义控件(名为Accordion),这些控件具有绑定属性(boolean),允许我知道它们的内容是打开还是关闭 如何检测单击了哪个手风琴并关闭设置为打开的手风琴 我曾想过为我拥有的每一个手风琴添加一个TapHandler,但我想知道是否有更好的解决方案来实现它 (我对打开单击的控件不感兴趣,因为我的手风琴控件有自己的点击处理程序) 谢谢你的回答 编辑: Accord

我目前正在基础项目的内容页上开发一个Xamarin Forms应用程序

在这个页面中,我有一个
StackLayout
,其中包含一些自定义控件(名为
Accordion
),这些控件具有
绑定属性
boolean),允许我知道它们的内容是
打开还是
关闭

如何检测单击了哪个手风琴并关闭设置为打开的手风琴

我曾想过为我拥有的每一个手风琴添加一个TapHandler,但我想知道是否有更好的解决方案来实现它

(我对打开单击的控件不感兴趣,因为我的手风琴控件有自己的点击处理程序)

谢谢你的回答

编辑:

Accordion.xaml.cs:

public partial class Accordion : ContentView
{
    public Accordion()
    {
        InitializeComponent();
        IsOpen = false;
        Close();

        Indicator = Img;
    }

    //Elements

    //Some properties

    public static readonly BindableProperty IsOpenProperty = BindableProperty.Create(nameof(IsOpen), typeof(bool), typeof(Accordion), false, propertyChanged: IsOpenChanged);
    public bool IsOpen
    {
        get => (bool)GetValue(IsOpenProperty);
        set => SetValue(IsOpenProperty, value);
    }

    public static readonly BindableProperty AccordionContentProperty = BindableProperty.Create(nameof(AccordionContent), typeof(View), typeof(Accordion), default(View));
    public View AccordionContent
    {
        get => (View)GetValue(AccordionContentProperty);
        set => SetValue(AccordionContentProperty, value);
    }

    //Open handling

    private void TapGestureRecognizer_Tapped(object sender, EventArgs e)
    {
        IsOpen = !IsOpen;
    }

    public static void IsOpenChanged(BindableObject bindable, object oldValue, object newValue)
    {
        if(bindable != null && newValue != null)
        {
            var Control = (Accordion)bindable;

            if(Control.IsOpen == false)
            {
                VisualStateManager.GoToState(Control, "Open");
                Control.Close();
            }
            if(Control.IsOpen == true)
            {
                VisualStateManager.GoToState(Control, "Closed");
                Control.Open();
            }
        }
    }

    public uint AnimationDuration { get; set; } = 250;
    public async void Open()
    {
        _content.IsVisible = true;
        await Task.WhenAll(
            _content.TranslateTo(0, 0, AnimationDuration),
            Img.TranslateTo(-20, 0, AnimationDuration),
            IndicatorOpen(),
            _indicator.RotateTo(0, AnimationDuration),
            _content.FadeTo(30, 50, Easing.SinIn)
            );
    }
    public async Task IndicatorOpen()
    {
        Img.Margin = new Thickness(0, 0, -10, 0);

        return;
    }

    public async void Close()
    {
        await Task.WhenAll(
            _content.TranslateTo(0, 0, AnimationDuration),
            Img.TranslateTo(10, 0, AnimationDuration),
            IndicatorClose(),
            _indicator.RotateTo(-180, AnimationDuration),
            _content.FadeTo(0, 50)
            );
        _content.IsVisible = false;
    }
    public async Task IndicatorClose()
    {
        Img.Margin = new Thickness(0, 0, 0, 0);

        return;
    }
}

TapGestureRecognizer\u Tapped
是单击手风琴头部时TapHandler调用的方法。

为什么不向手风琴控件添加一个自定义事件,该事件在其状态更改时触发?@Jason,正如我所说,我已经有了一个方法/事件,可以在单击时打开单个手风琴。我想要的是关闭所有其他的手风琴,然后让你的页面订阅该事件,当它触发时,它应该关闭其他控件。好的,也许我不明白。。。我将发布手风琴类我建议控件在打开/关闭状态更改时触发自定义事件。页面订阅该事件,然后调用其他accordions的Close()方法。