C# TextBox.TextChanged事件在Windows Phone 7 emulator上触发两次

C# TextBox.TextChanged事件在Windows Phone 7 emulator上触发两次,c#,xaml,silverlight,windows-phone-7,textbox,C#,Xaml,Silverlight,Windows Phone 7,Textbox,我有一个非常简单的测试应用程序,只是用来玩WindowsPhone7。我刚刚在标准UI模板中添加了一个TextBox和一个TextBlock。唯一的自定义代码如下所示: public partial class MainPage : PhoneApplicationPage { public MainPage() { InitializeComponent(); } private int counter = 0; private vo

我有一个非常简单的测试应用程序,只是用来玩WindowsPhone7。我刚刚在标准UI模板中添加了一个
TextBox
和一个
TextBlock
。唯一的自定义代码如下所示:

public partial class MainPage : PhoneApplicationPage
{
    public MainPage()
    {
        InitializeComponent();
    }

    private int counter = 0;

    private void TextBoxChanged(object sender, TextChangedEventArgs e)
    {
        textBlock1.Text += "Text changed " + (counter++) + "\r\n";
    }
}
TextBox.TextChanged
事件连接到XAML中的
TextBoxChanged

<TextBox Height="72" HorizontalAlignment="Left" Margin="6,37,0,0"
         Name="textBox1" Text="" VerticalAlignment="Top"
         Width="460" TextChanged="TextBoxChanged" />

但是,每次我在emulator中运行时按下一个键(屏幕键盘或物理键盘,按下Pause启用后者),计数器都会增加两次,在
文本块中显示两行。我试过的每一件事都表明,这场比赛确实进行了两次,我不知道为什么。我已经验证了它只被订阅了一次-如果我在
主页
构造函数中取消订阅,当文本发生变化时(文本块)什么也不会发生

我在一个普通的Silverlight应用程序中尝试了相同的代码,但没有出现。我现在还没有一部物理手机来复制它。我还没有在WindowsPhone7中发现这是一个已知问题的任何记录

谁能解释一下我做错了什么,或者我应该把这当作一个bug来报告吗

编辑:为了减少使用两个文本控件的可能性,我尝试完全删除
TextBlock
,并将TextBoxChanged方法更改为仅递增
计数器
。然后我在模拟器中运行,输入10个字母,然后在
计数器++上设置断点行(只是为了消除闯入调试器导致问题的任何可能性),它将
计数器显示为20


编辑:我现在。。。我们来看看会发生什么。

在我看来确实像个bug,如果每次文本更改时都试图引发事件,可以尝试使用双向绑定,不幸的是,这不会引发每次按键更改事件(仅当字段失去焦点时)。如果您需要,这里有一个解决方法:

        this.textBox1.TextChanged -= this.TextBoxChanged;
        textBlock1.Text += "Text changed " + (counter++) + "\r\n";
        this.textBox1.TextChanged += this.TextBoxChanged;

我会选择这个bug,主要是因为如果你把
KeyDown
keydup
事件放在那里,它表明它们只被触发一次(每一个),但是
TextBoxChanged
事件被触发两次

免责声明-我不熟悉xaml的细微差别,我知道这听起来不合逻辑。。。但无论如何,我的第一个想法是尝试作为普通事件参数而不是textchangedventargs传递。没有道理,但可能会有帮助吗?好像我以前见过这样的双重触发,要么是由于一个bug,要么是由于幕后发生了2个add事件处理程序调用。。。但我不确定是哪个


如果您需要快速和肮脏的,再一次,我没有经验的xaml-我的下一步将只是跳过该文本框的xaml作为一个快速的解决办法。。。现在就完全用c语言编写文本框,直到你能找出错误或棘手的代码。。。也就是说,如果您需要临时解决方案。

我相信这一直是Compact框架中的一个缺陷。它一定是被带入了WP7中。

这听起来确实像是一个bug。作为一种解决方法,您可以始终使用Rx的
DistinctUntilChanged
。有一个重载允许您指定不同的键

此扩展方法返回可观察的TextChanged事件,但跳过连续的重复事件:

