如何使用约定使用Caliburn.Micro操作实现延迟

时间:2016-02-21 11:48:49

标签: c# wpf async-await caliburn.micro

我有时需要延迟绑定执行(想象一下调用服务器的搜索框,只有当用户暂停一秒而不是每次击键时,你都希望它执行。)

延迟WPF绑定没问题 - 您只需使用绑定指定延迟: <TextBlock Text="{Binding Name, Delay=500}"/>

每当我需要在使用Caliburn.Micro Message.Attach的情况下延迟执行时,我通常会以这种方式实现它(消息附加到TextChanged事件DoSomething ):

private int doingSomething;

public async void DoSomething()
{
    int current = ++doingSomething;
    await Task.Delay(500);
    if (current != doingSomething) //method was reentered
        return;

    await DoWorkCallServerEtc();
}

这很好用,但它不能很好地扩展并打破 DRY 原则(我需要在需要延迟的地方再次写这个)。

我的问题是,我可以用Caliburn.Micro以某种方式为此编写约定吗? 或者可能是一种不同的,更具可扩展性的方法?

1 个答案:

答案 0 :(得分:4)

您可以阅读documentation page

  

动作功能可以利用System.Windows.Interactivity   触发机制。

这意味着

<TextBox Name="textBox" Margin="5"
    cal:Message.Attach="[Event TextChanged] = [Action DoAction(textBox.Text)]" />

相当于:

<TextBox Margin="5" Name="textBox">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="TextChanged">
            <cal:ActionMessage MethodName="DoAction">
                <cal:Parameter Value="{Binding ElementName=textBox, Path=Text}" />
            </cal:ActionMessage>
        </i:EventTrigger>
    </i:Interaction.Triggers>
</TextBox>

负责执行DoAction方法的对象是EventTriggerEventTrigger立即触发,没有延迟。所以我们需要创建自己的DelayedEventTrigger;类似的东西:

public class DelayedEventTrigger : System.Windows.Interactivity.EventTrigger
{
    private EventArgs args;
    private DispatcherTimer dispatcherTimer;

    public static readonly DependencyProperty DelayProperty =
        DependencyProperty.Register("Delay", typeof(int), typeof(DelayedEventTrigger), new PropertyMetadata(1000));

    public int Delay
    {
        get { return (int)base.GetValue(DelayProperty); }
        set { base.SetValue(DelayProperty, value); }
    }

    protected override void OnEvent(EventArgs eventArgs)
    {
        if (dispatcherTimer != null)
        {
            dispatcherTimer.Stop();
        }
        args = eventArgs;
        dispatcherTimer = new DispatcherTimer();
        dispatcherTimer.Interval = TimeSpan.FromMilliseconds(Delay);
        dispatcherTimer.Tick += new EventHandler(OnDispatcherTimerTick);
        dispatcherTimer.Start();
    }

    protected override void OnDetaching()
    {
        if (dispatcherTimer != null)
        {
            dispatcherTimer.Stop();
            dispatcherTimer = null;
        }
        base.OnDetaching();
    }

    private void OnDispatcherTimerTick(object sender, EventArgs e)
    {
        dispatcherTimer.Stop();
        InvokeActions(args);
    }
}

默认延迟为1秒(1000毫秒)。所以现在我们可以在XAML中使用它了:

<TextBox Margin="5" Name="textBox">
    <i:Interaction.Triggers>
        <local:DelayedEventTrigger Delay="800" EventName="TextChanged">
            <cal:ActionMessage MethodName="DoDelayAction">
                <cal:Parameter Value="{Binding ElementName=textBox, Path=Text}" />
            </cal:ActionMessage>
        </local:DelayedEventTrigger>
    </i:Interaction.Triggers>
</TextBox>

在我看来,没有必要使用约定(您可以明确使用DelayedEventTrigger),但如果您需要,可以在Bootstrapper类中配置Caliburn Parser:

protected override void Configure()
{
    base.Configure();

    Parser.CreateTrigger = delegate(DependencyObject target, string triggerText)
    {
        System.Windows.Interactivity.EventTrigger eventTrigger;
        if (triggerText == null)
        {
            ElementConvention elementConvention = ConventionManager.GetElementConvention(target.GetType());
            return elementConvention.CreateTrigger();
        }
        string eventName = triggerText.Replace("[", String.Empty).Replace("]", String.Empty);
        if (eventName.StartsWith("Delayed", StringComparison.OrdinalIgnoreCase))
        {
            eventName = eventName.Replace("DelayedEvent", String.Empty).Trim();
            eventTrigger = new DelayedEventTrigger();
        }
        else
        {
            eventName = eventName.Replace("Event", String.Empty).Trim();
            eventTrigger = new System.Windows.Interactivity.EventTrigger();
        }

        eventTrigger.EventName = eventName;
        return eventTrigger;
    };     
}

通过添加此代码,您可以使用此约定:

<TextBox Name="textBox" Margin="5"
    cal:Message.Attach="[DelayedEvent TextChanged] = [Action DoDelayAction(textBox.Text)]" />

我希望它可以帮到你。