本文介绍了为什么会这样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 

 

我正在使用c#进行简单的Silverlight测试。

I'm doing a simple test in silverlight with c# behind.

这是我的xaml和代码:

here is my xaml and code:

-------------------------- xaml ---------------------------

--------------------------xaml---------------------------

<UserControl
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions" x:Class="Test.MainPage"
  mc:Ignorable="d" Height="300" Width="400">
  <Canvas x:Name="LayoutRoot" Background="Aqua">
    <Rectangle Canvas.Left="98" Canvas.Top="59" Height="130" Name="rectangle1" Stroke="Black" StrokeThickness="1" Width="175" Fill="#FF9D3A3A" />
    <TextBlock Canvas.Left="0" Canvas.Top="0" Height="23" Name="textBlock1" Text="TextBlock" />
  </Canvas>
</UserControl>

--------------------- c# - ------------------

---------------------c#--------------------

using System;
using System.Windows.Controls;
using System.Windows.Input;

namespace Test
{
  public partial class MainPage : UserControl
  {
    private MouseEventHandler meh;
    private Int64 count;

    public MainPage()
    {
      InitializeComponent();
      InitialSet();
    }

    private void InitialSet()
    {
      rectangle1.MouseLeftButtonDown += new MouseButtonEventHandler(rectangle1_MouseLeftButtonDown);
      rectangle1.MouseLeftButtonUp += new MouseButtonEventHandler(rectangle1_MouseLeftButtonUp);
      meh = new MouseEventHandler(rectangle1_MouseMove);
    }

    private void rectangle1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
      rectangle1.MouseMove += meh;
    }

    private void rectangle1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
      rectangle1.MouseMove -= meh;
    }

    private void rectangle1_MouseMove(object sender, MouseEventArgs e)
    {
      count++;
      textBlock1.Text = count.ToString();
    }
  }
}

我的意图是当你左键单击矩形并开始拖动,左上角的数字会增加。当你这样做时效果很好。

my intention is that when you left-click the rectangle and start to drag, the number on the top-left shall increases.it works well when you do this.

但如果你尝试将鼠标拖到矩形外面释放左按钮,然后返回矩形,虽然左按钮已启动,但数字仍在增加(无论您再次单击它)。声明

but if you try drag your mouse outside the rectangle and release the left button, and then back to the rectangle, although your left button is up, the number is still increasing(no matter you click it again). the statement


rectangle1.MouseMove -= meh;<br/>

推荐答案

放一个"rectangle1_MouseLeftButtonUp"中的断点并查看它是否发生。

Put a breakpoint in "rectangle1_MouseLeftButtonUp" and see if it happens.

当您释放矩形外的按钮时,"rectangle1_MouseLeftButtonUp"不应该发生。当你回到矩形时,"rectangle1_MouseMove"和仍然附加并不断更新计数器。

As you release the button outside of the rectangle, "rectangle1_MouseLeftButtonUp" should not happen. When you are back over the rectangle, "rectangle1_MouseMove" is still attached and keeps updating the counter.

要处理此问题,您需要"鼠标捕获"。功能,即使鼠标远离此控件,也可以将鼠标事件定向到特定控件:

http://msdn.microsoft.com/en-us/library/cc189029(VS.95).aspx#mouse_capture

To handle this, you have a "mousecapture" feature, that allows to keep directing mouse events to a particular control even if the mouse moves away from this control :http://msdn.microsoft.com/en-us/library/cc189029(VS.95).aspx#mouse_capture

BTW,此论坛是一个通用的C#语言论坛,用于与Silverlight相关的问题, 可能更好。

BTW, this forum is a general C#Language forum, for Silverlight related questions, http://forums.silverlight.net/ is likely better.


这篇关于为什么会这样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 20:30
查看更多