Я создал новый простой проект Dot Net с отдельным пользовательским интерфейсом и проектом ViewModel для Android. Я использовал простой элемент управления EditText Xamarin.Android для визуализации в элементе управления 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()
{
}
}
Код для визуализации форм:
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
Пожалуйста, помогите мне явно использовать клавиатуру.
Спасибо