如何从方法发送和检索

时间:2015-01-08 00:23:06

标签: c# wpf xaml

我正在尝试发送和检索值,因为我有面向对象的代码,在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变量发送到Methods类,以确定我在Methods类中有哪些操作:

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发送到Methods类中的AddLevels方法,然后我尝试从此方法中检索Question1,Question2和Answer。我该怎么做呢?

3 个答案:

答案 0 :(得分:2)

一种选择是使用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;
    }
}

答案 1 :(得分:0)

构建方法以返回Dictionary:

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;

}

如果你想取问题1,问题2,答案的值,只需这样写:

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

词典的一个要求是Keys是唯一的,你应该保证。在您的情况下,键是:"Question1", "Question2", "Answer"

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

答案 2 :(得分:0)

我可能是错的,但似乎你过度复杂化以避免绑定? 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;
        }
    }
}