簡體   English   中英

參數2:無法從字符串轉換為Int Visual Studio 2017 C#WPF應用

[英]Argument 2:cannot convert from string to int visual studio 2017 c# wpf app

我正在嘗試從一本書中學習C#編程,到目前為止,我被教導要輸入的代碼是:

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.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SaveTheHumans
{
/// <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, int v)
    {
        throw new NotImplementedException();
    }

    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, new PropertyPath(propertyToAnimate));
        storyboard.Children.Add(animation);
        storyboard.Begin();
    }
 }
}

它旨在成為一種游戲,並且代碼設置了一旦單擊“開始”按鈕,敵人就可以生成和移動的地方。 我嘗試搜索其他帖子以查找問題,但是沒有一個可以解決我的錯誤。 有問題的書對Head First C#有什么幫助,引起此錯誤的特定行是:

random.Next((int) playArea.ActualHeight - 100, "(Canvas.Top");

任何幫助將非常感激

Next方法需要兩個int作為參數,因此您需要將"(Canvas.Top"定義為int才能調用該方法,例如:

random.Next((int) playArea.ActualHeight - 100, (int) playArea.MaxHeight);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM