question

24564018 avatar image
0 Votes"
24564018 asked 24564018 answered

How should I code in the style of MVVM with the following model

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!

windows-wpf
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi,@24564018. What do you want to show? Or which collection class do you want to bind to?
For using nested collections in MVVM, you could try to see the links:
https://stackoverflow.com/questions/4244222/building-viewmodels-based-on-nested-model-entities-in-wpf-and-mvvm-pattern
https://stackoverflow.com/questions/57911426/c-sharp-wpf-nested-list-in-mvvm

1 Vote 1 ·

1 Answer

24564018 avatar image
0 Votes"
24564018 answered

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.