コード例 #1
0
        public SendEditPostPageViewModel(CancellationTokenSource cts, PostEditDataModel editData,
                                         Action <int, int, string> beforeUpload, Action <string> insertFileCodeIntoContentTextBox, Action <int> afterUpload, Action <string> sentFailded, Action <string> sentSuccess)
        {
            ShowUnusedImage(cts);

            Title          = editData.Title;
            Content        = editData.Content;
            AttachFileList = editData.AttachFileList;
            _postId        = editData.PostId;
            _threadId      = editData.ThreadId;
            _beforeUpload  = beforeUpload;
            _insertFileCodeIntoContentTextBox = insertFileCodeIntoContentTextBox;
            _afterUpload = afterUpload;
            _sentSuccess = sentSuccess;
            _sentFailded = sentFailded;

            //RemoveAttachFileCommand = new DelegateCommand();
            //RemoveAttachFileCommand.ExecuteAction = (p) =>
            //{
            //    int a = 1;
            //};

            //InsertAttachFileCommand = new DelegateCommand();
            //InsertAttachFileCommand.ExecuteAction = (p) =>
            //{
            //    int b = 1;
            //};

            AddAttachFilesCommand = new DelegateCommand();
            AddAttachFilesCommand.ExecuteAction = async(p) =>
            {
                var data = await SendService.UploadFileAsync(cts, _beforeUpload, _afterUpload);

                if (data[0] != null && data[0].Count > 0)
                {
                    _fileAddList.AddRange(data[0]);
                }
                if (data[1] != null && data[1].Count > 0)
                {
                    _fileCodeList.AddRange(data[1]);
                }

                if (_fileCodeList.Count > 0)
                {
                    string fileCodes = string.Join("\r\n", _fileCodeList).Trim();
                    _insertFileCodeIntoContentTextBox($"{fileCodes}\r\n");
                    _fileCodeList.Clear();

                    ShowUnusedImage(cts);
                }
            };

            SendCommand = new DelegateCommand();
            SendCommand.ExecuteAction = async(p) =>
            {
                if (string.IsNullOrEmpty(Content))
                {
                    _sentFailded?.Invoke("请填写内容!");
                    return;
                }

                await SendService.SendEditPostAsync(cts, Title, Content, _fileAddList, _fileRemoveList, _postId, _threadId);

                _fileAddList.Clear();
                _fileRemoveList.Clear();

                Title   = string.Empty;
                Content = string.Empty;

                // 提示发贴成功
                _sentSuccess?.Invoke(Title);
            };
        }