wpf 如果在“TextBox”上单击鼠标左键,则元素将加速

tvz2xvvm  于 2023-11-21  发布在  其他
关注(0)|答案(2)|浏览(179)

查看问题的操作:
1.创建一个WPF项目(我使用.NET 7)
1.在下面插入代码
XAML:

<Window x:Class="TestApp.MainWindow"
        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:local="clr-namespace:TestApp"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <TextBox HorizontalAlignment="Left" Margin="96,240,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="227" Height="97"/>
        <Button Content="Start" HorizontalAlignment="Left" Margin="400,240,0,0" VerticalAlignment="Top" Height="97" Width="160" Click="Button_Click"/>
        <Border Name="MyBorder" BorderBrush="Black" BorderThickness="1" Margin="96,45,0,0" Background="Black" VerticalAlignment="Top" HorizontalAlignment="Left" Height="131" Width="76"/>

    </Grid>
</Window>

字符串
C#:

using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace TestApp
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        async private void Button_Click(object sender, RoutedEventArgs e)
        {
            for (int i = 96; i <= 300; i++)
            {
                MyBorder.Margin = new Thickness(i,45,0,0);
                await Task.Delay(5);
            }
        }
    }
}

3)点击开始按钮,指向文本框并按下鼠标左键,看到边框开始移动得更快。
为什么会发生这种情况?如何解决此问题?

问题视频:https://drive.google.com/file/d/1KWsp-ghJMnntW9KqbxcHxjtQ_fQ2tTbo/view?usp=share_link

disho6za

disho6za1#

尝试复制这个问题没有工作。我确实注意到你缺少一个“using System.Threading.Tasks”,并且当多次单击按钮时,边框会出现故障。我建议在for循环之前禁用按钮,并在for循环完成后再次启用它,如下所示:
XAML:

<Window x:Class="TestApp.MainWindow"
    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:local="clr-namespace:TestApp"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800">
<Grid>
    <TextBox HorizontalAlignment="Left" Margin="96,240,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="227" Height="97"/>
    <Button Name="start_button" Content="Start" HorizontalAlignment="Left" Margin="400,240,0,0" VerticalAlignment="Top" Height="97" Width="160" Click="Button_Click"/>
    <Border Name="MyBorder" BorderBrush="Black" BorderThickness="1" Margin="96,45,0,0" Background="Black" VerticalAlignment="Top" HorizontalAlignment="Left" Height="131" Width="76"/>
</Grid>

字符串
C#:

using System.Threading.Tasks;
using System.Windows;

namespace TestApp
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private async void Button_Click(object sender, RoutedEventArgs e)
        {
            start_button.IsEnabled = false;
            for (int i = 96; i <= 300; i++)
            {
                MyBorder.Margin = new Thickness(i,45,0,0);
                await Task.Delay(5);
            }
            start_button.IsEnabled = true;
        }
    }
}


也许你可以提供更多关于你使用的windows版本的信息,以及你是否改变了项目中的其他文件。(也许你可以尝试重新创建项目,看看是否会改变一些东西)
证明它对我有效:
任务延迟(5):
x1c 0d1x的数据

  • 任务延迟(10):


7z5jn7bk

7z5jn7bk2#

我建议使用动画。这将给给予更平滑的运动。

<Grid>
        <TextBox HorizontalAlignment="Left" Margin="96,240,0,0" TextWrapping="Wrap"
                    Text="TextBox" VerticalAlignment="Top" Width="227" Height="97"/>
        <Button Content="Start" HorizontalAlignment="Left" Margin="400,240,0,0"
                VerticalAlignment="Top" Height="97" Width="160" Click="Button_Click"/>
        <Border Name="MyBorder" BorderBrush="Black" BorderThickness="1"
                Margin="96,45,0,0" Background="Black" VerticalAlignment="Top"
                HorizontalAlignment="Left" Height="131" Width="76" RenderTransformOrigin="0.5,0.5">
            <Border.RenderTransform>
                <TranslateTransform x:Name="brdMove" X="0"/>
            </Border.RenderTransform>
        </Border>
    </Grid>

个字符

相关问题