2012-07-06 45 views
0
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

您错过了MyApp类的[Application]属性以正确生成AndroidManifest.xml,因此Android不知道将它用作您的应用程序类。因此,试图将Application属性转换为MyApp失败,因为该对象实际上并不是您类的实例。更新您的班级定义,如下所示:

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

就是这样。谢谢! – jmease 2012-07-09 12:56:31