如何在调用时只运行方法?

时间:2011-08-23 06:37:18

标签: c# silverlight xaml methods

下面有一堆代码。但是,我遇到了一些错误,因为Move()和Genius()方法运行逻辑太多了。如果通过提交点击方法调用它们,我只想要运行两个方法。我怎么能这样做?

namespace ShotgunApp
{
    public partial class SingleGame : PhoneApplicationPage
    {
        public static class AmmoCount
        {
            public static int userAmmo = startVars.startAmmo;
            public static int geniusAmmo = startVars.startAmmo;
        }

        public static class Global
        {
            public static int lives = 1;
            public static string GeniusMove;
            public static string UserMove;
        }

        public SingleGame()
        {
            InitializeComponent();
            GeniusAmmo.Text = "ammo: " + AmmoCount.geniusAmmo;
            UserAmmo.Text = "ammo: " + AmmoCount.userAmmo;   
        }

        private void submit_Click(object sender, RoutedEventArgs e)
        {
            if (((String)submit.Content) == "Submit")
            {
                Move();
                submit.Content = "Wait for Genius...";
                uReload.IsEnabled = false;
                uFire.IsEnabled = false;
                uShield.IsEnabled = false;
                Genius();
            }
            else if (((String)submit.Content) == "Go!")
            {
                GeniusSpeak.Text = "";
                OutcomeDesc.Text = "You have " + Move() + " and Genius has " + Genius();
                Outcome.Text = "ANOTHER ROUND...";
                submit.Content = "Continue";
            }
            else if (((String)submit.Content) == "Continue")
            {

                uReload.IsEnabled = true;
                uFire.IsEnabled = true;
                uShield.IsEnabled = true;
                OutcomeDesc.Text = "";
                Outcome.Text = "";
                submit.Content = "Submit";
            }
        }


        public string Move()
        { 
            if (uReload.IsChecked.HasValue && uReload.IsChecked.Value == true)
            {
                UserAmmo.Text = "ammo: " + ++AmmoCount.userAmmo;
                Global.UserMove = "reloaded";

            }
            else if (uShield.IsChecked.HasValue && uShield.IsChecked.Value == true)
            {
                Global.UserMove = "shielded";
            }
            else if (uFire.IsChecked.HasValue && uFire.IsChecked.Value == true)
            {
                UserAmmo.Text = "ammo: " + --AmmoCount.userAmmo;
                Global.UserMove = "fired";

            }
            else
            {
                submit.Content = "Enter a move!";
            }

            return Global.UserMove;
        }

        public string Genius()
        {
            GeniusSpeak.Text = "Genius has moved";
            submit.Content = "Go!";

            Random RandomNumber = new Random();
            int x = RandomNumber.Next(0, 3);

            if (x == 0)
            {
                Global.GeniusMove = "reloaded";
                GeniusAmmo.Text = "ammo: " + ++AmmoCount.geniusAmmo;
            }
            else if (x == 1)
            {
                Global.GeniusMove = "shielded";
            }
            else if (x == 2)
            {
                Global.GeniusMove = "fired";
                GeniusAmmo.Text = "ammo: " + ++AmmoCount.geniusAmmo;
            }

            return Global.GeniusMove;

        }
    }
}

1 个答案:

答案 0 :(得分:4)

将最后的结果存储在数据成员中:

        private string lastMoveResult = string.Empty;
        private string lastGeniusResult = string.Empty;

        private void submit_Click(object sender, RoutedEventArgs e)
        {
            if (((String)submit.Content) == "Submit")
            {
                lastMoveResult = Move();
                submit.Content = "Wait for Genius...";
                uReload.IsEnabled = false;
                uFire.IsEnabled = false;
                uShield.IsEnabled = false;
                lastGeniusResult = Genius();
            }
            else if (((String)submit.Content) == "Go!")
            {
                GeniusSpeak.Text = "";
                OutcomeDesc.Text = "You have " + lastMoveResult + " and Genius has " + lastGeniusResult ;
                Outcome.Text = "ANOTHER ROUND...";
                submit.Content = "Continue";
            }