c# – 如何使用约定使用Caliburn.Micro操作实现延迟
作者:互联网
我有时需要延迟绑定执行(想想一个调用服务器的搜索框,你只希望它只在用户暂停一秒而不是每次击键时执行).
延迟WPF绑定没有问题 – 您只需使用绑定指定延迟:
< TextBlock Text =“{Binding Name,Delay = 500}”/>.
每当我需要在我使用Caliburn.Micro的Message.Attach的情况下延迟执行时,我通常以这种方式实现它(消息通过DoSomething操作附加到TextChanged事件):
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为此编写一个约定吗?
或者可能采用另一种更具可扩展性的方法
解决方法:
正如你在documentation page中所读到的那样
Actions feature leverages System.Windows.Interactivity for it’s
trigger mechanism.
这意味着
<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方法的对象是EventTrigger.AventTrigger立即触发,没有延迟.所以我们需要创建自己的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)]" />
我希望它可以帮到你.
标签:c,wpf,async-await,caliburn-micro 来源: https://codeday.me/bug/20190623/1270155.html