Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在WinForms中,时间像箭一样飞逝_C#_Winforms_System.reactive - Fatal编程技术网

C# 在WinForms中,时间像箭一样飞逝

C# 在WinForms中,时间像箭一样飞逝,c#,winforms,system.reactive,C#,Winforms,System.reactive,在上查看javascript演示的反应式扩展时,我想在C#/Winforms中尝试一下,但它似乎工作得不太好 我只是将其放入表单的构造函数中(安装并引用了Rx框架): Observable.Context=SynchronizationContext.Current; var mousemove=Observable.FromEvent(这个“mousemove”); var message=“时间如箭一般飞逝”。ToCharArray(); for(int i=0;i { l、 左=e.Eve

在上查看javascript演示的反应式扩展时,我想在C#/Winforms中尝试一下,但它似乎工作得不太好

我只是将其放入表单的构造函数中(安装并引用了Rx框架):

Observable.Context=SynchronizationContext.Current;
var mousemove=Observable.FromEvent(这个“mousemove”);
var message=“时间如箭一般飞逝”。ToCharArray();
for(int i=0;i
{
l、 左=e.EventArgs.X+闭包*15+10;
l、 Top=e.EventArgs.Y;
//Debug.WriteLine(l.Text);
});
添加(l);
}
当我移动鼠标时,字母似乎以随机顺序移动,如果我取消注释调试行,我会看到同一字母的多个事件

有什么想法吗?我试过油门,但似乎没什么区别。我是不是对WinForms的要求太高了,以至于无法移动所有这些标签

(交叉张贴在上)

回答交叉张贴的回复(如果作者在这里,并且想要索取,我可以):

问题1:您正在使用旧位。可观察。上下文在大约4个版本之前就消失了。 问题2:Javascript没有线程的概念,Rx喜欢为您在其他线程上添加内容

因此,使用最新的位,解决方案如下所示:

void Form1_Load(object sender, EventArgs e)
{
   Reactive("Time flies like an arrow");
}

private void Reactive(string msg)
{
    var mousemove = Observable.FromEvent<MouseEventArgs>(this, "MouseMove");
    var message = msg.ToCharArray();

    for(int i = 0; i < message.Length; i++)
    {
        var l = new Label()
        { 
            Text = message[i].ToString(), 
            AutoSize = true, 
            TextAlign = ContentAlignment.MiddleCenter 
        };
        int closure = i;
        mousemove
            .Delay(TimeSpan.FromSeconds(0.07 * i), Scheduler.Dispatcher)
            .Subscribe(e =>
            {
                l.Left = e.EventArgs.X + closure * 12 - 5;
                l.Top = e.EventArgs.Y + 15;
            });
        Controls.Add(l);
    }
}
void Form1\u加载(对象发送方,事件参数e)
{
反应(“时光如箭飞逝”);
}
私有无效(字符串消息)
{
var mousemove=Observable.FromEvent(这个“mousemove”);
var message=msg.ToCharArray();
for(int i=0;i
{
l、 左=e.EventArgs.X+闭包*12-5;
l、 Top=e.EventArgs.Y+15;
});
添加(l);
}
}
请注意ObserveOnDispatcher和SubscribeOnDispatcher。这使我们接近Javascript版本,但线程是这里真正的问题

更新,在上述代码中添加了杰夫·梵高的更正


只是为了好玩,这里是一个模糊的渲染相同的东西没有接收:

private void Unreactive(string msg)
{
    var message = msg.ToCharArray();

    for(int i = 0; i < message.Length; i++)
    {
        var l = new Label()
        { 
            Text = message[i].ToString(), 
            AutoSize = true, 
            TextAlign = ContentAlignment.MiddleCenter 
        };
        Controls.Add(l);
        int closure = i;
        this.MouseMove += (s, e) => LabelDelayMove(closure, e, l);
    }
}

private void LabelDelayMove(int i, MouseEventArgs e, Label l)
{
    Point p = new Point(e.X + i * 12 - 5, e.Y - 15);
    var timer = new System.Threading.Timer((_) => LabelMove(l, p), null, i * 70, System.Threading.Timeout.Infinite);
}

private void LabelMove(Label l, Point location)
{
    this.BeginInvoke(new Action(() => l.Location = location));
}
private void未激活(字符串msg)
{
var message=msg.ToCharArray();
for(int i=0;iLabelDelayMove(closure,e,l);
}
}
专用无效标签移动(int i、MouseEventArgs e、标签l)
{
点p=新点(e.X+i*12-5,e.Y-15);
var timer=new System.Threading.timer(()=>LabelMove(l,p),null,i*70,System.Threading.Timeout.Infinite);
}
专用无效标签移动(标签l,点位置)
{
this.BeginInvoke(新操作(()=>l.Location=Location));
}

链接到原始帖子?啊,我在你的问题中看到了,但不是这个答案。@Richard,是的,我已经添加了一个问题的链接,但我一生都看不到哪里可能有答案的链接。他们真的应该放下论坛,到这里来参加聚会!
private void Unreactive(string msg)
{
    var message = msg.ToCharArray();

    for(int i = 0; i < message.Length; i++)
    {
        var l = new Label()
        { 
            Text = message[i].ToString(), 
            AutoSize = true, 
            TextAlign = ContentAlignment.MiddleCenter 
        };
        Controls.Add(l);
        int closure = i;
        this.MouseMove += (s, e) => LabelDelayMove(closure, e, l);
    }
}

private void LabelDelayMove(int i, MouseEventArgs e, Label l)
{
    Point p = new Point(e.X + i * 12 - 5, e.Y - 15);
    var timer = new System.Threading.Timer((_) => LabelMove(l, p), null, i * 70, System.Threading.Timeout.Infinite);
}

private void LabelMove(Label l, Point location)
{
    this.BeginInvoke(new Action(() => l.Location = location));
}