C# 聚焦或聚焦更改事件不触发
我有自定义条目,我想知道它何时聚焦,但它从未发生过。我试图在自定义android类和xaml事件聚焦中捕获它,但它什么也没做。 我以前也有过这个问题,但没有解决它。 这是一节课。注释块中的事件C# 聚焦或聚焦更改事件不触发,c#,xaml,xamarin,xamarin.android,xamarin.forms,C#,Xaml,Xamarin,Xamarin.android,Xamarin.forms,我有自定义条目,我想知道它何时聚焦,但它从未发生过。我试图在自定义android类和xaml事件聚焦中捕获它,但它什么也没做。 我以前也有过这个问题,但没有解决它。 这是一节课。注释块中的事件 using Android.Support.Design.Widget; using Android.Text; using Android.Views; using Android.Views.InputMethods; using GitRemote.CustomClasses; using GitR
using Android.Support.Design.Widget;
using Android.Text;
using Android.Views;
using Android.Views.InputMethods;
using GitRemote.CustomClasses;
using GitRemote.Droid.DependencyServices;
using GitRemote.Droid.Renderers;
using GitRemote.ViewModels;
using System.ComponentModel;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using Color = Xamarin.Forms.Color;
using TextChangedEventArgs = Android.Text.TextChangedEventArgs;
using View = Android.Views.View;
[assembly: ExportRenderer(typeof(MaterialEntry), typeof(MaterialEntryRendererDroid))]
namespace GitRemote.Droid.Renderers
{
public class MaterialEntryRendererDroid : Xamarin.Forms.Platform.Android.AppCompat.ViewRenderer<Entry, View>
{
private TextInputLayout _nativeView;
private TextInputLayout NativeView => _nativeView ?? ( _nativeView = InitializeNativeView() );
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
if ( e.OldElement != null ) return;
// MaterialEntry Render Staff
#region
var ctrl = CreateNativeControl();
SetNativeControl(ctrl);
SetText();
SetHintText();
SetBackgroundColor();
SetTextColor();
SetIsPassword();
#endregion
if ( Control != null )
switch ( e.NewElement.ClassId )
{
case "LoginEntry":
ViewSaver.SaveLoginView(Control);
//Element.Focused += (sender, args) =>
//{
// ViewSaver.LastView = "LoginEntry";
//};
break;
case "PasswordEntry":
SetSendButtonAction();
ViewSaver.SavePasswordView(Control);
//Element.Focused += (sender, args) =>
//{
// ViewSaver.LastView = "PasswordEntry";
//};
break;
}
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if ( e.PropertyName == Entry.PlaceholderProperty.PropertyName )
{
SetHintText();
}
if ( e.PropertyName == Entry.TextColorProperty.PropertyName )
{
SetTextColor();
}
if ( e.PropertyName == VisualElement.BackgroundColorProperty.PropertyName )
{
SetBackgroundColor();
}
if ( e.PropertyName == Entry.IsPasswordProperty.PropertyName )
{
SetIsPassword();
}
if ( e.PropertyName == Entry.TextProperty.PropertyName )
{
SetText();
}
}
private void EditTextOnTextChanged(object sender, TextChangedEventArgs textChangedEventArgs)
{
Element.Text = textChangedEventArgs.Text.ToString();
NativeView.EditText.SetSelection(Element.Text.Length);
}
private void SetText()
{
NativeView.EditText.Text = Element.Text;
}
private void SetIsPassword()
{
NativeView.EditText.InputType = Element.IsPassword
? InputTypes.TextVariationPassword | InputTypes.ClassText
: InputTypes.TextVariationVisiblePassword;
}
public void SetBackgroundColor()
{
NativeView.SetBackgroundColor(Element.BackgroundColor.ToAndroid());
}
private void SetHintText()
{
NativeView.Hint = Element.Placeholder;
}
private void SetTextColor()
{
if ( Element.TextColor == Color.Default )
NativeView.EditText.SetTextColor(NativeView.EditText.TextColors);
else
NativeView.EditText.SetTextColor(Element.TextColor.ToAndroid());
}
private TextInputLayout InitializeNativeView()
{
var view = FindViewById<TextInputLayout>(Resource.Id.textInputLayout);
view.EditText.TextChanged += EditTextOnTextChanged;
return view;
}
protected override View CreateNativeControl()
{
return LayoutInflater.From(Context).Inflate(Resource.Layout.TextInputLayout, null);
}
/// <summary>
/// If Action of our entry is Send than call method from Portable
/// </summary>
private void SetSendButtonAction()
{
NativeView.EditText.EditorAction += (sender, e) =>
{
if ( e.ActionId == ImeAction.Send )
{
( ( LoginingPageViewModel )Element.BindingContext ).OnLogInTapped();
}
else
e.Handled = false;
};
}
}
}
使用Android.Support.Design.Widget;
使用Android.Text;
使用Android.Views;
使用Android.Views.InputMethods;
使用GitRemote.CustomClass;
使用GitRemote.Droid.DependencyServices;
使用gitreemote.Droid.renders;
使用gitreemote.ViewModels;
使用系统组件模型;
使用Xamarin.Forms;
使用Xamarin.Forms.Platform.Android;
使用Color=Xamarin.Forms.Color;
使用textchangedventargs=Android.Text.textchangedventargs;
使用View=Android.Views.View;
[程序集:导出渲染器(typeof(MaterialEntry)、typeof(materialEntryRenderDroid))]
命名空间gitmote.Droid.Renderers
{
公共类MaterialEntryRenderDroid:Xamarin.Forms.Platform.Android.AppCompat.ViewRenderer
{
私有文本输入布局_nativeView;
私有文本InputLayout NativeView=>_NativeView??(_NativeView=InitializenActiveView());
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
如果(e.OldElement!=null)返回;
//材质渲染杆
#区域
var ctrl=CreateNativeControl();
SetNativeControl(ctrl);
SetText();
SetHintText();
SetBackgroundColor();
SetTextColor();
SetIsPassword();
#端区
if(控件!=null)
开关(e.NewElement.ClassId)
{
案例“登录”:
ViewSaver.SaveLoginView(控件);
//Element.Focused+=(发送方,参数)=>
//{
//ViewSaver.LastView=“LoginEntry”;
//};
打破
大小写“PasswordEntry”:
SetSendButtonAction();
SavePasswordView(控件);
//Element.Focused+=(发送方,参数)=>
//{
//ViewSaver.LastView=“PasswordEntry”;
//};
打破
}
}
受保护的覆盖无效OnElementPropertyChanged(对象发送方,PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(发送方,e);
if(e.PropertyName==Entry.PlaceholderProperty.PropertyName)
{
SetHintText();
}
如果(e.PropertyName==Entry.TextColorProperty.PropertyName)
{
SetTextColor();
}
if(e.PropertyName==VisualElement.BackgroundColorProperty.PropertyName)
{
SetBackgroundColor();
}
if(e.PropertyName==Entry.IsPasswordProperty.PropertyName)
{
SetIsPassword();
}
if(e.PropertyName==Entry.TextProperty.PropertyName)
{
SetText();
}
}
私有void EditTextContextChanged(对象发送方,TextChangedEventArgs TextChangedEventArgs)
{
Element.Text=textchangedventargs.Text.ToString();
NativeView.EditText.SetSelection(Element.Text.Length);
}
私有void SetText()
{
NativeView.EditText.Text=元素.Text;
}
私有void SetIsPassword()
{
NativeView.EditText.InputType=Element.IsPassword
?InputTypes.TextVariationPassword | InputTypes.ClassText
:InputTypes.TextVariationVisiblePassword;
}
公共空间挫折背景色()
{
NativeView.SetBackgroundColor(Element.BackgroundColor.ToAndroid());
}
私有void SetHintText()
{
NativeView.Hint=Element.Placeholder;
}
私有void SetTextColor()
{
if(Element.TextColor==Color.Default)
NativeView.EditText.SetTextColor(NativeView.EditText.TextColor);
其他的
NativeView.EditText.SetTextColor(Element.TextColor.ToAndroid());
}
私有TextInputLayout初始化激活视图()
{
var view=findviewbyd(Resource.Id.textInputLayout);
view.EditText.TextChanged+=editTextContextChanged;
返回视图;
}
受保护的覆盖视图CreateNativeControl()
{
返回LayoutInflater.From(Context).Inflate(Resource.Layout.TextInputLayout,null);
}
///
///若我们输入的操作是Send,则从Portable调用方法
///
私有void SetSendButtonAction()
{
NativeView.EditText.EditorAction+=(发件人,e)=>
{
if(e.ActionId==ImeAction.Send)
{
((LoginingPageViewModel)Element.BindingContext).OnLogInTapped();
}
其他的
e、 已处理=错误;
};
}
}
}
我用过:
private void EditTextOnFocusChanged(object sender, FocusChangeEventArgs focusChangedEventArgs)
{
if ( focusChangedEventArgs.HasFocus )
ViewSaver.SetLastView(Element.ClassId);
}
view.EditText.FocusChange += EditTextOnFocusChanged;
它是有效的