我创建了一个新的简单的Dot Net项目,其中包含适用于Android的单独的UI和ViewModel项目。我使用了一个简单的Xamarin.Android EditText控件在Forms控件中进行呈现。在某些情况下,我将手动处理键盘。因此,我使用以下代码来处理键盘。但是上下文没有转换为活动。找不到指定的案例,发生了异常。
本地代码:
public class Native_View : EditText
{
public Native_View(Context context) : base(context)
{
OnLoading(context);
}
public Native_View(Context context, IAttributeSet attrs):base(context,attrs)
{
OnLoading(context);
}
public Native_View(Context context, IAttributeSet attrs, int defStyle):base(context,attrs,defStyle)
{
OnLoading(context);
}
private void OnLoading(Context con)
{
this.Text = "NativeControl";
}
protected override void OnFocusChanged(bool gainFocus, FocusSearchDirection direction, Rect previouslyFocusedRect)
{
base.OnFocusChanged(gainFocus, direction, previouslyFocusedRect);
Window window = ((Activity)this.Context).Window;
InputMethodManager mgr = (InputMethodManager)((Activity)this.Context).GetSystemService(Context.InputMethodService);
if (window != null)
{
window.SetSoftInputMode(SoftInput.StateHidden);
mgr.HideSoftInputFromWindow(this.WindowToken, 0);
}
}
}
形成PCL代码:
public class FormsView : View
{
public FormsView()
{
}
}
Forms Renderer代码:
public class SourceForms_Renderer : ViewRenderer<Source_PCL.FormsView, Native>
{
Native nativeView;
public SourceForms_Renderer()
{
}
protected override void OnElementChanged(ElementChangedEventArgs<Source_PCL.FormsView> e)
{
base.OnElementChanged(e);
if (e.NewElement != null)
{
nativeView = new Native(Forms.Context);
SetNativeControl(nativeView);
}
}
}
示例代码:
<ContentPage.Content>
<StackLayout>
<simpleFormsControl:FormsView />
</StackLayout>
</ContentPage.Content>
完整示例链接:https://www.c-sharpcorner.com/forums/uploadfile/392f47/07062018020515AM/SampleWithSource.zip
请帮助我明确使用键盘。
谢谢