C#类间值传递方法咨询及Android双Activity传值故障排查
问题1:C#中如何将部分值从第一个类传递到第二个类?
嘿,在C#里实现类之间的值传递有几种实用方式,你可以根据场景选择:
构造函数传参(推荐给必须的参数)
给第二个类的构造函数定义需要接收的参数,创建实例时直接传入第一个类的对应值,能保证类实例初始化后就拥有必要的数据:// 第一个类 public class FirstClass { public int Score { get; set; } public string Username { get; set; } } // 第二个类 public class SecondClass { private readonly int _userScore; private readonly string _userName; // 通过构造函数接收参数 public SecondClass(int score, string username) { _userScore = score; _userName = username; } public void ShowInfo() { Console.WriteLine($"用户{_userName}的分数是{_userScore}"); } } // 使用示例 var first = new FirstClass { Score = 95, Username = "Alice" }; var second = new SecondClass(first.Score, first.Username); second.ShowInfo();属性/字段直接赋值(适合可选参数)
如果这些值不是类实例必须的,可以在创建第二个类的实例后,直接给它的属性或字段赋值:public class SecondClass { public int Score { get; set; } public string Username { get; set; } } // 使用示例 var first = new FirstClass { Score = 95, Username = "Alice" }; var second = new SecondClass(); second.Score = first.Score; second.Username = first.Username;通过方法传递(适合一次性操作)
如果只是需要把值传给第二个类的某个方法使用,可以直接在调用方法时传入参数:public class SecondClass { public void DisplayUserInfo(int score, string username) { Console.WriteLine($"用户{username}的分数是{score}"); } } // 使用示例 var first = new FirstClass { Score = 95, Username = "Alice" }; var second = new SecondClass(); second.DisplayUserInfo(first.Score, first.Username);
问题2:Android中如何将Activity的三个变量传递到另一个Activity?
看了你的代码,发现两个核心问题:一是你用SetContentView(Resource.Layout.page2)只是切换了当前Activity的布局,并没有真正启动第二个Activity;二是直接访问page1.postCount的方式不符合Android组件生命周期规则,很容易引发错误。下面是正确的实现方案:
步骤1:修改第一个Activity的跳转逻辑
首先修正异步方法的写法(async void除了事件处理场景尽量替换为async Task),然后在获取到用户信息后,用Intent传递数据并启动第二个Activity:
// 第一个Activity的代码修改 public long postCount; public long followerCount; public long followingCount; // 把async void改成async Task,确保异步逻辑能被正确等待 private async Task LoginToAcc() { user.UserName = txtUser.Text; user.Password = txtPwd.Text; api = InstaApiBuilder.CreateBuilder() .SetUser(user) .Build(); var loginReq = await api.LoginAsync(); if (loginReq.Succeeded) { // 等待GetUserInfo完成,确保数据已经赋值完成 await GetUserInfo(); // 启动第二个Activity,假设你的第二个Activity类名为SecondActivity var intent = new Intent(this, typeof(SecondActivity)); // 将三个变量存入Intent的Extra中 intent.PutExtra("PostCount", postCount); intent.PutExtra("FollowerCount", followerCount); intent.PutExtra("FollowingCount", followingCount); StartActivity(intent); } } // 同样改成async Task private async Task GetUserInfo() { var info = await api.GetUserInfoByIdAsync(pkid); if (info.Succeeded) { followerCount= info.Value.FollowerCount; followingCount = info.Value.FollowingCount; postCount = info.Value.MediaCount; } }
步骤2:在第二个Activity中接收数据
在第二个Activity的OnCreate方法里,从Intent中取出传递过来的值,注意要先设置布局:
// 第二个Activity的代码修改 protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); // 先设置页面布局,你之前的代码里遗漏了这一步 SetContentView(Resource.Layout.page2); txtPost = FindViewById<TextView>(Resource.Id.labelPosts); txtfollower = FindViewById<TextView>(Resource.Id.labelFollowers); txtfollowing = FindViewById<TextView>(Resource.Id.labelFollowings); // 从Intent中获取数据,第二个参数是默认值(如果没取到对应值就用0) long postCount = Intent.GetLongExtra("PostCount", 0); long followerCount = Intent.GetLongExtra("FollowerCount", 0); long followingCount = Intent.GetLongExtra("FollowingCount", 0); txtPost.Text += "\n"+ postCount.ToString(); txtfollower.Text += "\n" + followerCount.ToString(); txtfollowing.Text += "\n" + followingCount.ToString(); }
为什么之前的方法不可行?
- Android系统会管理Activity的生命周期,你无法直接访问另一个Activity的实例变量,因为该实例可能已经被系统销毁回收。
SetContentView只是替换当前Activity的布局内容,并没有创建新的Activity实例,你的"第二个Activity"实际上还是第一个Activity的上下文,这不是Android中页面跳转的正确方式。
内容的提问来源于stack exchange,提问作者user6845869
相关产品推荐
相关产品推荐

