简体   繁体   English

UWP:绑定+ TextChanging = JIT win32异常

[英]UWP: Binding + TextChanging = JIT win32 exception

I am 100% sure I am doing this the wrong way and this problem is "by design". 我100%肯定我做错了这个方法,而这个问题是“设计使然”。

I want to have a Slider and a TextBox that displays its value. 我想要一个Slider和一个显示其值的TextBox。 The user can either use the Slider, or manually enter a number in the TextBox. 用户可以使用滑块,也可以在文本框中手动输入数字。

I also wanted to take advantage of the TextChanging event to ignore any non-numerical entries. 我还想利用TextChanging事件来忽略任何非数字条目。 TextChanged would only function after a user has entered something and it's not a preferable scenario, and KeyDown would not capture other methods of input like ink or speech. TextChanged仅在用户输入了某些内容后才起作用,这不是一个可取的方案,并且KeyDown不会捕获其他输入方法,如墨水或语音。

So I have this: 所以我有这个:

<StackPanel>
    <Slider x:Name="Size" Value="100" Maximum="100" Minimum="0" />
    <TextBox x:Name="SizeText" Text="{Binding ElementName=Size,Path=Value,Mode=TwoWay}" TextChanging="SizeText_TextChanging" />
</StackPanel>

Where "SizeText_TextChanging" is simply an empty block of code right now: 现在,“ SizeText_TextChanging”只是一个空代码块:

    private void SizeText_TextChanging(TextBox sender, TextBoxTextChangingEventArgs args)
    {
        // Nothing Here.
    }

This code builds, but at startup the app throws a JIT unhandled win32 exception and closes. 这段代码会生成,但是在启动时,应用程序会引发未处理的JIT Win32异常并关闭。

Changing TextChanging to TextChanged works fine, but again I prefer to get TextChanging to work (or something similar) to give a better user experience. 将TextChanging更改为TextChanged可以很好地工作,但是我还是想让TextChanging正常工作(或类似的操作),以提供更好的用户体验。

"Mode" also has no effect. “模式”也无效。 I tried all three different Modes, all crash. 我尝试了所有三种不同的模式,都崩溃了。 By removing the binding altogether and giving the Text property any value works fine. 通过完全删除绑定并赋予Text属性任何值都可以正常工作。

I also thought that maybe having the TextChanging event handler empty is the problem, so I borrowed the code below from here but the app still crashes: 我还认为可能是TextChanging事件处理程序为空是问题所在,所以我从这里借用了下面的代码,但应用仍然崩溃:

private void SizeText_TextChanging(TextBox sender, TextBoxTextChangingEventArgs args)
{
    if (!Regex.IsMatch(sender.Text, "^\\d*\\.?\\d*$") && sender.Text != "")
    {
        int pos = sender.SelectionStart - 1;
        sender.Text = sender.Text.Remove(pos, 1);
        sender.SelectionStart = pos;
    }
}

Like I said, I am probably approaching this the wrong way. 就像我说的那样,我可能正以错误的方式来对待。 I am just starting to learn UWP and C# so I am a total noob. 我才刚刚开始学习UWP和C#,所以我完全是菜鸟。 But I have read everything I could about TextChanging and it simply talks about rendering the value and the associated cautions of what not to write within the TextChanging event. 但是我已经阅读了有关TextChanging的所有内容,并且只讨论了呈现值以及有关在TextChanging事件中不要写的内容的注意事项。 So while it sounds like the app is being thrown into a loop trying to read the value of the slider and trying to see what the TextChanging event says, I don't see how to fix it. 因此,虽然听起来好像该应用程序陷入了一个循环,试图读取滑块的值并尝试查看TextChanging事件的内容,但我看不到如何解决它。 Please help! 请帮忙!

Thank you 谢谢

I don't know why this is happening, but a workaround is to register the TextChanging event handler only once SizeText (or the page) has loaded and using x:Bind instead of Binding: 我不知道为什么会这样,但是一种解决方法是仅在加载SizeText (或页面)并使用x:Bind而不是Binding时注册TextChanging事件处理程序:

XAML XAML

<TextBox x:Name="SizeText" Text="{x:Bind Size.Value, Mode=TwoWay}" Loaded="SizeText_Loaded"/>

CS CS

private void SizeText_Loaded(object sender, RoutedEventArgs e)
{
    SizeText.TextChanging += SizeText_TextChanging;
}

private void SizeText_TextChanging(TextBox sender, TextBoxTextChangingEventArgs args)
{
}

I meet the same problem but this solution doesn't works for me. 我遇到了同样的问题,但是此解决方案不适用于我。 As @Mushriq, I use _TextChanging() to ignore any non-numerical entries in a form. 作为@Mushriq,我使用_TextChanging()忽略表单中的任何非数字条目。 But my form contains a lot of numeric fields and also a master-detail part, which contains 2 of these fields. 但是我的表单包含许多数字字段以及一个主从部分,其中包含这些字段中的2个。 The problem is that I enter in the _Loaded the first time that I display a part, but not for the other parts. 问题是我第一次显示零件时输入_Loaded ,而其他零件则不输入。 When I display existing parts there is no problem, but if I add a new part I get the exception. 当显示现有零件时,没有问题,但是如果添加新零件,则会出现异常。 Is there a way to adapt the solution to my case? 有没有办法使解决方案适合我的情况?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM