Well, after a night's work, I figured out that what I really wondered was how to create a proper view model according to the model given above, and now I worked out the solution.
Thanks anyway.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I am quite new to WPF and MVVM, and I wonder how can a class like the following can code in the style of MVVM (I know how to handle it with MVC). I am having trouble especially because the class contains List and Dictionary, and I wonder whether I should use ObserableCollection directly in the class.
Here is the class file:
public class YuzuProject
{
private string _path;
public string path
{
get
{
return _path;
}
set
{
if (!IOUtils.JudgeFilePath(value))
throw new Exception("Invalid Yuzu Project Path");
_path = value;
}
}
public string projectName;
private string _fileName;
public string fileName
{
get
{
return _fileName;
}
set
{
if (!IOUtils.JudgeFileName(value))
throw new Exception("Invalid Yuzu Project file name");
_fileName = value;
}
}
public List<YuzuImage> Images;
public void EnsureImageFolderExist()
{
IOUtils.EnsureDirectoryExist(Path.Combine(path, "./Images"));
}
public void EnsurePSDFolderExist()
{
IOUtils.EnsureDirectoryExist(Path.Combine(path, "./PSD"));
}
public YuzuProject(string path, string fileName, string projectName)
{
this.path = path;
this.fileName = fileName;
this.projectName = projectName;
Images = new List<YuzuImage>();
}
public YuzuProject(string path, string fileName, string projectName, List<YuzuImage> Images)
{
this.path = path;
this.fileName = fileName;
this.projectName = projectName;
this.Images = Images;
}
public void RemoveImageAt(int index)
{
File.Delete(Path.Combine(path, Images[index].ImageName));
Images.RemoveAt(index);
}
private string CopyImage(string imagePath)
{
EnsureImageFolderExist();
string originalImageFileName = Path.GetFileName(imagePath);
string imageFileName = originalImageFileName;
int prefix = 1;
bool exist = true;
while (exist)
{
exist = false;
foreach (YuzuImage yuzuImage in Images)
{
if (yuzuImage.ImageName == imageFileName)
{
exist = true;
imageFileName = (prefix++) + "-" + originalImageFileName;
break;
}
}
}
string targetImagePath = Path.Combine(path, "./Images/", imageFileName);
File.Copy(imagePath, targetImagePath);
return imageFileName;
}
public string InsertImageAt(int index, string imagePath)
{
string imageFileName = CopyImage(imagePath);
Images.Insert(index, new YuzuImage(this, imageFileName));
return imageFileName;
}
public string AddImage(string imagePath)
{
string imageFileName = CopyImage(imagePath);
Images.Add(new YuzuImage(this, imageFileName));
return imageFileName;
}
public void MoveImage(int fromIndex, int toIndex)
{
YuzuImage moveItem = Images[fromIndex];
Images.Insert(toIndex, moveItem);
}
}
public class YuzuImage
{
public YuzuProject parent;
public string ImageName;
public Dictionary<long, YuzuSimpleNotation> SimpleNotations = new Dictionary<long, YuzuSimpleNotation>();
public YuzuImage(YuzuProject parent, string ImageName)
{
if (!File.Exists(Path.Combine(parent.path, "./Images/", ImageName)))
throw new Exception("YuzuImage Init Error: file does not exist. Name: " + ImageName);
this.parent = parent;
this.ImageName = ImageName;
}
public void AddSimpleNotation(int x, int y, string text)
{
SimpleNotations.Add(new DateTimeOffset(DateTime.Now).ToUnixTimeMilliseconds(), new YuzuSimpleNotation(x, y, text));
}
public void RemoveNotation(long timestamp)
{
SimpleNotations.Remove(timestamp);
}
public string GetImageFilePath()
{
parent.EnsureImageFolderExist();
return Path.Combine(parent.path, "./Images/", ImageName);
}
public string GetImagePSDPath()
{
parent.EnsurePSDFolderExist();
return Path.Combine(parent.path, "./PSD/" + ImageName + ".psd");
}
}
public class YuzuSimpleNotation
{
public int x;
public int y;
public string text;
public YuzuSimpleNotation(int x, int y, string text)
{
this.x = x;
this.y = y;
this.text = text;
}
}
Thanks in advance!
Well, after a night's work, I figured out that what I really wondered was how to create a proper view model according to the model given above, and now I worked out the solution.
Thanks anyway.