locked
using textbox with button.

    Question

  • hi

    im lavanya, i involved in creating game app(in blank app)..In the first level ter is four picture , the user/player will enter the name  of the picture....Now if the player answered all the picture correctly then it shuld navigate to other level othrwise it should be in same page...plz help me with yur coding in c#and Xaml desig(visual studio 2013)....As soon as possible..

    Wednesday, March 26, 2014 4:31 PM

Answers

  • The title doesn't really reflect the question here. In any case I would suggest the following:

    • Decide a method for storing you levels; SQLite database (recommend) or XML files. You can then create a simple schema for your data. This could be a single table in a database or a single XML file. Based on what you have provided so far in terms of requirements I would recommend starting off with the following fields: Level (int), Name (string), ImgUrl (string).
    • Decide on where you want to host the images. If you have a lot of images that take up a lot of file space it might be best to store the images online. This will keep your app file size small which end users like. You can then store the link to the image in your level data.

    Once you have decided on these things you can then do a couple of different things in terms of creating the UI. If you are always going to have just 4 images then you can create 4 placeholders for the images and hard code things but that wouldn't that would limit you. Instead you can create an ItemControl and pass a view model to it that loads the data for you. Here is a quick test app I put together that does everything other than load in the data from a data source and move to the next level.

    Here is the XAML I added to MainPage.xaml

    <ItemsControl Name="GameBoard" ItemsSource="{Binding ImageInfo}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapGrid Orientation="Horizontal" />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
    
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <StackPanel Width="300" Height="350" Margin="30">
                    <Border BorderBrush="White" BorderThickness="3">
                        <Image Source="{Binding ImageUrl}" Width="300" Height="300" Stretch="Uniform"/>
                    </Border>
                            
                    <StackPanel Orientation="Horizontal" Margin="0,10,0,0">                        
                        <TextBlock Text="Guess:"/>
                        <TextBox Text="{Binding Guess, Mode=TwoWay}" Margin="10,0,0,0" Width="200"/>
                    </StackPanel>
                </StackPanel>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
            
    <Button Content="Check Answers" Tapped="CheckAnswers_Tapped"
            Margin="0,100,0,0" HorizontalAlignment="Center"/>

    Here is the code I used to power this.

    public sealed partial class MainPage : Page
    {
        private ImageInfoViewModel vm;
    
        public MainPage()
        {
            this.InitializeComponent();
    
            vm = new ImageInfoViewModel();
    
            vm.ImageInfo.Add(new ImageInfo()
            {
                Level = 1,
                Name = "Eiffel tower",
                ImageUrl = "http://www.styleofdesign.com/i/eiffel-tower-is-most-valuable-monument-in-europe-telegraph_pg-n-_0.jpg"
            });
    
            vm.ImageInfo.Add(new ImageInfo()
            {
                Level = 1,
                Name = "Buckingham Palace",
                ImageUrl = "http://www.london.citysam.de/fotos-london-i/westminster/buckingham-palace-2.jpg"
            });
    
            vm.ImageInfo.Add(new ImageInfo()
            {
                Level = 1,
                Name = "Niagra Falls",
                ImageUrl = "http://a.abcnews.com/images/Travel/gty_niagara_falls_dm_130225_ms.jpg"
            });
    
            vm.ImageInfo.Add(new ImageInfo()
            {
                Level = 1,
                Name = "Space Needle",
                ImageUrl = "http://chrisglass.com/travel/seattle2003/display/spaceneedle_05.jpg"
            });
    
            GameBoard.DataContext = vm;
        }
    
        private async void CheckAnswers_Tapped(object sender, TappedRoutedEventArgs e)
        {
            bool isComplete = true;
            foreach (var i in GameBoard.Items)
            {
                var item = (i as ImageInfo);
                isComplete &= item.IsCorrect;
            }
                
            if (isComplete)
            {
                await new MessageDialog("Congrats! Moving to next level.", "Congrats").ShowAsync();
    
                //Add code to read in data for next level.
                    
                //Update view model doing something like this:
                //Clear the data in the view model: vm.ImageInfo.Clear();
                //Loop through data for next level, create ImageInfo obects and add to vm.ImageInfo.
            }
            else
            {
                await new MessageDialog("Not all guesses are correct.", "Try Again").ShowAsync();
            }
        }        
    }
    
    public class ImageInfoViewModel
    {
        public ImageInfoViewModel()
        {
            ImageInfo = new ObservableCollection<ImageInfo>();
        }
    
        public ObservableCollection<ImageInfo> ImageInfo { get; set; }
    }
    
    public class ImageInfo
    {
        public int Level { get; set; }
        public string Name { get; set; }
        public string ImageUrl { get; set; }
    
        public string Guess { get; set; }
    
        public bool IsCorrect
        {
            get
            {
                return Name.Equals(Guess, StringComparison.OrdinalIgnoreCase);
            }
        }
    }


    http://rbrundritt.wordpress.com

    Wednesday, March 26, 2014 6:16 PM