【发布时间】: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"