Rav*_*ven 1 c# xaml xamarin xamarin.forms
我基本上是在做聊天功能,这就是XAML代码片段的样子:
<StackLayout Grid.Row="1" Orientation="Horizontal" HeightRequest="50">
<Entry x:Name="ChatField" Text="{Binding CurrentMessageText}" Placeholder="{Binding MessageTextPlaceHolder}" HorizontalOptions="FillAndExpand"/>
<Button Text="send" BackgroundColor="Transparent" Command="{Binding SendMessageCommand}"/>
</StackLayout>
Run Code Online (Sandbox Code Playgroud)
我有一个Entry控件和一个按钮控件,该控件绑定到在chatfield中发送文本的命令。
而且我想模仿聊天应用程序标准,其中按下发送按钮不会使焦点不集中或隐藏键盘。
不幸的是,现在当我按下发送按钮时-它会自动使键盘从输入单元格失去焦点。
我为防止这种情况而采取的初始步骤是在ViewModel SendMessageCommand上:
var chatEntry = CurrentPage.FindByName<Entry>("ChatField");
chatEntry.Focus();
Run Code Online (Sandbox Code Playgroud)
但这使向上推所有列表视图变得很奇怪。
已经在IOS上使用自定义渲染器解决了此问题:
public class ChatEntryViewRenderer : EntryRenderer
{
protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (e.PropertyName == VisualElement.IsFocusedProperty.PropertyName)
{
if (Control != null)
{
var chatEntryView = this.Element as ChatEntryView;
Control.ShouldEndEditing = (UITextField textField) =>
{
return !chatEntryView.KeepOpen; // KeepOpen is a custom property I added and is set on my ViewModel if I want the Entry Keyboard to be kept open then I just set this to true.
};
}
}
base.OnElementPropertyChanged(sender, e);
}
}
Run Code Online (Sandbox Code Playgroud)
它的作用是,它基本上使用Control的ShouldEndEditing事件,该事件在EditText失去焦点或它的键盘即将失去焦点时触发。
| 归档时间: |
|
| 查看次数: |
4944 次 |
| 最近记录: |