从接口抛出转换错误访问方法

时间:2012-07-06 22:38:31

标签: android xamarin.android

public class NewCallInfo
{
    public string Site { get; set; }
    public string CustomerName { get; set; }
    public string CustomerPhone { get; set; }
    public string CustomerExt { get; set; }
    public string CustomerAddress { get; set; }
    public string CustomerCity { get; set; }
    public string CustomerState { get; set; }
    public string CustomerZip { get; set; }
    public string ServiceType { get; set; }
    public string Priority { get; set; }
    public string Description { get; set; }
    public string Technician { get; set; }
    public string Serial { get; set; }
    public string Model { get; set; }
}

public interface INewCall
{
    NewCallInfo GetNewCallInfo(string site);
}

public class HandleNewCall : INewCall
{
   private SqLiteHelper Helper {get;set;}

   public HandleNewCall(Context context)
   {
      Helper = new SqLiteHelper(context);
   }
   public NewCallInfo GetNewCallInfo(string site)
   {
      string whereClause = string.Format("Where Site='{0}'", site);

        ICursor callCursor = _helper.ReadableDatabase.Query("NewCall", null, whereClause, null, null, null, null);
        NewCallInfo newCall = new NewCallInfo();

        while(callCursor.MoveToNext())
        {
            newCall.Site                =       callCursor.GetString(0);
            newCall.CustomerName        =       callCursor.GetString(1);
            newCall.CustomerPhone       =       callCursor.GetString(2);
            newCall.CustomerExt         =       callCursor.GetString(3);
            newCall.CustomerAddress     =       callCursor.GetString(4);
            newCall.CustomerCity        =       callCursor.GetString(5);
            newCall.CustomerState       =       callCursor.GetString(6);
            newCall.CustomerZip         =       callCursor.GetString(7);
            newCall.ServiceType         =       callCursor.GetString(8);
            newCall.Priority            =       callCursor.GetString(9);
            newCall.Description         =       callCursor.GetString(10);
            newCall.Technician          =       callCursor.GetString(11);
            newCall.Serial              =       callCursor.GetString(12);
            newCall.Model               =       callCursor.GetString(13);
        }

        if (string.IsNullOrEmpty(newCall.Site))
            newCall.Site = "None";

        return newCall;
   }
}

class MyApp : Application
{
    public INewCall NewCall { get; set; }

    public MyApp(IntPtr handle, JniHandleOwnership transfer)
        :base(handle, transfer)
    {

    }

    public override void OnCreate()
    {
       NewCall = new HandleNewCall(this);
    }
}

然后在我的活动中:

var call = ((MyApp) Application).NewCall.GetNewCallInfo("siteString");

我得到“无法从源类型转换为目标类型”。所有上面的接口和应用程序子类的东西对我来说都是非常新的,但我觉得我已经完成了与在线示例相同的所有方式。即在this example。几个小时以来我一直撞在墙上。有人看到我失踪了吗?

1 个答案:

答案 0 :(得分:1)

您错过了MyApp类的[Application]属性,无法正确生成AndroidManifest.xml,因此Android不知道将其用作您的应用程序类。因此,尝试将Application属性强制转换为MyApp失败,因为该对象实际上并不是您的类的实例。将您的类定义更新为:

[Application]
public class MyApp : Application
{
    // ...
}