【问题标题】:Xamarin Forms Entry invoke Completed eventXamarin Forms Entry 调用 Completed 事件
【发布时间】:2017-01-07 10:19:04
【问题描述】:

我目前正在 Xamarin Forms 中的登录和注册页面上工作,在将键盘的完成按钮更改为下一个并继续最后一个之后,我不再在 Android 上收到 Completed 事件(在 iOS 上工作正常) .在自定义渲染器中,我可以捕获 Control.EditorAction 事件,该事件现在与 Completed 事件的行为相同,但我似乎无法在条目本身上调用 Completed 事件。

在 EntryRenderer 中

Control.EditorAction += (object sender, TextView.EditorActionEventArgsargs) =>
{
    if (entryExt.ReturnKeyType != ReturnKeyTypes.Next)
        entryExt.Unfocus();

    // Call all the methods attached to base_entry event handler Completed
    entryExt.InvokeCompleted();
};

在 EntryExt 内(直接扩展 Entry)

public void InvokeCompleted()
{
    Completed?.Invoke(this, null);
}

但是由于错误无法调用 Completed 事件

Error CS0070: The event `Xamarin.Forms.Entry.Completed' can only appear on the left hand side of += or -= when used outside of the type `Xamarin.Forms.Entry'

有没有办法调用 Completed 事件?我宁愿在我的视图中没有单独的事件处理程序。

【问题讨论】:

    标签: c# android xamarin xamarin.forms


    【解决方案1】:

    通过更改解决了问题

    entryExt.InvokeCompleted(); 
    

    在EditorAction里面去

    ((IEntryController)Element).SendCompleted(); 
    

    将完成的事件发送回 Entry 基类。

    【讨论】:

      【解决方案2】:

      我已经实现了一个类似的实现方法,它允许扩展 Return 按钮以实现任何类型:Go、Next、Done、Send 和 Search。

      示例应用

      Here is a Xamarin.Forms app 我已经实现了这种方法。随意下载 repo 并尝试一下!

      1。创建自定义条目

      在 Xamarin.Forms PCL 中,创建自定义条目

      public class EntryWithCustomKeyboardReturnButton : Entry
      {
          public new event EventHandler Completed;
      
          public static readonly BindableProperty ReturnTypeProperty =
              BindableProperty.Create<EntryWithCustomKeyboardReturnButton, ReturnType>(s => s.ReturnType, ReturnType.Done);
      
          public ReturnType ReturnType
          {
              get { return (ReturnType)GetValue(ReturnTypeProperty); }
              set { SetValue(ReturnTypeProperty, value); }
          }
      
          public void InvokeCompleted()
          {
              Completed?.Invoke(this, null);
          }
      }
      
      public enum ReturnType
      {
          Go,
          Next,
          Done,
          Send,
          Search
      }
      

      2。创建 iOS 自定义渲染器

      在 iOS PCL 中,创建此自定义渲染器

      [assembly: ExportRenderer(typeof(EntryWithCustomKeyboardReturnButton), typeof(EntryWithCustomKeyboardReturnButtonCustomRenderer))]
      namespace Sample.iOS
      {
          public class EntryWithCustomKeyboardReturnButtonCustomRenderer : EntryRenderer
          {
              protected override void OnElementChanged(ElementChangedEventArgs e)
              {
                  base.OnElementChanged(e);
      
                  var customEntry = Element as EntryWithCustomKeyboardReturnButton;
      
                  if (Control != null && customEntry != null)
                  {
                      SetKeyboardButtonType(customEntry.ReturnType);
      
                      Control.ShouldReturn += (UITextField tf) =>
                      {
                          customEntry?.InvokeCompleted();
                          return true;
                      };
                  }
              }
      
              void SetKeyboardButtonType(ReturnType returnType)
              {
                  switch (returnType)
                  {
                      case ReturnType.Go:
                          Control.ReturnKeyType = UIReturnKeyType.Go;
                          break;
                      case ReturnType.Next:
                          Control.ReturnKeyType = UIReturnKeyType.Next;
                          break;
                      case ReturnType.Send:
                          Control.ReturnKeyType = UIReturnKeyType.Send;
                          break;
                      case ReturnType.Search:
                          Control.ReturnKeyType = UIReturnKeyType.Search;
                          break;
                      case ReturnType.Done:
                          Control.ReturnKeyType = UIReturnKeyType.Done;
                          break;
                      default:
                          Control.ReturnKeyType = UIReturnKeyType.Default;
                          break;
                  }
              }
          }
      }

      3。创建 Android 自定义渲染器

      在 Android PCL 中,创建此自定义渲染器

      [assembly: ExportRenderer(typeof(EntryWithCustomKeyboardReturnButton), typeof(EntryWithCustomKeyboardReturnButtonCustomRenderer))]
      namespace Sample.Droid
      {
          public class EntryWithCustomKeyboardReturnButtonCustomRenderer : EntryRenderer
          {
              protected override void OnElementChanged(ElementChangedEventArgs e)
              {
                  base.OnElementChanged(e);
      
                  var customEntry = Element as EntryWithCustomKeyboardReturnButton;
      
                  if (Control != null && customEntry != null)
                  {
                      SetKeyboardButtonType(customEntry.ReturnType);
      
                      Control.EditorAction += (object sender, TextView.EditorActionEventArgs args) =>
                      {
                          if (customEntry?.ReturnType != ReturnType.Next)
                              customEntry?.Unfocus();
      
                          customEntry?.InvokeCompleted();
                      };
                  }
              }
      
              void SetKeyboardButtonType(ReturnType returnType)
              {
                  switch (returnType)
                  {
                      case ReturnType.Go:
                          Control.ImeOptions = ImeAction.Go;
                          Control.SetImeActionLabel("Go", ImeAction.Go);
                          break;
                      case ReturnType.Next:
                          Control.ImeOptions = ImeAction.Next;
                          Control.SetImeActionLabel("Next", ImeAction.Next);
                          break;
                      case ReturnType.Send:
                          Control.ImeOptions = ImeAction.Send;
                          Control.SetImeActionLabel("Send", ImeAction.Send);
                          break;
                      case ReturnType.Search:
                          Control.ImeOptions = ImeAction.Search;
                          Control.SetImeActionLabel("Search", ImeAction.Search);
                          break;
                      default:
                          Control.ImeOptions = ImeAction.Done;
                          Control.SetImeActionLabel("Done", ImeAction.Done);
                          break;
                  }
              }
          }
      }

      4。在 Xamarin.Forms PCL 中实现条目的 Completed 事件

      这是一个 link to some sample code,它展示了如何在 Xamarin.Forms PCL 中实现 Completed 事件。

      【讨论】:

        猜你喜欢
        • 2021-12-26
        • 2016-11-13
        • 1970-01-01
        • 1970-01-01
        • 2018-07-22
        • 2020-04-22
        • 2015-01-06
        • 1970-01-01
        • 2018-02-19
        相关资源
        最近更新 更多