在Windows Phone 7.1上构建测验游戏的问题

时间:2014-03-20 18:55:08

标签: c# windows-phone-7

我在Windows Phone 7.1上忙于一个小项目。我正在尝试建立一个基于测验的游戏。我在Visual Studio 2010中使用C#。

我希望问题和答案以随机顺序出现。每个问题都会出现一个图像,并且在五个问题之后游戏结束。如果你犯了一个错误,那你就是失败者。

这是我到目前为止所得到的:

namespace PhoneApp3
{
    public partial class MainPage : PhoneApplicationPage
    {
        private List<Quiz> _Questions = null;
        private const int _QuestionCount = 7;
        private Quiz _CurrentQuestion = null;
        private int _CurrentQuestionIndex = 0;
        private int _Missed = 0;
        private DateTime _StartTime = DateTime.UtcNow;

        public MainPage()
        {
            InitializeComponent();

            _Questions = new List<Quiz>();

            _Questions.Add(new Quiz("What is the height of the Eiffel Tower?", "324 meters", "/PhoneApp3/Images/Eiffel_Tower.png", new[] { "176 meters", "257 meters" }));
            _Questions.Add(new Quiz("Where can you find the Big Ben?", "In London", "/PhoneApp3/Images/Big_Ben.png", new[] { "In Berlin", "In Copenhagen" }));
            _Questions.Add(new Quiz("Where can you find the Tower of Pisa?", "In Italy", "/PhoneApp3/Images/Tower_of_Pisa.png", new[] { "In Spain", "In Greece" }));
            _Questions.Add(new Quiz("What is the largest city of Europe?", "Istanbul", "/PhoneApp3/Images/City.png", new[] { "Moscow", "London" }));
            _Questions.Add(new Quiz("Which country is the host of the next UEFA European Championship (2016)?", "France", "/PhoneApp3/Images/UEFA.png", new[] { "Spain", "Germany" }));
            _Questions.Add(new Quiz("Who painted The Night Watch?", "Rembrandt van Rijn", "/PhoneApp3/Images/Night_Watch.png", new[] { "Vincent van Gogh", "Pablo Picasso" }));
            _Questions.Add(new Quiz("Which is the smallest country of Europe?", "Iceland", "/PhoneApp3/Images/Eiffel_Tower.png", new[] { "Greece", "The Netherlands" }));

            StartGame();
        }

        private void StartGame()
        {
            _CurrentQuestion = null;
            _CurrentQuestionIndex = 0;
            _Missed = 0;
            _StartTime = DateTime.UtcNow;

            _Questions = Shuffle(_Questions);
        }

        private List<T> Shuffle<T>(List<T> inputList)
        {
            List<T> randomList = new List<T>(inputList.Count);

            var random = new Random();
            int randomIndex = 0;
            while (inputList.Count > 0)
            {
                randomIndex = random.Next(0, inputList.Count);
                randomList.Add(inputList[randomIndex]);
                inputList.RemoveAt(randomIndex);
            }

            return randomList;
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            if (e.NavigationMode == System.Windows.Navigation.NavigationMode.Back)
            {
                StartGame();
            }
        }

        private void btnOption1_Click(object sender, RoutedEventArgs e)
        {
            CheckResult((sender as Button).Content);
        }

        private void btnOption2_Click(object sender, RoutedEventArgs e)
        {
            CheckResult((sender as Button).Content);
        }

        private void btnOption3_Click(object sender, RoutedEventArgs e)
        {
            CheckResult((sender as Button).Content);
        }

        private void CheckResult(object obj)
        {
            var choice = obj as string;

            if (!choice.Equals(_CurrentQuestion.Answer))
            {
                ++_Missed;
            }

            MoveToNext();
        }

        private void MoveToNext()
        {
            if (_CurrentQuestionIndex < 5)
            {
                // continue with next question
                _CurrentQuestion = _Questions[_CurrentQuestionIndex++];
                img.Source = new BitmapImage(new Uri(_CurrentQuestion.ImagePath, UriKind.RelativeOrAbsolute));

                var opties = Shuffle(new List<string>() { _CurrentQuestion.Options[0], _CurrentQuestion.Options[1], _CurrentQuestion.Answer });

                btnOption1.Content = opties[0];
                btnOption2.Content = opties[1];
                btnOption3.Content = opties[2];
            }
            else
            {
                // end of the game
                var time = DateTime.UtcNow.Subtract(_StartTime);

                if (_Missed > 0)
                {
                    NavigationService.Navigate(new Uri("/Result.xaml?win=false&time=" + time, UriKind.Relative));
                }
                else
                {
                    NavigationService.Navigate(new Uri("/Result.xaml?win=true&time=" + time, UriKind.Relative));
                }
            }
        }
    }


    public class Quiz
    {
        public string Question { get; set; }
        public string Answer { get; set; }
        public string ImagePath { get; set; }
        public string[] Options { get; set; }

        public Quiz(string question, string answer, string imagePath, string[] options)
        {
            Question = question;
            Answer = answer;
            ImagePath = imagePath;
            Options = options;
        }
    }
}

但是我认为它不会起作用。问题和选项没有出现,整个游戏都不起作用。我无法弄清楚我必须做什么,所以我很困惑。

修改 XAML

<phone:PhoneApplicationPage 
    x:Class="PhoneApp3.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    mc:Ignorable="d" d:DesignHeight="800" d:DesignWidth="480"
    shell:SystemTray.IsVisible="False">


    <Grid x:Name="LayoutRoot" Background="#FF008FFF">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <StackPanel x:Name="TitlePanel" Margin="12,17,12,28" Height="0"></StackPanel>

        <Grid x:Name="ContentPanel" Grid.RowSpan="2" HorizontalAlignment="Center" VerticalAlignment="Center" Height="Auto" Margin="0,36,12,0">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="227*" />
                <ColumnDefinition Width="229*" />
            </Grid.ColumnDefinitions>
            <Button Background="#FF3EA6FF" BorderBrush="Transparent" Grid.ColumnSpan="2" Height="72" HorizontalAlignment="Left" Margin="12,656,12,0" Name="btnOption3" VerticalAlignment="Top" Width="480" Click="btnOption3_Click" />
            <Button Background="#FF3EA6FF" BorderBrush="Transparent" Grid.ColumnSpan="2" Height="72" HorizontalAlignment="Left" Margin="12,500,12,0" Name="brnOption1" VerticalAlignment="Top" Width="480" Click="btnOption1_Click" />
            <Button Background="#FF3EA6FF" BorderBrush="Transparent" Grid.ColumnSpan="2" Height="72" HorizontalAlignment="Left" Margin="12,578,12,0" Name="btnOption2" VerticalAlignment="Top" Width="480" Click="btnOption2_Click" />
            <Image Height="276" HorizontalAlignment="Center" Margin="96,218,96,0" Name="img" Stretch="Fill" VerticalAlignment="Top" Width="276" Grid.ColumnSpan="2" />
        </Grid>
    </Grid>

问题:我的屏幕上没有任何数据,按钮中没有文字,例如

0 个答案:

没有答案