0

我是 Silverlight 的新手,我正在运行时在屏幕上绘制一个矩形,

我有一个类( SelectionBox ),在主页上,我有一个画布,当单击该画布时,我有以下函数调用

private void BeginDrawing(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            _initXPos = e.GetPosition(drawingArea).X;
            _initYPos = e.GetPosition(drawingArea).Y;
            _selectionBox = new SelectionBox ();
            drawingArea.Children.Add(_selectionBox);
            _isDrawing = true;
        }

并且该处理程序已通过混合事件(MouseLeftButtonDown)添加

当鼠标移动时,会调用下面的方法,也是通过 Blend 事件添加的,

private void UpdateSelectionBox(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if(!_isDrawing &&)
                return;
            double rectWidth, rectHeight, rectXPos, rectYPos;
            if (e.GetPosition(drawingArea).X >= _initXPos)
            {
                rectWidth = e.GetPosition(drawingArea).X - _initXPos;
                rectXPos = _initXPos;
            }
            else
            {
                rectWidth = _initXPos - e.GetPosition(drawingArea).X;
                rectXPos = e.GetPosition(drawingArea).X;
            }

            if (e.GetPosition(drawingArea).Y >= _initYPos)
            {
                rectHeight = e.GetPosition(drawingArea).Y - _initYPos;
                rectYPos = _initYPos;
            }
            else
            {
                rectHeight = _initYPos - e.GetPosition(drawingArea).Y;
                rectYPos = e.GetPosition(drawingArea).Y;
            }

            _selectionBox.Width = Math.Abs(rectWidth - 20);
            _selectionBox.Height = Math.Abs(rectHeight - 20);
            Canvas.SetLeft(_selectionBox, Math.Abs(rectXPos - 20));
            Canvas.SetTop(_selectionBox, Math.Abs(rectYPos - 20));
        }

当 mouseLeftButtonUp 被触发时,以下处理程序应该工作,

private void StopDrawingAndSelect(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            _isDrawing = false;
            drawingArea.Children.Remove(_selectionBox);
        }

但不幸的是它永远不会触发,我放了一个断点并尝试调试它,但它永远不会到达,我不确定为什么,这是 SelectionBox 类的 XAML

<UserControl
    ...

    <Grid x:Name="LayoutRoot">
        <Rectangle Fill="#00F4F4F5" Stroke="Black" StrokeDashArray="1 2"/>
    </Grid>
</UserControl>

这是 MainPage 的 XAML

<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:SelectionBoxTraining"
    x:Class="SelectionBoxTraining.MainPage"
    Width="640" Height="480">

    <Grid x:Name="LayoutRoot" Background="White">
        <Canvas Margin="165,0,0,0" x:Name="drawingArea" MouseLeftButtonDown="BeginDrawing" Background="#FF959FD6" MouseMove="UpdateSelectionBox" MouseLeftButtonUp="StopDrawingAndSelect" />

    </Grid>
</UserControl>

请问有人可以帮我吗?我尝试了很多事情,但没有奏效

希望能在这里得到帮助

注意:这些属性不包含在 XAML 代码中以节省空间,

谢谢你。

4

2 回答 2

0

它现在正在工作,即使我不相信答案,但我通过双击 Blend 的 MouseUp 事件位置更改了 OnMouseUp 函数,它变成了

private void drawingArea_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            _isDrawing = false;
            drawingArea.Children.Remove(_selectionBox);
        }

有人解释导致问题的原因吗?处理程序的名称有这种效果吗?我想知道..

于 2012-03-08T11:47:30.347 回答
0

我猜原因是您的矩形正在获取 UP 事件并且(不知何故?)没有将其冒泡到画布上。所以尝试将 ishitestvisible 设置为 false:

private void BeginDrawing(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            _initXPos = e.GetPosition(drawingArea).X;
            _initYPos = e.GetPosition(drawingArea).Y;
            _selectionBox = new SelectionBox ();

            _selectionBox.IsHitTestVisible = false;

            drawingArea.Children.Add(_selectionBox);
            _isDrawing = true;
        }

`

于 2012-03-07T16:48:09.760 回答