Nadanie nazwy kontrolce.

0

Witam,
zaczęłam naukę C# z książki "Rusz głową". Przykład podany w książce nie działa, a ja po trzech dniach poszukiwań w google (i jednym dniu nauki c#) nie wiem jak naprawić błąd.
Poniżej kod:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
 
namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        Random random = new Random();
        public MainWindow()
        {
            InitializeComponent();
        }
 
        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);                  //55
            storyboard.Children.Add(animation);
            storyboard.Begin();
        }
    }
}

Visual Studio podpowiada:

Error CS0103 The name 'playArea' does not exist in the current context

Kontrolkę nazwałam w pliku XAML:
x:Name="playArea"

Proszę o pomoc, nie chcę się tak szybko zniechęcić.
Dziękuję :)

0

Dziękuję serdecznie :)
oto kod XAML:

<Page x:Name="Save_them_all_"
    x:Class="App1.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App1"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">
    <Page.Foreground>
        <SolidColorBrush Color="{ThemeResource SystemAltLowColor}"/>
    </Page.Foreground>
    <Page.BorderBrush>
        <SolidColorBrush Color="{ThemeResource SystemAltMediumColor}"/>
    </Page.BorderBrush>
    <Page.Background>
        <SolidColorBrush Color="{ThemeResource SystemAltLowColor}"/>
    </Page.Background>
    <Grid>
        <Grid.Background>
            <SolidColorBrush Color="{ThemeResource SystemBaseHighColor}"/>
        </Grid.Background>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="160"/>
            <ColumnDefinition Width="1*"/>
            <ColumnDefinition Width="160"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="140"/>
            <RowDefinition Height="160"/>
            <RowDefinition Height="60"/>
        </Grid.RowDefinitions>

        <Grid Grid.ColumnSpan="3">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
        </Grid>
        <Button x:Name="startButton"  Content="Start" Grid.Row="2" BorderThickness="0" RequestedTheme="Light" Foreground="White" BorderBrush="{x:Null}" Click="startButton_Click" Margin="0,0,0,16" VerticalAlignment="Bottom">
            <Button.Background>
                <SolidColorBrush Color="{ThemeResource SystemBaseMediumColor}"/>
            </Button.Background>
        </Button>
        <ProgressBar Grid.Column="1" Grid.Row="2" Foreground="#FFF23876" Height="20">
            <ProgressBar.Background>
                <SolidColorBrush Color="{ThemeResource SystemAltMediumColor}"/>
            </ProgressBar.Background>
        </ProgressBar>
        <StackPanel Grid.Column="2" Orientation="Vertical" Grid.Row="2">
            <TextBlock x:Name="Avoid_them_all_" Margin="0" TextWrapping="Wrap" FontSize="10" Style="{StaticResource SubheaderTextBlockStyle}" HorizontalAlignment="Center" VerticalAlignment="Center">
                <TextBlock.Foreground>
                    <SolidColorBrush Color="{ThemeResource SystemAltMediumColor}"/>
                </TextBlock.Foreground>
                <Run Text="Avoid them "/>
                <Run Text="a"/>
                <Run Text="l"/>
                <Run Text="l"/>
                <Run Text="!"/>
                <LineBreak/>
                <Run/>
            </TextBlock>
            <ContentControl Content="ContentControl" HorizontalAlignment="Center" VerticalAlignment="Center" Background="Black" Foreground="White">
                <ContentControl.Resources>
                    <ControlTemplate x:Key="EnemyTemplate" TargetType="ContentControl">
                        <Grid>
                            <Ellipse Fill="#FFF23876" Height="5" Width="5"/>
                        </Grid>
                    </ControlTemplate>
                </ContentControl.Resources>
                <ContentControl.Template>
                    <StaticResource ResourceKey="EnemyTemplate"/>
                </ContentControl.Template>
            </ContentControl>
        </StackPanel>
        <Canvas x:Name="playArea" Grid.Row="1" Margin="0" Grid.ColumnSpan="3" Background="White">
            <Ellipse x:Name="human" Width="10" Height="10">
                <Ellipse.Fill>
                    <SolidColorBrush Color="{ThemeResource SystemAltLowColor}"/>
                </Ellipse.Fill>
            </Ellipse>
            <Ellipse x:Name="target" Width="13" Margin="0" Height="13" Canvas.Left="10" Canvas.Top="4">
                <Ellipse.Stroke>
                    <SolidColorBrush Color="{ThemeResource SystemAltMediumColor}"/>
                </Ellipse.Stroke>
            </Ellipse>
            <TextBlock x:Name="gameOverText" TextWrapping="Wrap" Text="Game Over" Foreground="#33000000" FontSize="24" VerticalAlignment="Center" HorizontalAlignment="Center" Canvas.Left="261" Canvas.Top="10"/>
        </Canvas>

    </Grid>
