参考我最后一个问题。我需要我的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中。
答案 0 :(得分:0)
利用属性:
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; }
}
}
答案 1 :(得分:0)
这将为您带来年份差异:
body: "someKey=someValue&anotherKey=anotherValue...."