以下依赖于存储每部电影的标题和图像的 json 文件。
此处完成的 json 文件位于包含电影图像的文件夹中,但可以位于任何适合您的文件夹中。
我使用可用图像的 Json 文件)
[
{
"Title": "Movie 1",
"ImageName": "checkmark.png"
},
{
"Title": "Movie 2",
"ImageName": "Mail_16x.png"
},
{
"Title": "Movie 3",
"ImageName": "upGreenArrow.png"
}
]
电影课
namespace MoviesCodeSample
{
/// <summary>
/// Represents a single movie, add other
/// properties if needed before serializing
/// data in MovieOperations.
/// </summary>
public class Movie
{
public string Title { get; set; }
public string ImageName { get; set; }
/// <summary>
/// For ComboBox DisplayMember
/// </summary>
/// <returns></returns>
public override string ToString() => Title;
}
}
电影运营
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using Newtonsoft.Json;
namespace MoviesCodeSample
{
/// <summary>
/// Movie operations which requires Json.Net NuGet package
/// or if using .NET Core 5 or higher use System.Text.Json
/// </summary>
public class MovieOperations
{
/*
* Mocked up for demonstration, these two properties
* can be stored under application settings or a json file
* obtaining your settings rather than done via project properties.
*/
public static string FolderName = "TODO";
public static string FileName = Path.Combine(FolderName,"Movies.json");
/// <summary>
/// Mockup for demonstration purposes, you would create your own
/// list, perhaps creating a utility that reads images, allows you
/// to add descriptions for each movie save back to disk
/// </summary>
public static List<Movie> Movies => new List<Movie>()
{
new Movie() { Title = "Movie 1", ImageName = "checkmark.png" },
new Movie() { Title = "Movie 2", ImageName = "Mail_16x.png" },
new Movie() { Title = "Movie 3", ImageName = "upGreenArrow.png" }
};
public static void CreateInitialMoviesJsonFile()
{
File.WriteAllText(
FileName,
JsonConvert.SerializeObject(Movies, Formatting.Indented));
}
/// <summary>
/// Read movies from json file, in this case for
/// demonstration a mocked up json file is created if not exists
/// which in this case it does not.
/// </summary>
/// <returns>movies in json file</returns>
public static List<Movie> GetMovies()
{
if (!File.Exists(FileName))
{
CreateInitialMoviesJsonFile();
}
return JsonConvert.DeserializeObject<List<Movie>>(
File.ReadAllText(FileName));
}
/// <summary>
/// Get current movie triggered from selection changed in the ComboBox
/// on the calling form.
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static Image GetMovieImage(string name)
=> Image.FromFile(Path.Combine(FolderName, name));
}
}
表格,一个PictureBox,一个ComboBox
using System;
using System.Windows.Forms;
namespace MoviesCodeSample
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Shown += OnShown;
}
private void OnShown(object sender, EventArgs e)
{
MoviesComboBox.DataSource = MovieOperations.GetMovies();
MoviesComboBox.SelectedIndexChanged += MoviesComboBoxOnSelectedIndexChanged;
ShowCurrentMovieImage();
}
private void MoviesComboBoxOnSelectedIndexChanged(object sender, EventArgs e)
{
ShowCurrentMovieImage();
}
private void ShowCurrentMovieImage()
{
MoviePictureBox.Image = MovieOperations.GetMovieImage(
((Movie)MoviesComboBox.SelectedItem).ImageName);
}
}
}