</Page>

cały czas podkreśla frazę playArea, dodanie this. nie pomaga...

1

Kiedy masz kursor w XAML-u i wciśniesz F7 (albo klik prawym i pierwsza opcja od góry, po angielsku to jest "View Code"), to otwiera się ten powyższy kod w C#? Nie podoba mi się x:Class="App1.MainPage na samej górze, bo nie wskazuje na klasę, której kod podałaś. Zobacz, co będzie po zmianie tego na x:Class="WpfApplication1.MainWindow.

1

na 99% coś z tymi namespaceami w deklaracji Page. Tutaj na szybko poprawiony kod po sprawdzeniu w visualu:

 <Page x:Name="Save_them_all_"
    x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication1"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

Spróbuj tego zamiast tego co masz teraz i daj znać co i jak

0

zdesperowana utworzyłam nowy projekt, gdzie nie namieszałam w nazwach, niestety nadal występuje ten sam błąd, podam kod ponownie:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.ApplicationModel;
using Windows.ApplicationModel.Activation;
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;


namespace App3
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        Random random = new Random();
        public MainWindow()
        {
            InitializeComponent();
        }

        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);                  //55
            storyboard.Children.Add(animation);
            storyboard.Begin();
        }
    }
}

oraz

<Page
    x:Class="App3.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App3"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">
    <Page.Foreground>
        <SolidColorBrush Color="{ThemeResource SystemAltLowColor}"/>
    </Page.Foreground>
    <Page.BorderBrush>
        <SolidColorBrush Color="{ThemeResource SystemAltMediumColor}"/>
    </Page.BorderBrush>
    <Page.Background>
        <SolidColorBrush Color="{ThemeResource SystemAltLowColor}"/>
    </Page.Background>
    <Grid>
        <Grid.Background>
            <SolidColorBrush Color="{ThemeResource SystemBaseHighColor}"/>
        </Grid.Background>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="160"/>
            <ColumnDefinition Width="1*"/>
            <ColumnDefinition Width="160"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="140"/>
            <RowDefinition Height="160"/>
            <RowDefinition Height="60"/>
        </Grid.RowDefinitions>

        <Grid Grid.ColumnSpan="3">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
        </Grid>
        <Button x:Name="startButton"  Content="Start" Grid.Row="2" BorderThickness="0" RequestedTheme="Light" Foreground="White" BorderBrush="{x:Null}" Click="startButton_Click" Margin="0,0,0,16" VerticalAlignment="Bottom">
            <Button.Background>
                <SolidColorBrush Color="{ThemeResource SystemBaseMediumColor}"/>
            </Button.Background>
        </Button>
        <ProgressBar Grid.Column="1" Grid.Row="2" Foreground="#FFF23876" Height="20">
            <ProgressBar.Background>
                <SolidColorBrush Color="{ThemeResource SystemAltMediumColor}"/>
            </ProgressBar.Background>
        </ProgressBar>
        <StackPanel Grid.Column="2" Orientation="Vertical" Grid.Row="2">
            <TextBlock x:Name="Avoid_them_all_" Margin="0" TextWrapping="Wrap" FontSize="10" Style="{StaticResource SubheaderTextBlockStyle}" HorizontalAlignment="Center" VerticalAlignment="Center">
                <TextBlock.Foreground>
                    <SolidColorBrush Color="{ThemeResource SystemAltMediumColor}"/>
                </TextBlock.Foreground>
                <Run Text="Avoid them "/>
                <Run Text="a"/>
                <Run Text="l"/>
                <Run Text="l"/>
                <Run Text="!"/>
                <LineBreak/>
                <Run/>
            </TextBlock>
            <ContentControl Content="ContentControl" HorizontalAlignment="Center" VerticalAlignment="Center" Background="Black" Foreground="White">
                <ContentControl.Resources>
                    <ControlTemplate x:Key="EnemyTemplate" TargetType="ContentControl">
                        <Grid>
                            <Ellipse Fill="#FFF23876" Height="5" Width="5"/>
                        </Grid>
                    </ControlTemplate>
                </ContentControl.Resources>
                <ContentControl.Template>
                    <StaticResource ResourceKey="EnemyTemplate"/>
                </ContentControl.Template>
            </ContentControl>
        </StackPanel>
        <Canvas x:Name="playArea" Grid.Row="1" Margin="0" Grid.ColumnSpan="3" Background="White">
            <Ellipse x:Name="human" Width="10" Height="10">
                <Ellipse.Fill>
                    <SolidColorBrush Color="{ThemeResource SystemAltLowColor}"/>
                </Ellipse.Fill>
            </Ellipse>
            <Ellipse x:Name="target" Width="13" Margin="0" Height="13" Canvas.Left="10" Canvas.Top="4">
                <Ellipse.Stroke>
                    <SolidColorBrush Color="{ThemeResource SystemAltMediumColor}"/>
                </Ellipse.Stroke>
            </Ellipse>
            <TextBlock x:Name="gameOverText" TextWrapping="Wrap" Text="Game Over" Foreground="#33000000" FontSize="24" VerticalAlignment="Center" HorizontalAlignment="Center" Canvas.Left="261" Canvas.Top="10"/>
        </Canvas>

    </Grid>
