xamarin将数据加载到列表中的标签中

时间:2014-08-02 22:09:17

标签: c# .net mono xamarin

我有2个标签' firstName'和' lastName'如果我点击按钮' pushNext' 它将执行插入数据到列表

的插入功能
partial void pushNext_TouchUpInside (UIButton sender)
{
    Core.insert(firstName.Text,lastName.Text);
}
核心

中的

public class Core
{
    public static List<string> values = new List<string>();
    public Core ()
    {
    }
    public static void insert(string first,string last){
        Core.values.Add (first);
        Core.values.Add (last);

    }

单击按钮从我到下一个视图按

partial class nextView : UIViewController
{
    public nextView (IntPtr handle) : base (handle)
    {
        lastLabel.Text = Core.getdata ();
    }
}

在这个视图中,我试图从Core类中获取数据

public static string getdata(){

    return Core.values[0] ;


}

在这一点上,我可以看到Core.values [0]具有正确的值 但在这一点上我得到了一个错误 System.Reflection.TargetInvocationException:调用目标抛出了异常。 ---&GT; System.Exception:未将对象引用设置为对象的实例   在DsignerWalkthroug.ne​​xtView..ctor(IntPtr句柄)[0x00013] /Users/shaharnakash/Projects/DsignerWalkthroug/DsignerWalkthroug/nextView.cs:12   at at(wrapper managed-to-native)System.Reflection.MonoCMethod:InternalInvoke(System.Reflection.MonoCMethod,object,object [],System.Exception&amp;)   在SystemDeflection.MonoCMethod.InternalInvoke(System.Object obj,System.Object [] parameters)[0x00002] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:537   ---内部异常堆栈跟踪结束---   在SystemDeflection.MonoCMethod.InternalInvoke(System.Object obj,System.Object [] parameters)[0x00016] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:543   at System.Reflection.MonoCMethod.DoInvoke(System.Object obj,BindingFlags invokeAttr,System.Reflection.Binder binder,System.Object [] parameters,System.Globalization.CultureInfo culture)[0x00095] in / Developer / MonoTouch / Source / mono /mcs/class/corlib/System.Reflection/MonoMethod.cs:528   at System.Reflection.MonoCMethod.Invoke(BindingFlags invokeAttr,System.Reflection.Binder binder,System.Object [] parameters,System.Globalization.CultureInfo culture)[0x00000]在/ Developer / MonoTouch / Source / mono / mcs / class / corlib /的System.Reflection / MonoMethod.cs:556   在SystemDeflection.ConstructorInfo.Invoke(System.Object []参数)[0x00000] /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Reflection/ConstructorInfo.cs:62   在/Developer/MonoTouch/Source/monotouch/src/ObjCRuntime/Runtime.cs:427中的MonoTouch.ObjCRuntime.Runtime.ConstructNSObject [NSObject](IntPtr ptr,System.Type类型,MissingCtorResolution missingCtorResolution)[0x0003e]   在/Developer/MonoTouch/Source/monotouch/src/ObjCRuntime/Runtime.cs:408中的MonoTouch.ObjCRuntime.Runtime.ConstructNSObject(IntPtr ptr,IntPtr klass,MissingCtorResolution missingCtorResolution)[0x00013]   在/Developer/MonoTouch/Source/monotouch/src/ObjCRuntime/Runtime.cs:492中的MonoTouch.ObjCRuntime.Runtime.GetNSObject(IntPtr ptr,MissingCtorResolution missingCtorResolution)[0x00021]   在/Developer/MonoTouch/Source/monotouch/src/ObjCRuntime/DynamicRegistrar.cs中的MonoTouch.Registrar.DynamicRegistrar.GetMethodDescriptionAndObject(System.Type类型,IntPtr选择器,IntPtr obj,布尔throw_on_failure,System.IntPtr&amp; mthis)[0x0003e]: 587   在/Developer/MonoTouch/Source/monotouch/src/ObjCRuntime/Runtime.cs:242中的MonoTouch.ObjCRuntime.Runtime.GetMethodAndObject(IntPtr klass,IntPtr sel,IntPtr obj,Boolean throw_on_failure,System.IntPtr&amp; mthis)[0x00000]   at at(wrapper native-to-managed)MonoTouch.ObjCRuntime.Runtime:GetMethodAndObject(intptr,intptr,intptr,int,intptr&amp;)   at at(wrapper managed-to-native)MonoTouch.UIKit.UIApplication:UIApplicationMain(int,string [],intptr,intptr)   在/Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:38中的MonoTouch.UIKit.UIApplication.Main(System.String [] args,System.String principalClassName,System.String delegateClassName)[0x0004c]   在DsignerWalkthroug.Application.Main(System.String [] args)[0x00008] in /Users/shaharnakash/Projects/DsignerWalkthroug/DsignerWalkthroug/Main.cs:17

1 个答案:

答案 0 :(得分:3)

我的猜测是标签对象还没有被实例化 您应该在ViewDidLoad()中执行初始化。

有关详细信息,请参阅此处:http://developer.xamarin.com/guides/ios/application_fundamentals/ios_code_only/

编辑:

基本上,您的代码应该是这样的:

partial class nextView : UIViewController
{
        public nextView (IntPtr handle) : base (handle)
        {
        }

        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            //perform initialization here
            lastLabel.Text = Core.getdata ();
        }
}