1

我正在尝试更多地了解反应式扩展,但我发现很难找到一个真实世界的例子,所以我可以训练自己。

几天前,我发现自己正在编写一些ToggleButtonMouse Enter、Leave Checked Unchecked 事件,现在我想知道是否可以使用响应式扩展来简化它。

这是目标:

给定 a ToggleButton,当悬停在它上面并且它没有被选中时,应该显示一个弹出窗口,如果鼠标不在按钮或弹出窗口上,则弹出窗口应该关闭

如果我按下切换按钮(选中),弹出窗口应保持打开状态,直到取消选中按钮(忽略鼠标输入离开事件),之后鼠标悬停行为应再次启动。

如果弹出窗口在外部关闭,则应自动取消选中切换按钮。(我知道这可以使用一些绑定和数据触发器来实现,但我想练习我的反应式扩展逻辑)

现在我有以下内容:

    private void ToggleButton_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
    {
        if (!ToggleButton.IsChecked ?? false)
            Popup.IsOpen = true;
    }

    private void ToggleButton_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
    {
        if (!Popup.Child.IsMouseOver && !(TaskManagerTab.IsChecked ?? false))
        {
            Popup.IsOpen = false;
            return;
        }

        popup.Child.MouseLeave +=  Popup_MouseLeave;
    }

    void Popup_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
    {
        Popup.Child.MouseLeave -=  Popup_MouseLeave;

        if (!ToggleButton.IsMouseOver && !(ToggleButton.IsChecked ?? false))
        {
            Popup.IsOpen = false;
            return;
        }
    }

    private void ToggleButton_CheckedChanged(object sender, System.Windows.RoutedEventArgs e)
    {
        Popup.IsOpen = ToggleButton.IsChecked ?? false;

        if (Popup.IsOpen)
            Popup.Closed += Popup_Closed;
    }

    void Popup_Closed(object sender, System.EventArgs e)
    {
        Popup.Closed -= Popup_Closed;

        ToggleButton.IsChecked = false;
    }
}

我想要一个粗略的版本,但我真的不知道如何开始。

谢谢!

更新:

我确实想出了这个,但我不确定性能,如果未选中切换按钮,我似乎无法重复。

var mouseEnterSaveBtn = Observable.FromEventPattern<MouseEventHandler, MouseEventArgs>(h => SaveBtn.MouseEnter += h,
                                                                       h => SaveBtn.MouseEnter -= h);

        var mouseLeaveList = Observable.FromEventPattern<MouseEventHandler, MouseEventArgs>(h => popup.MouseLeave += h,
                                                                       h => popup.MouseLeave -= h);

        var toggleBtnChecked =
            Observable.FromEventPattern<RoutedEventHandler, RoutedEventArgs>(h => SaveBtn.Checked += h,
                                                                            h => SaveBtn.Checked -= h);

        var allCloseEvents = mouseLeaveList.Merge(mouseLeaveList);

        mouseEnterSaveBtn.TakeUntil(toggleBtnChecked).Subscribe(pattern =>
                                                                    {
                                                                        popup.Visibility = Visibility.Visible;

                                                                        allCloseEvents.TakeUntil(toggleBtnChecked).
                                                                            Subscribe(eventPattern =>
                                                                                          {
                                                                                              if (!popup.IsMouseOver && !popup.IsMouseOver)
                                                                                                  popup.Visibility = Visibility.Collapsed;
                                                                                          });

                                                                    });
4

1 回答 1

5

这非常接近我会做的事情。不过,我建议的一件事是,每当您觉得应该订阅观察者中的可观察对象(注意您嵌套的订阅调用)时,您可能可以组合这些可观察对象然后订阅。

var mouseIn = Observable.FromEventPattern<MouseEventHandler, MouseEventArgs>
    (
        h => SaveBtn.MouseEnter += h,
        h => SaveBtn.MouseEnter -= h
    );

var mouseOut = Observable.FromEventPattern<MouseEventHandler, MouseEventArgs>
    (
        h => popup.MouseLeave += h,
        h => popup.MouseLeave -= h
    );

var isMouseOver = mouseIn
    .Select((o,e) => true)
    .Merge(mouseOut
        .Select((o,e) => false));

var toggleBtnChecked = Observable.FromEventPattern<RoutedEventHandler, RoutedEventArgs>
    (
        h => SaveBtn.Checked += h,
        h => SaveBtn.Checked -= h
    )
    .Select((o,e) => (o as ToggleButton).Checked);

var shouldShow = isMouseOver
    .DistinctUntilChanged()
    .CombineLatest(toggleBtnChecked, (mouse, pressed) => pressed || mouse);

shouldShow.Subscribe
(
    shouldBeVisible =>
    {
        popup.Visibility = shouldBeVisible ? Visibility.Visible : Visibility.Collapsed;
    }
);
于 2012-07-13T18:04:51.943 回答