</Page>

podam również link do PrtScr zaznaczonych przez VS błedów:
https://1drv.ms/i/s!AmtYxsShGfjphSLjsvfrIp6kydIC

bardzo dziękuję za ogromne chęci Z Waszej strony, przykro mi, że nie potrafię bardziej sprecyzować problemu (wynika to z mojej niewiedzy). Bardzo chcę się nauczyć programować... nie zdawałam sobie sprawy, że to takie czasochłonne... ale nie chcę się poddać:)

1

Hehe.. InitializeComponent() też podkreśla a tego już nie napisałaś. Czy Ty po prostu po utworzeniu nowego projektu (to bardzo ważne) kopiujesz i wklejasz cały ten kod który tutaj podałaś ? Dlaczego nie ma using System.Windows... tylko Windows... ?

Ok jeszcze raz. Jeżeli tworzysz solucję w visualu to tworzy się też projekt (ten zielony) i on ma jakąś swoją nazwę. Nie wiem jaką Ty mu nadałaś ale format mnie więcej będzie wyglądał tak :

  x:Class="NazwaTegoProjektu.NazwaTejKlasy"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:NazwaTegoProjektu"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

u mnie wygląda to tak :

http://ctrlv.in/782460

MainWindow.xaml to jest klasa tam gdzie jest wklejony ten cały kod z <Page ... />

W MainWindow jak sobie to rozwiniesz to będzie MainWindow.xaml.cs i tam ma być ten kod C# (zwróć uwagę jaką nazwę ma namespace) i postaraj się to napisać u siebie.

http://ctrlv.in/782465

0

serdecznie dziękuję,
wiem już, gdzie leżał mój błąd - nie tworzyłam WPF, tylko blank App - stąd zamieszanie, za które przepraszam. Niestety, książka, z której korzystam nie jest najlepszą możliwą pozycją dla początkujących dziewczyn :)

teraz borykam się z kolejnym problemem tego samego kodu:

Severity Code Description Project File Line Suppression State
Error CS1061 'MainWindow' does not contain a definition for 'startButton_Click' and no extension method 'startButton_Click' accepting a first argument of type 'MainWindow' could be found (are you missing a using directive or an assembly reference?) WpfApplication1 C:\Users\biuro\Desktop\VS\WpfApplication1\MainWindow.xaml 39 Active

Nie chcę potykać się co krok o błędy zamieszczone w książce. Nie chcę się tak szybko zniechęcać. Może mogę prosić o polecenie jakiegoś tytułu na miarę blondynki?

Jeszcze raz dziękuję :)

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