我正在将原生Android应用移植到Xamarin + MVVMCross。在我的原生Android应用程序上,我可以轻松地创建自定义DateDisplayPicker并使用它。我在Xamarin遇到麻烦。我的控制住在这里:
我的DateDisplayPicker.cs看起来像这样:
namespace XxxxXxxx.Droid.CustomViews
{
public class DateDisplayPicker : TextView, DatePickerDialog.IOnDateSetListener
{
private readonly Context context;
private readonly Calendar calendar;
protected DateDisplayPicker(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
{
}
public DateDisplayPicker(Context context) : base(context)
{
}
public DateDisplayPicker(Context context, IAttributeSet attrs) : base(context, attrs)
{
this.context = context;
SetAttributes();
this.calendar = Calendar.Instance;
}
public DateDisplayPicker(Context context, IAttributeSet attrs, int defStyle) : base(context, attrs, defStyle)
{
this.context = context;
calendar = Calendar.Instance;
}
private void SetAttributes()
{
Click += (sender, args) => ShowDateDialog();
}
private void ShowDateDialog()
{
var c = Calendar.Instance;
var dp = new DatePickerDialog(context, this, c.Get(CalendarField.Year),
c.Get(CalendarField.Month), c.Get(CalendarField.DayOfMonth));
dp.Show();
}
public void OnDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
{
Text = String.Format("%s/%s/%s", monthOfYear + 1, dayOfMonth, year);
calendar.Set(year, monthOfYear, dayOfMonth);
}
public void AddDay()
{
calendar.Add(CalendarField.Date, 1);
Text = String.Format("%s/%s/%s", calendar.Get(CalendarField.Month) + 1,
calendar.Get(CalendarField.DayOfMonth), calendar.Get(CalendarField.Year));
}
public void SubtractDay()
{
calendar.Add(CalendarField.Date, -1);
Text = String.Format("%s/%s/%s", calendar.Get(CalendarField.Month) + 1,
calendar.Get(CalendarField.DayOfMonth), calendar.Get(CalendarField.Year));
}
public string GetDate()
{
var sdf = new SimpleDateFormat("yyyyMMdd");
return sdf.Format(calendar.Time);
}
}
}
他们在我的Android片段中访问它是这样的:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true">
<com.workspace.xxxxxxxx.droid.customviews.DateDisplayPicker
android:id="@+id/managerDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:layout_centerHorizontal="true"
android:text="XX-XX-XXXX"
android:textSize="16sp"
android:textColor="@android:color/holo_blue_dark"
style="@android:style/Widget.Holo.Spinner"
android:hint="Date" />
<ImageButton
android:id="@+id/managerDateMinusButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/managerDate"
android:layout_marginRight="@dimen/button_padding"
android:src="@drawable/minus"
android:background="@android:color/transparent" />
<ImageButton
android:id="@+id/managerDatePlusButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/managerDate"
android:layout_marginLeft="@dimen/button_padding"
android:src="@drawable/plus"
android:background="@android:color/transparent" />
</RelativeLayout>
<ExpandableListView
android:id="@+id/managerExpandableListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"
android:stackFromBottom="false"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginBottom="15dp" />
</LinearLayout>
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="invisible" />
<TextView
android:id="@+id/assignedMessageText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center"
android:text="No tasks today."
android:visibility="visible"
android:layout_below="@id/progressBar1" />
</RelativeLayout>
从我所见过的几个例子来看,这几乎就是他们如何设置但我必须遗漏一些东西。有任何想法吗?我已经确认我的axml控件的命名空间全是小写的。当我的应用程序错误时,我得到:
Pending exception is:
android.view.InflateException: Binary XML file line #1: Error inflating class com.workspace.xxxxxxxx.droid.customviews.DateDisplayPicker
(raw stack trace not found)
Caused by:
java.lang.ClassNotFoundException: Didn't find class "com.workspace.xxxxxxxx.droid.customviews.DateDisplayPicker" on path: DexPathList[[zip file "/data/app/com.workspace.xxxxxxxx-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.workspace.xxxxxxxx-1, /vendor/lib, /system/lib]]
答案 0 :(得分:1)
别介意我只是愚蠢。如果我放弃&#34; com。&#34;这样做似乎这里学到的教训似乎是使用完全控制类上的命名空间,在这种情况下它是&#34; xxxxxxxx.droid.customviews.DateDisplayPicker&#34;在axml。希望这可以帮助其他任何与此相关的人!