如何在C#中从第一类向第二类发送一些值?

时间:2019-07-09 11:50:24

标签: c# xamarin

我有一个具有2个活动的android应用。 我试图将三个变量的值发送到另一个活动,但这在我的代码中不起作用:

CLASS 1

        <div class="col-sm-6">
                        <a href="#abc" class="btn btn-success" data-toggle="modal">
                            <i class="material-icons">&#xE147;</i>
                            <span>Add new course</span>
                        </a>
                    </div>

<p style="height:1000px">space</p>

        <div id="abc" class="modal fade">
          <div class="modal-dialog">
            <div class="modal-content">
                <form action="">
                    <div class="modal-header">
                        <h4 class="modal-title">
                            Add Course
                        </h4>
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                            &times;
                        </button>
                    </div>
                    <div class="modal-body">
                        <div class="form-group">
                            <label>Subject</label>
                            <input type="text" class="form-control typeahead" required>
                        </div>
                        <div class="form-group">
                            <label>Time start</label>
                            <div class="input-group date" id="datepicker">
                                <input type="text" class="form-control">
                                <span class="input-group-addon">
                                    <span class="glyphicon glyphicon-time"></span>
                                </span>
                            </div>
                        </div>
                    </div>
                    <div class="modal-footer">
                            <input type="button" class="btn btn-default" data-dismiss="modal" value="Cancel">
                            <input type="submit" class="btn btn-success" value="Add">
                    </div>
                </form>
            </div>
        </div>
    </div>

<p style="height:1000px">space</p>

如何将followeCount,postCount,followingCount的值发送给第二类? 您可以在此处看到第二项活动:

CLASS 2

public long postCount;
    public long followerCount;
    public long followingCount;

    private async void LoginToAcc()
    {
        user.UserName = txtUser.Text;
        user.Password = txtPwd.Text;
        api = InstaApiBuilder.CreateBuilder()
            .SetUser(user)
            .Build();

        var loginReq = await api.LoginAsync();
        if (loginReq.Succeeded)
        {
            GetUserInfo();
            SetContentView(Resource.Layout.page2);
        }
    }
    private async void GetUserInfo()
    {
        var info = await api.GetUserInfoByIdAsync(pkid);
        if (info.Succeeded)
        {
           followerCount= info.Value.FollowerCount;
            followingCount = info.Value.FollowingCount;
            postCount = info.Value.MediaCount;

2 个答案:

答案 0 :(得分:0)

1-创建一个class以用户名

class User
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Family { get; set; }
        public int Age { get; set; }
    }

2-为VM_User的名称创建一个class

class VM_User
    {
        public string Name { get; set; }
        public string Family { get; set; }
    }

3-从用户创建一个对象并设置您的值

User user = new User() { Id = 1, Name = "Diako", Family = "Hasani", Age = 20 };

4-然后从VM_User创建对象并设置此值

VM_User vm_user = new VM_User() { Name = user.Name, Family = user.Family };

5-此展示价值代码

Console.WriteLine(vm_user.Name + "   " + vm_user.Family);

答案 1 :(得分:0)

尝试使用 Interface 从其他类或Task方法中获取价值。

创建一个TodoItem类,其中包含要传递的参数:

public class TodoItem
{
    public long postCount { set; get; }
    public long followerCount { set; get; }
    public long followingCount { set; get; }

}

创建一个 PassValueInterface (接口)以添加方法:

public interface PassValueInterface
{
    Task<TodoItem> PassTodoValue();
}

ClassOne 中,从任务(如http请求)获取TodoItem

public class ClassOne : PassValueInterface
{
    public async Task<TodoItem> PassTodoValue()
    {

        TodoItem todoItem = new TodoItem();
        todoItem.followerCount = 0;
        todoItem.followingCount = 0;
        todoItem.postCount = 0;

        // Here can be http request code to set value

        return todoItem;
    }


}

最后,在其他类 ContentPage 类中,可以获得TodoItem

public MainPage()
{
    InitializeComponent();
    //-------Get TodoItem here-------------
    ClassOne classOne = new ClassOne();
    Console.WriteLine("----"+ GetTasksAsync(classOne).Result.followerCount);
    Console.WriteLine("----"+ GetTasksAsync(classOne).Result.followingCount); 
    Console.WriteLine("----"+ GetTasksAsync(classOne).Result.postCount);  
}
// from this method in other class
public Task<TodoItem> GetTasksAsync(ClassOne classOne)
{
     return classOne.PassTodoValue();
}

如果还有其他问题,请参考Consume a RESTful Web Service