Kod z książki "C# Rusz Głową" - program nie włącza się

0

Czesc. Jestem laikiem i kupiłe książke c# rusz głową . w pierwszym programie do napisania mam problem. Na początku aplikacja sie uruchamiała tylko po wcisnieciu start pojawial sie błąd. Potem próbowałem coś naprawić i teraz nie uruchamia się w ogóle . ktoś pomoże ?

using RatujZiemie.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.UI.Xaml.Media.Animation;


// The Basic Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234237

namespace RatujZiemie
{
    /// <summary>
    /// A basic page that provides characteristics common to most applications.
    /// </summary>
    public sealed partial class MainPage : Ratuj_ludzi.Common.LayoutAwarePage
    {
        Random random = new Random();
        private NavigationHelper navigationHelper;
        private ObservableDictionary defaultViewModel = new ObservableDictionary();

        /// <summary>
        /// This can be changed to a strongly typed view model.
        /// </summary>
        public ObservableDictionary DefaultViewModel
        {
            get { return this.defaultViewModel; }
        }

        /// <summary>
        /// NavigationHelper is used on each page to aid in navigation and 
        /// process lifetime management
        /// </summary>
        public NavigationHelper NavigationHelper
        {
            get { return this.navigationHelper; }
        }


        public MainPage()
        {
            this.InitializeComponent();
            this.navigationHelper = new NavigationHelper(this);
            this.navigationHelper.LoadState += navigationHelper_LoadState;
            this.navigationHelper.SaveState += navigationHelper_SaveState;
        }

        /// <summary>
        /// Populates the page with content passed during navigation. Any saved state is also
        /// provided when recreating a page from a prior session.
        /// </summary>
        /// <param name="sender">
        /// The source of the event; typically <see cref="Common.NavigationHelper"/>
        /// </param>
        /// <param name="e">Event data that provides both the navigation parameter passed to
        /// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested and
        /// a dictionary of state preserved by this page during an earlier
        /// session. The state will be null the first time a page is visited.</param>
        private void navigationHelper_LoadState(object sender, LoadStateEventArgs e)
        {
        }

        /// <summary>
        /// Preserves state associated with this page in case the application is suspended or the
        /// page is discarded from the navigation cache.  Values must conform to the serialization
        /// requirements of <see cref="Common.SuspensionManager.SessionState"/>.
        /// </summary>
        /// <param name="sender">The source of the event; typically <see cref="Common.NavigationHelper"/></param>
        /// <param name="e">Event data that provides an empty dictionary to be populated with
        /// serializable state.</param>
        private void navigationHelper_SaveState(object sender, SaveStateEventArgs e)
        {
        }

        #region NavigationHelper registration

        /// The methods provided in this section are simply used to allow
        /// NavigationHelper to respond to the page's navigation methods.
        /// 
        /// Page specific logic should be placed in event handlers for the  
        /// <see cref="Common.NavigationHelper.LoadState"/>
        /// and <see cref="Common.NavigationHelper.SaveState"/>.
        /// The navigation parameter is available in the LoadState method 
        /// in addition to page state preserved during an earlier session.

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            navigationHelper.OnNavigatedTo(e);
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            navigationHelper.OnNavigatedFrom(e);
        }

        #endregion

        private void startButton_Click(object sender, RoutedEventArgs e)
        {
            AddEnemy();
        }

        private void AddEnemy()
        {
            ContentControl enemy = new ContentControl();
            enemy.Template = Resources["EnemyTemplate"] as ControlTemplate;
            AnimateEnemy(enemy, 0, playArea.ActualWidth - 100, "(Canvas.Left)");
            AnimateEnemy(enemy, random.Next((int)playArea.ActualHeight - 100),
                random.Next((int)playArea.ActualHeight - 100), "(Canvas.Top)");
            playArea.Children.Add(enemy);
        }

        private void AnimateEnemy(ContentControl enemy, double from, double to, string propertyToAnimate)
        {
            Storyboard storyboard = new Storyboard() { AutoReverse = true, RepeatBehavior = RepeatBehavior.Forever };
            DoubleAnimation animation = new DoubleAnimation()
            {
                From = from,
                To = to,
                Duration = new Duration(TimeSpan.FromSeconds(random.Next(4, 6)))

            };
            Storyboard.SetTarget(animation, enemy);
            Storyboard.SetTargetProperty(animation, propertyToAnimate);
            storyboard.Children.Add(animation);
            storyboard.Begin();
        }
    }
}
1

Coś nie działa, coś zmieniłem i teraz...
To jest bardzo dobra okazja, żeby nauczyć się obsługi debuggera oraz przy okazji systemu kontroli wersji ;)

To jest Twoje pierwsze podejście do programowania?

0

Tak, zaczynam dopiero tą przygodę :)

0

Witam,
Także zaczynam naukę c# od tej książki i też mam kłopot w przykładzie podanym powyżej. Chodzi o using:
using Windows.UI.Xaml.Media.Animation;
Moje Visual Studio podkreśla mi w tym usingu UI na czerwono - no i kod nie działa... Jak rozwiązać ten problem?
W książce przykłady przerabiane są na win8 i Visual Studio 2012, a ja mam win 10 i Visual Studio Community 2015

0

A jaki komunikat błędu pokazuje oprócz podkreślenia na czerwono? Aplikacja powinna być typu "Universal Windows", bardzo wiele kodu z aplikacji Windows 8 Store jest bezpośrednio, bezproblemowo, przenośna do aplikacji "Universal Windows" w Windows 10.

0

Taki komunikat błędu pokazuje:
The type or namespace name 'UI' does not exist in the namespace 'Windows' (are you missing an assembly reference?)
Utworzyłem nowy projekt jako WPF Application

0

Tak u mnie wygląda references:

screenshot-20161228173845.png

0

Jeżeli utworzyłeś WPF Application, to ten kod nie zadziała, bo on jest dostosowany do Windows 8 Store App.

0

Ok, pominę w takim razie wszystko co dotyczy Windows 8 Store App i przejdę do c#. Chociaż widzę, że w dalszej części książki jest jeszcze kilka razy coś tworzone pod sklep windows 8. A może powinienem jednak zmienić książkę "C# Rusz głową" na inną...?

1

Nie jestem pewien, czy w tej książce to wszystko nie jest na przykładach aplikacji dla Windows 8. Może po prostu doinstaluj sobie do Visual Studio tworzenie aplikacji Windows 8? Ewentualnie pisz aplikacje "Universal Windows" dla Windows 10 (też ewentualnie trzeba doinstalować, ale mniej zajmuje miejsca ;-)), kod z Store App jest mniej więcej przenośny bezproblemowo.

0

Doinstalowałem tworzenie aplikacji Windows 8, przepisałem wszytko w Blank App (Universal Windows 8) i ruszyło :)
Dzięki za pomoc.

1 użytkowników online, w tym zalogowanych: 0, gości: 1