【问题标题】:Xamarin Forms Android key event handling on pages页面上的 Xamarin Forms Android 关键事件处理
【发布时间】:2016-09-08 22:44:04
【问题描述】:

目前我正在为 Android 开发 PCL Xamarin Forms。我现在正在处理一个通过 Text_Changed 事件捕获扫描数据的条目。我想知道是否可以在内容页面上处理此扫描数据的事件。我在 Xamarin 中缺少类似 KeyPress 的东西。 有人有解决办法吗?

【问题讨论】:

    标签: c# xamarin.android xamarin.forms onkeypress


    【解决方案1】:

    您可以使用 CustomEntry。

    在表单中添加一个新类:

    public class CustomEntry : Entry
    {
        public Action DonePressed = delegate {};
    
    }
    

    在您的 Android 项目中,添加 CustomEntryRenderer:

     class CustomEntryRenderer : EntryRenderer
     {
        private CustomEntry customEntry;
    
        protected override void OnElementChanged (ElementChangedEventArgs<Entry> e)
        {
            base.OnElementChanged (e);
            if (Control != null) 
            {                
                Control.ImeOptions = ImeAction.Done;
                Control.EditorAction += (sender, args) => {
                    if (args.ActionId == ImeAction.Done) {                  
                        var entry = (CustomEntry)Element;
                        entry.DonePressed();
                    }
                };
            }       
    

    【讨论】:

    • 这就是我现在的工作方式。但是我想在没有条目的情况下工作。
    猜你喜欢
    • 2017-07-22
    • 1970-01-01
    • 2014-09-13
    • 1970-01-01
    • 2020-07-04
    • 1970-01-01
    • 1970-01-01
    • 2014-11-11
    • 2010-10-13
    相关资源
    最近更新 更多