【问题标题】:Completed Event on a custom Entry自定义条目上的已完成事件
【发布时间】:2018-05-23 13:27:17
【问题描述】:

我有两个条目,一个用于用户名,另一个用于密码。

<customEntry:EmailEntry Placeholder="Your email address" x:Name="Email" Keyboard="Email" WidthRequest="50" Text="{Binding UserName}"/>

<customEntry:PwdEntry Placeholder="Your password" x:Name="Password" IsPassword="true" Text="{Binding Password}"/>       

这两个条目(EmailEntry 和 PwdEntry)的类型是 ContentView 而不是 ContentPage。我正在尝试在 EmailEntry 上获取 Completed 事件,但不能。一旦用户点击键盘上的“下一步”按钮,焦点应该转移到 PwdEntry。

如果这些是正常条目,我知道我可以使用,

Email.Completed += (object sender, EventArgs e) => Password.Focus();

由于这两个条目是 ContentViews,所以一旦用户点击“下一步”,我就无法将焦点更改为下一个条目。

这是我的 CustomEntry...

<?xml version="1.0" encoding="UTF-8"?><ContentView xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="Ecommerce.Mobile.Core.EmailEntry"
         xmlns:local="clr-namespace:Ecommerce.Mobile.Core.CustomViews"
         xmlns:fr="clr-namespace:Ecommerce.Mobile.Core.Controls">

<ContentView.Content>
    <fr:MyFrame CornerRadius="5" 
           OutlineColor="{StaticResource MocoWhite}" 
           BackgroundColor="Blue" 
           HasShadow="false" Padding="15,0">    

        <Grid ColumnSpacing="16">
                <Grid.RowDefinitions>
                    <RowDefinition Height="50"/>
                    <RowDefinition Height="1"/>
                </Grid.RowDefinitions>

                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>

                <Grid Grid.Column="0" Padding="0,10,0,0" HeightRequest="30" WidthRequest="20">
                    <Image Source="icons_envelope_white1x.png" HorizontalOptions="Start" />
                </Grid>

                <Grid Grid.Column="1" HeightRequest="65" WidthRequest="20">
                    <Label x:Name="HiddenLabel" Font="ProximaNovaRegular" FontSize="12" IsVisible="False" Margin="0" FontAttributes="Bold"/>
                    <fr:MyKeyboardEntry x:Name="EntryField" FontSize="15" TextColor="White" Keyboard="Email" ReturnType="Next" Text="{Binding Text, Mode=TwoWay}" PlaceholderColor="White" Margin="0,12,0,0"/>
                </Grid>
         </Grid>
     </fr:MyFrame>  
</ContentView.Content>

我怎样才能做到这一点?

【问题讨论】:

  • @DiegoRafaelSouza,我现在添加了自定义条目...请看一下
  • 在您的 EmailEntry 和 MyKeyboardEntry 上的 TakeFocus 方法中公开 Completed 方法。你可以相应地使用两者。
  • 好的,继续。
  • @EvZ,感谢您的 cmets。你的建议让我知道应该做什么,但不清楚如何去做。您有我可以参考的链接或示例吗?谢谢

标签: xamarin xamarin.forms


【解决方案1】:

正如我所见,您在自己的类中包含了很多嵌套组件(在本例中为 ViewCell)。

我敢打赌,你最终会有一个基础组件,它提供你想在顶级组件中公开的事件。

我们以Completed 事件为例。假设你有下面的结构:

- EmailEntry _继承自_
   └ MyKeyboardEntry _继承自_
      └ ...
         └ Entry(提供 `Completed` 事件)

因此您可以在每个包装器上订阅和公开事件:

public class MyKeyboardEntry : View
{
    ...
    // Expose the event
    public event EventHandler<TextChangedEventArgs> TextChanged;

    ...
    // Subscribe the original one with your own handler
    public MyKeyboardEntry()
    {
        ...
        // Its your wrapped Entry object
        entry.TextChanged += OnTextChanged;
        ...
    }

    ...
    // Implementing the @EvZ suggestion
    public void TakeFocus()
    {
        entry.Focus();
    }
    ...
    // Finally, throw up your event notification 
    protected virtual void OnTextChanged(object sender, TextChangedEventArgs args)
    {
        ... // Handle something if you need

        TextChanged?.Invoke(this, args); // invoking the event this class owns using the C# 6 syntax

        /*
        // Using the C# <6
        var hdlr = TextChanged;

        if(hdlr != null) 
            hdlr.Invoke(this, args);
        */
    }

    ...
}

现在您可以在课堂外处理MyKeyboardEntry.TextChanged 事件了。

您必须对包装另一个更通用(EmailEntry 等)的每个特定类执行此操作。

希望对你有帮助。

【讨论】:

  • 感谢您发布此信息。你实际上让我很容易理解。我试图实施您的建议,但无法实现所需的行为。任何指向示例代码的链接都将非常有用。提前致谢
猜你喜欢
  • 2017-01-24
  • 1970-01-01
  • 1970-01-01
  • 2013-01-08
  • 2019-11-12
  • 1970-01-01
  • 2021-01-15
  • 2012-11-05
  • 2019-02-28
相关资源
最近更新 更多