C# Xamarin的奇怪行为形成了一个入口

C# Xamarin的奇怪行为形成了一个入口,c#,android,xaml,custom-controls,xamarin-forms,C#,Android,Xaml,Custom Controls,Xamarin Forms,我和Xamarin的冒险永远不会结束 我发现在ListView中使用条目控件时出现问题。我的ContentPage中有一个带有ViewCells的ListView。每个ViewCell都有一个CustomEntry(我创建的) 我想要控制用户是否将条目留空。如果他将条目留空,我希望将该条目的最后一个值留空 对我来说,最简单的解决方案是使用有重点和无重点的事件。但事件之间的行为非常奇怪 public class CustomEntry : Entry { private string ol

我和Xamarin的冒险永远不会结束

我发现在ListView中使用条目控件时出现问题。我的ContentPage中有一个带有ViewCells的ListView。每个ViewCell都有一个CustomEntry(我创建的)

我想要控制用户是否将条目留空。如果他将条目留空,我希望将该条目的最后一个值留空

对我来说,最简单的解决方案是使用有重点和无重点的事件。但事件之间的行为非常奇怪

public class CustomEntry : Entry
{
    private string oldValue;

    public CustomEntry()
    {
        this.oldValue = "1";            
        this.Focused += CustomEntry_Focused;
        this.Unfocused += CustomEntry_Unfocused;                        
    }

    private void CustomEntry_Focused(object sender, FocusEventArgs e)
    {
        if (this.Text != null)
        {
            this.oldValue = this.Text;
        }  
    }

    private void CustomEntry_Unfocused(object sender, FocusEventArgs e)
    {
        try
        {
            if (this.Text != null) { 
                //If the user leaves the field empty                
                if (this.Text.Trim().Equals(string.Empty))
                {
                    this.Text = this.oldValue;
                }
            }
        }
        catch (FormatException ex) { }
    }
}
MainPage.XAML的XAML代码:

  <Grid>
  <ListView x:Name="lst" BackgroundColor="Red" >
    <ListView.ItemTemplate>
      <DataTemplate>
        <ViewCell>            
          <StackLayout Orientation="Vertical" BackgroundColor="Yellow" >
            <local:CustomEntry BackgroundColor="Gray" FontSize="30" 
                 Keyboard="Numeric" Text="{Binding NumElements}" />
          </StackLayout>          
        </ViewCell>
      </DataTemplate>
    </ListView.ItemTemplate>
  </ListView>
</Grid>

最后是MainPage.xaml.cs:

public partial class MainPage : ContentPage
{
    private List<ItemModel> coleccio;

    public MainPage()
    {
        InitializeComponent();
        this.coleccio = new List<ItemModel>();
        this.coleccio.Add(new ItemModel("1")); //"1" value for NumElements property
        this.coleccio.Add(new ItemModel("2")); //"2" value for NumElements property
        this.coleccio.Add(new ItemModel("3")); //"3" value for NumElements property
        this.lst.ItemsSource = this.coleccio;
    }
}
public分部类主页面:ContentPage
{
私人名单;
公共主页()
{
初始化组件();
this.coleccio=新列表();
NumElements属性的this.coleccio.Add(新的ItemModel(“1”));/“1”值
NumElements属性的this.coleccio.Add(新的ItemModel(“2”));/“2”值
NumElements属性的this.coleccio.Add(新ItemModel(“3”);/“3”值
this.lst.ItemsSource=this.coleccio;
}
}
要重现问题,请执行以下操作:

  • 选项卡第一个自定义条目
  • 选项卡第二个自定义条目
  • 按键盘上的Del键
  • 您将看到光标在CustomEntry的开头移动,但不会删除文本
  • 调试跟踪是:

  • 点击第一个自定义条目
  • 触发第一个CustomEntry事件
  • 激发未聚焦的第一个CustomEntry事件
  • 点击第二个自定义条目
  • 按键盘的Del键
  • 触发第二个CustomEntry事件
  • 激发未聚焦的第二个CustomEntry事件
  • 触发第一个CustomEntry事件
  • 激发未聚焦的第一个CustomEntry事件
  • 触发第二个CustomEntry事件
  • 光标在CustomEntry的开头移动
  • 我做错了什么?或者是关于Xamarin入口控制的bug

    我正在使用Nexus5设备和Android 6.0进行调试