public static IObservable<IEvent<TextChangedEventArgs>> GetTextChanged(
    this TextBox tb)
{
    return Observable.FromEvent<TextChangedEventArgs>(
               h => textBox1.TextChanged += h, 
               h => textBox1.TextChanged -= h
           )
           .DistinctUntilChanged(t => t.Text);
}
publicstaticiobservable GetTextChanged(
此文本框(tb)
{
返回可观察的.FromEvent(
h=>textBox1.TextChanged+=h,
h=>textBox1.TextChanged-=h
)
.DistinctUntilChanged(t=>t.Text);
}

一旦bug被修复,您可以简单地删除
distinctunitrichanged
行。

我认为这不是bug。。 将值指定给textchanged事件内的文本属性时,textbox值将更改,这将再次调用text changed事件

在Windows窗体应用程序中尝试此操作,可能会出现错误


“System.Windows.Forms.dll中发生“System.StackOverflowException”类型的未处理异常”

WP7中两次触发
TextChanged
事件的原因是
TextBox
已为Metro外观模板化的副作用

如果在Blend中编辑
文本框
模板,您将看到它包含一个用于禁用/只读状态的辅助
文本框
。作为副作用,这会导致事件触发两次

如果不需要这些状态,您可以更改模板以删除额外的
文本框
(和相关状态),或者修改模板以在禁用/只读状态下实现不同的外观,而无需使用辅助
文本框


这样,事件只会触发一次。

< Pt> StFiffigk是正确的,考虑使用这个模板< /P>
<Application.Resources>
        <ControlTemplate x:Key="PhoneDisabledTextBoxTemplate" TargetType="TextBox">
            <ContentControl x:Name="ContentElement" BorderThickness="0" HorizontalContentAlignment="Stretch" Margin="{StaticResource PhoneTextBoxInnerMargin}" Padding="{TemplateBinding Padding}" VerticalContentAlignment="Stretch"/>
        </ControlTemplate>
        <Style x:Key="TextBoxStyle1" TargetType="TextBox">
            <Setter Property="FontFamily" Value="{StaticResource PhoneFontFamilyNormal}"/>
            <Setter Property="FontSize" Value="{StaticResource PhoneFontSizeMediumLarge}"/>
            <Setter Property="Background" Value="{StaticResource PhoneTextBoxBrush}"/>
            <Setter Property="Foreground" Value="{StaticResource PhoneTextBoxForegroundBrush}"/>
            <Setter Property="BorderBrush" Value="{StaticResource PhoneTextBoxBrush}"/>
            <Setter Property="SelectionBackground" Value="{StaticResource PhoneAccentBrush}"/>
            <Setter Property="SelectionForeground" Value="{StaticResource PhoneTextBoxSelectionForegroundBrush}"/>
            <Setter Property="BorderThickness" Value="{StaticResource PhoneBorderThickness}"/>
            <Setter Property="Padding" Value="2"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="TextBox">
                        <Grid Background="Transparent">
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="CommonStates" ec:ExtendedVisualStateManager.UseFluidLayout="True">
                                    <VisualState x:Name="Normal"/>
                                    <VisualState x:Name="MouseOver"/>
                                    <VisualState x:Name="Disabled">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="EnabledBorder">
                                                <DiscreteObjectKeyFrame KeyTime="0">
                                                    <DiscreteObjectKeyFrame.Value>
                                                        <Visibility>Collapsed</Visibility>
                                                    </DiscreteObjectKeyFrame.Value>
                                                </DiscreteObjectKeyFrame>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="ReadOnly">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="EnabledBorder">
                                                <DiscreteObjectKeyFrame KeyTime="0">
                                                    <DiscreteObjectKeyFrame.Value>
                                                        <Visibility>Collapsed</Visibility>
                                                    </DiscreteObjectKeyFrame.Value>
                                                </DiscreteObjectKeyFrame>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                                <VisualStateGroup x:Name="FocusStates">
                                    <VisualState x:Name="Focused">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="EnabledBorder">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneTextBoxEditBackgroundBrush}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="EnabledBorder">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneTextBoxEditBorderBrush}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="Unfocused"/>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                            <VisualStateManager.CustomVisualStateManager>
                                <ec:ExtendedVisualStateManager/>
                            </VisualStateManager.CustomVisualStateManager>
                            <Border x:Name="EnabledBorder" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Margin="{StaticResource PhoneTouchTargetOverhang}">
                                <ContentControl x:Name="ContentElement" BorderThickness="0" HorizontalContentAlignment="Stretch" Margin="{StaticResource PhoneTextBoxInnerMargin}" Padding="{TemplateBinding Padding}" VerticalContentAlignment="Stretch"/>
                            </Border>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Application.Resources>

崩溃
boolean already = false;
private void Tweet_SizeChanged(object sender, EventArgs e)
{
    if (!already)
    {
        already = true;
        ...
    }
    else
    {
    already = false;
    }
}
private string filterText = String.Empty;

private void SearchBoxUpdated( object sender, TextChangedEventArgs e )
{
    if ( filterText != filterTextBox.Text )
    {
        // one call per change
        filterText = filterTextBox.Text;
        ...
    }

}