获取回调参数值

时间:2016-11-02 02:56:26

标签: c# xamarin.android

我有一个非常简单的回调类,看起来像这个

#include <fstream>
#include <iostream>

const int MAX_ROOMS = 50;
const int MAX_EXITS = MAX_ROOMS * 4;
std::ifstream input;
int read_world(std::ifstream &input, std::string rooms[MAX_ROOMS], int &num_rooms,
               bool exits[MAX_EXITS], int &num_exits);
int read_rooms(std::ifstream &input, std::string rooms[MAX_ROOMS], int &num_rooms);
int read_exits(std::ifstream &input, bool exits[], int &num_exits);

int main() {
    std::string fileName;
    std::cout<<"filename::";
    std::getline(std::cin,fileName);
    input.open(fileName, std::ios::in);
    if(input) {
        std::string rooms[MAX_ROOMS];
        int num_rooms;
        bool exits[MAX_EXITS];
        int num_exits;
        read_world(input,rooms,num_rooms,exits,num_exits);
        input.close();
    } else {
        std::cout<<"Error: couldn't read from "<<fileName<<std::endl;
    }
    return 0;
};

int read_world(std::ifstream &input, std::string rooms[MAX_ROOMS], int &num_rooms,
               bool exits[MAX_EXITS], int &num_exits) {
    std::string fnCaller;
    while (std::getline(input, fnCaller, ' ')) { // to check which function to call
        std::cout<<"function::"<<fnCaller<<"\n";
        if (fnCaller == "rooms") {
            std::string temp;
            getline(input, temp);
            std::cout<<"num_rooms::"<<temp<<"\n";
            num_rooms = atoi(temp.c_str());
            read_rooms(input, rooms, num_rooms);
        }
        if (fnCaller == "exits") {
            std::string temp;
            std::getline(input, temp);
            std::cout<<"num_exits::"<<temp<<"\n";
            num_exits = atoi(temp.c_str());
            read_exits(input, exits, num_exits);
        }
    }
};

int read_rooms(std::ifstream &input, std::string rooms[MAX_ROOMS], int &num_rooms) {
    for (int i = 0; i < num_rooms; i++) {//get the info
        std::string str;
        std::getline(input, str);
        std::cout<<"room["<<i<<"]::"<<str<<"\n";
        rooms[i] = str;
    }
    return 0;
};

int read_exits(std::ifstream &input, bool exits[], int &num_exits) {
    for (int i = 0; i < num_exits; i++) {//get the info
        std::string str;
        std::getline(input, str);
        std::cout<<"exit["<<i<<"]::"<<str<<"\n";
        if (str == "locked") {
            exits[i] = true;
        } else if (str == "unlocked") {
            exits[i] = false;
        }
    }
    return 0;
};

如果我使用

之类的东西,回调可以正常工作
class ActivityCallbacks
{
    public class ActivityCallback : Java.Lang.Object, ICallbacks
    {
        Action<JSONObject> onSuccess;
        Action<JSONObject> onFail;

        public ActivityCallback(Action<JSONObject> success, Action<JSONObject> fail = null)
        {
            onFail = fail;
            onSuccess = success;
        }

        public void FailCallback(JSONObject p0)
        {
            onFail?.Invoke(p0);
        }
        public void SuccessCallback(JSONObject p0)
        {
            onSuccess?.Invoke(p0);
        }
    }
}

这很好,但有时我需要能够使用回调将要接收的值。

目前我正在使用此功能(这是不正确的)

someMethod(foo, new ActivityCallbacks.ActivityCallback(success=>...));#

通过生成错误的外观,预计最终成功括号中会出现分号。但是,这给了我一个分配错误。

我是否正确地接受这种方法来获取回调所期望的参数值?

1 个答案:

答案 0 :(得分:0)

您需要在success.GetString("id")中使用try,因为尝试是内部成功操作方法,r将传递给success,您只能访问sucesstry