正在处理Android自定义渲染器,但我不希望它是



我很难理解Android端的渲染器是如何工作的。

我试图在我的应用程序中实现一个MasterDetail页面。它应该在旧设备上工作,所以我在应用程序启动时创建我的页面,存储它们并将细节切换到这些页面,以获得更流畅的用户体验。

假设我在第一页上有一个自定义条目,它工作得很好,但是当我切换到page2然后回到page1时,我不能再使用我的渲染器中的Control变量了,因为它被处理了。嗯,当我在事件中使用它时,我不能,但它在OnElementChanged方法中像往常一样工作,该方法在页面出现时被调用。

有人能解释我如何仍然能够访问渲染器的控制变量时,我的事件被触发?

这是我的渲染器类
public class EntryIncroyableRenderer : EntryRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
if(e.NewElement != null)
{
Control.InputType = 0;
Control.EditorAction += (sender, args) =>
{
if ((args.ActionId == ImeAction.ImeNull && args.Event.Action == KeyEventActions.Down) || args.ActionId == ImeAction.Done)
{
(e.NewElement as EntryIncroyable)?.EnterPressed();
}
};
(e.NewElement as EntryIncroyable).OnFocused_TriggerRenderer += MyEntryRenderer_FocusControl;
(e.NewElement as EntryIncroyable).OnHideKeyBoard_TriggerRenderer += MyEntryRenderer_HideKeyboard;
}
}
void MyEntryRenderer_HideKeyboard()
{
HideKeyboard();
}
void MyEntryRenderer_FocusControl(object sender, GenericEventArgs<FocusArgs> args)
{
args.EventData.CouldFocusBeSet = Control.RequestFocus();
if (!((EntryIncroyable)Element).CanShowVirtualKeyboard)
{
HideKeyboard();
}
}
public void HideKeyboard()
{
Control.RequestFocus();
if (!((EntryIncroyable)Element).CanShowVirtualKeyboard)
{
Control.InputType = 0;
InputMethodManager imm = Control.Context.GetSystemService(Context.InputMethodService) as InputMethodManager;
imm.HideSoftInputFromWindow(Control.WindowToken, HideSoftInputFlags.None);
}
else
{
Control.InputType = Android.Text.InputTypes.ClassText;
InputMethodManager imm = Control.Context.GetSystemService(Context.InputMethodService) as InputMethodManager;
imm.ShowSoftInput(this, ShowFlags.Implicit);
}
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
}
} 

我找到问题所在了。我的事件是从处理渲染器中调用的,因为我没有取消订阅。我试图用(e.o oldelement != null)条件在OnElementChanged方法中做到这一点,但它从来都不是真的。所以我取消了dispose方法中的事件订阅。

protected override void Dispose(bool disposing)
{
(Element as EntryIncroyable).OnFocused_TriggerRenderer -= MyEntryRenderer_FocusControl;
(Element as EntryIncroyable).OnHideKeyBoard_TriggerRenderer -= MyEntryRenderer_HideKeyboard;
base.Dispose(disposing);
}

相关内容

最新更新