【问题标题】:Argument 2:cannot convert from string to int visual studio 2017 c# wpf app参数 2:无法从字符串转换为 int visual studio 2017 c# wpf app
【发布时间】:2017-08-21 15:21:37
【问题描述】:

我正在尝试从一本书中学习 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");

任何帮助将不胜感激

【问题讨论】:

    标签: c# .net wpf windows


    【解决方案1】:

    Next 方法需要两个 int 作为 parameters 所以你需要将 "(Canvas.Top" 定义为 int 来调用这样的方法:

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

    【讨论】:

    • 现在我从这一行单击开始按钮时出现错误: }
    • 提醒一下,为您遇到的每个问题创建一个新问题,并接受解决第一个问题的答案。快速回答:你得到错误的那一行会抛出一个异常,因为方法没有实现。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多