如何从方法中发送和检索

How to send and retrieve from methods

我正在尝试发送和检索值,因为我的代码是面向对象的,在 Addition.xaml 我正在尝试发送 'AddLevel' 变量,它是一个 int,在 Addition.xaml 我有:

private void pageRoot_Loaded(object sender, RoutedEventArgs e)
{
   Methods.AddLevels(AddLevel, Question1,Question2, Answer);
   QuestionText1.Text = System.Convert.ToString(Question1);
   QuestionText2.Text = System.Convert.ToString(Question2);
}

在这个方法中,我试图将 AddLevel int 变量发送到方法 class 以确定要做什么,在方法 class 中,我有:

public static int AddLevels(int AddLevel, int Question1, int Question2, int Answer)
{
    Random rnd = new Random();

    if(AddLevel == 0 || AddLevel == 1)
    {
       Question1 = rnd.Next(0, 10);
       Question2 = rnd.Next(0, 10);
       Answer = Question1 + Question2;
    }

    return Question1;
    return Question2;
    return Answer;

}

总结一下,我试图将 Addition.xaml 中的 AddLevel 发送到方法 class 中的 AddLevels 方法,然后我试图从中检索问题 1、问题 2 和答案方法。我该怎么做呢?

将你的方法构建到 return 词典:

public static Dictionary<string, int> AddLevels(int AddLevel, int Question1, int Question2, int Answer)
{
    Random rnd = new Random();
    Dictionary<string, int> dic = new Dictionary<string,int>();

    if(AddLevel == 0 || AddLevel == 1)
    {
       Question1 = rnd.Next(0, 10);
       Question2 = rnd.Next(0, 10);
       Answer = Question1 + Question2;
    }

    dic.Add("Question1", Question1);
    dic.Add("Question2", Question2);
    dic.Add("Answer", Answer);

    return dic;

}

想取Question1,Question2,Answer的值时,这样写就可以了:

Dictionary<string, int> dic = AddLevels(AddLevel, Question1, Question2);
int question1 = dic["Question1"]; // this will return the value for Question1.

Dictionary 的要求之一是 Keys 是唯一的,你应该保证这一点。在您的情况下,键是:"Question1", "Question2", "Answer".

您应该以小写字母开头编写参数名称:addLevel, question1, question2

一种选择是使用 Tuple:

private void pageRoot_Loaded(object sender, RoutedEventArgs e)
{
    Tuple<int, int, int> result = Methods.AddLevels(AddLevel, Question1, Question2, Answer);
    QuestionText1.Text = System.Convert.ToString(result.Item1);
    QuestionText2.Text = System.Convert.ToString(result.Item2);
}

public static Tuple<int, int, int> AddLevels(int addLevel, int question1, int question2, int answer)
{
    if (addLevel == 0 || addLevel == 1)
    {
        Random rnd = new Random();
        question1 = rnd.Next(0, 10);
        question2 = rnd.Next(0, 10);
        answer = question1 + question2;
    }
    return Tuple.Create(question1, question2, answer);
}

另一个不太推荐的选项是使用 ref 关键字:

private void pageRoot_Loaded(object sender, RoutedEventArgs e)
{
    Methods.AddLevels(AddLevel, ref Question1, ref Question2, ref Answer);
    QuestionText1.Text = System.Convert.ToString(Question1);
    QuestionText2.Text = System.Convert.ToString(Question2);
}

public void AddLevels(int addLevel, ref int question1, ref int question2, ref int answer)
{
    if (addLevel == 0 || addLevel == 1)
    {
        Random rnd = new Random();
        question1 = rnd.Next(0, 10);
        question2 = rnd.Next(0, 10);
        answer = question1 + question2;
    }
}

我可能是错的,但你似乎过度复杂化了以避免束缚? AddLevels 似乎很容易成为 DataContext 的一部分?

SomePage.xaml

<TextBox x:Name="QuestionText1" Text={Binding Question1} />
<TextBox x:Name="QuestionText2" Text={Binding Question2} />

SomePage.xaml.cs

public partial class SomePage : UserControl
{
    public SomePage()
    {
        InitializeComponent();
        this.DataContext = new SomePageViewModel();
    }

    private void pageRoot_Loaded(object sender, RoutedEventArgs e)
    {
        ((SomePageViewModel)this.DataContext).AddLevels(AddLevel);
    }
}

SomePageViewModel.cs

public class SomePageViewModel : INotifyPropertyChanged
{
    public int Answer
    {
        get { return _answer; }
        set
        {
            if (_answer != value)
            {
                _answer = value;
                OnPropertyChanged("Answer");
            }
        }
    }

    public int Question1
    {
        get { return _question1; }
        set
        {
            if (_answer != value)
            {
                _question1 = value;
                OnPropertyChanged("Question1");
            }
        }
    }

    public int Question2
    {
        get { return _question2; }
        set
        {
            if (_question2 != value)
            {
                _question2 = value;
                OnPropertyChanged("Question2");
            }
        }
    }

    public void AddLevels(int addLevel)
    {
        Random rnd = new Random();

        if(addLevel == 0 || addLevel == 1)
        {
           Question1 = rnd.Next(0, 10);
           Question2 = rnd.Next(0, 10);
           Answer = Question1 + Question2;
        }
    }
}