【问题标题】:Auto-Calculating the Age C# [duplicate]自动计算年龄 C# [重复]
【发布时间】:2016-03-19 12:26:48
【问题描述】:

参考我最后一个问题。我需要我的 WPF 应用程序根据发布日期和当前日期自动计算电影的年龄。这是我个人没有经验的一个领域。我认为这与 DateTime 和 CurrentDate 有关。有什么建议吗?

我的 WPF 有一个 Movie 类,其中包含:

namespace FinalExam
{
    public class Movie
    {
        public string movieName { get; set; }

        public DateTime releaseDate { get; set; }

        public DateTime movieAge { get; set; }

        public string onDVD { get; set; }

        public string onBluRay { get; set; }

        public string genreType { get; set; }

        public string directorName { get; set; }

        public string producerName { get; set; }

        public int movieLength { get; set; }

        public string moveRating { get; set; }        
    }
}

MovieUtility 类,其中包含:

namespace FinalExam
{
    public static class MovieUtility
    {
        public static bool isItDate(string dateString)
        {
            DateTime date;
            bool isDate;
            isDate = DateTime.TryParse(dateString, out date);
            return isDate;
        }
        public static DateTime ConvertStringToDate(string dateString)
        {
            DateTime date;
            DateTime.TryParse(dateString, out date);
            return date;
        }
    }
}

MainWindow.xaml 是用户输入数据的地方,数据网格将显示它,最后是 MainWindow.xaml.cs,它是调用所有字符串的地方,数据将被添加到列表。

namespace FinalExam
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        //GLOBAL VARIABLE AREA
        List<Movie> movieList = new List<Movie>();

        public MainWindow()
        {
            InitializeComponent();
        }

        private void but_Submit_Click(object sender, RoutedEventArgs e)
        {
            bool isGoodToAddNewMovie = true;
            Movie newMovie = new Movie();
            newMovie.movieName = txtBox_MovieName.Text;
            newMovie.onDVD = txtBox_DVD.Text;
            newMovie.onBluRay = txtBox_BluRay.Text;
            newMovie.genreType = txtBox_Genre.Text;
            newMovie.directorName = txtBox_Director.Text;
            newMovie.producerName = txtBox_Producer.Text;
            newMovie.moveRating = txtBox_Rating.Text;

            if (MovieUtility.isItDate(txtBox_ReleaseDate.Text))
            {
                newMovie.releaseDate = MovieUtility.ConvertStringToDate(txtBox_ReleaseDate.Text);
                txtBox_ReleaseDate.Background = new SolidColorBrush(Colors.LightGray);
            }
            else
            {
                txtBox_ReleaseDate.Background = new SolidColorBrush(Colors.Red);
                isGoodToAddNewMovie = false;
            }

            int length;

            if (int.TryParse(txtBox_Length.Text, out length))
            {
                newMovie.movieLength = length;
                txtBox_Length.Background = new SolidColorBrush(Colors.LightGray);
            }
            else
            {
                txtBox_Length.Background = new SolidColorBrush(Colors.Red);
                MessageBox.Show("Please enter movie length in minutes.");
                isGoodToAddNewMovie = false;
            }

            //ADD PERSON TO LIST
            if (isGoodToAddNewMovie)
            {
                movieList.Add(newMovie);
                dataGrid_Movies.ItemsSource = new List<Movie>(movieList);
            }
        }
    }
}

那么我将如何根据发行日期自动计算电影的年龄,并让它自动将电影的年龄放入窗口上的 DataGrid 中。

【问题讨论】:

  • 你做过研究吗?不难;尝试搜索“C# 日期差异”或类似内容。
  • 这看起来很像期末考试的问题......
  • 我尝试搜索根据当前日期自动计算日期,但找不到任何关于我的困境的确切信息。我一直在寻找过去的 45 分钟。我对 C# 非常陌生,对 DateTime 计算更是如此。
  • 格雷格,我们的教授甚至告诉我们谷歌它。另一个学生问了如何使用自动计算的问题。所以我来到了这个我已经学到了很多东西的很棒的网站。
  • 就像我们的系统管理员曾经在我们有问题时给我们发电子邮件一样 - "read the manual"

标签: c# wpf xaml datagrid


【解决方案1】:

利用属性:

    private DateTime  _ReleaseDate;
    private double _AgeInDays;

    public DateTime  ReleaseDate
    {
        get { return _ReleaseDate; }
        set { _ReleaseDate = value;
        _AgeInDays = (DateTime.Now - value).TotalDays; 
        }
    }     
    //here we define a read-only property this will gives you the age in days
    //when ever you assign value to the  ReleaseDate.
    //You cannot directly assign value to AgeInDays. 
    public double AgeInDays
    {
        get { return _AgeInDays; }            
    }

因此你的电影类将是这样的:

 public class Movie
        {
            public string movieName { get; set; }
            public string onDVD { get; set; }
            public string onBluRay { get; set; }
            public string genreType { get; set; }
            public string directorName { get; set; }
            public string producerName { get; set; }
            public int movieLength { get; set; }
            public string moveRating { get; set; }
            private DateTime _ReleaseDate;
            private double _AgeInDays;
            public DateTime ReleaseDate
            {
                get { return _ReleaseDate; }
                set
                {
                    _ReleaseDate = value;
                    _AgeInDays = (DateTime.Now - value).TotalDays;
                }
            }
            public double AgeInDays
            {
                get { return _AgeInDays; }
            }
        }

【讨论】:

  • 我不应该在包含所有方法的类中添加它吗?我已经在 Datagrid 中显示了 Movie Age 列,我只需要计算即可在该列中添加正确的日期。
  • 将其包含在您的Movie 类中并绑定到网格,它将为您提供输出
  • 我已经发布并绑定了。我将 releaseDate 与 _ReleaseDate 交换,它显示在网格中,但是,它表示天数为 0。
【解决方案2】:

这将为您提供年份差异:

Movie movie = new Movie()
TimeSpan ts = DateTime.Today - movie.releaseDate;
movie.movieAge = ts.Days/365;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    • 2013-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-21
    相关资源
    最近更新 更多