XAML 如果感知到特定条件,则清除Entry.Text值

vd2z7a6w  于 2023-02-06  发布在  其他
关注(0)|答案(1)|浏览(119)

有没有一种方法,我可以清除entry.text时,曾经满足一些条件?我想我的问题是,我如何捕捉文本的条目在Xamarin中改变了(sender,TextChangedEventArgs)?

private void EntryBoxBarCode_TextChanged(object sender, TextChangedEventArgs e)
{
    if (EntryBoxBarCode.Text != "")
    {
        var entry = new Entry();
        entry.Text = e.NewTextValue;
        WorkFormCheck(entry.Text);

        if (typeOfBarCode != "")
        {
            //Here is the condition where I want to clear the text
            entry.Text = "";
            EntryBoxBarCode.Focus();
        }
    }
    else
    {
        //pasing the right value of the entry, then focus to other Entry
        EntryPackCode.Focus();
    }         
}

XAML:

<Entry Grid.Row="0" Grid.Column="1" x:Name="EntryBoxBarCode" WidthRequest="250" TextChanged="EntryBoxBarCode_TextChanged"/>
sc4hvdpw

sc4hvdpw1#

我不明白的是为什么你要在运行时在TextChanged上创建一个条目,这会在每次你在调用TextChanged事件的条目中输入文本时创建一个又一个条目。
当你在这里创建一个新条目时,它并不在你的UI上,如果你想让你的UI上的条目触发它,你必须给予触发条目一个名字,然后使用这个名字来检查条目中的内容并相应地更新。
您的XAML类似于:

<Entry Grid.Row="0" Grid.Column="1" x:Name="EntryBoxBarCode" WidthRequest="250" TextChanged="EntryBoxBarCode_TextChanged"/>
<Entry Grid.Row="" Grid.Column="1" x:Name="EntryPackCode" WidthRequest="250" />

private void EntryBoxBarCode_TextChanged(object sender, TextChangedEventArgs e)
    {
        if (EntryBoxBarCode.Text != "")
        {
            WorkFormCheck(EntryBoxBarCode.Text);

            if (typeOfBarCode != "")
            {
                //Here is the condition where I want to clear the text
                EntryBoxBarCode.Text = "";
                //EntryBoxBarCode.Focus(); //not sure this is required or not since you should already have focus here.
            }
        }
        else
        {
            //passing the right value of the entry, then focus to other Entry
            EntryPackCode.Focus();
        }         
    }

相关问题