/// <summary> /// 依據Column * Row調整投票選項數量 /// </summary> private void ChangeVoteCandidate() { int count = this.Column * this.Row; //刪除多餘選項 while (_voteCandidateColle.Count > count) { VoteCandidateVM del = _voteCandidateColle.ElementAt(count); del.Clear(); del.CommandAction = null; _voteCandidateColle.RemoveAt(count); } //新增不足選項 while (_voteCandidateColle.Count < count) { VoteCandidateVM candidate = new VoteCandidateVM(); _voteCandidateColle.Add(candidate); } //初始化所有選項編號與狀態 for (int i = 0; i < _voteCandidateColle.Count; i++) { _voteCandidateColle.ElementAt(i).Num = i + 1; _voteCandidateColle.ElementAt(i).IsShowStatistic = this.IsShowStatistic; _voteCandidateColle.ElementAt(i).IsShowVoterListButton = this.IsShowVoterListButton; _voteCandidateColle.ElementAt(i).IsShowVoterSlide = this.IsShowVoterSlide; } }
/// <summary> /// 解析留言是否為有效票並進行投票 /// </summary> /// <param name="vm">留言</param> /// <returns>若投票成功則回傳True</returns> public bool Vote(CommentVM vm) { //判斷投票是否已開始 if (!this.IsStarted) { return(false); } int voteNum = 0; if (Int32.TryParse(vm.Message, out voteNum)) { //判斷是否已投過票 bool isVoted = _voterTable.IsVoted(vm.AuthorID); if (isVoted) { if (!this.CanChangeVote) { //不允許換票 //判斷投票者是否已投滿票數 if (_voterTable.GetVoterCount(vm.AuthorID) >= this.MaxVoteCount) { return(false); } } else if (this.CanChangeVote && this.MaxVoteCount == 1) { //允許換票,單票制 //移除舊票 Dictionary <CommentVM, VoteCandidateVM> tickets = _voterTable.GetVoterTicket(vm.AuthorID); if (tickets != null) { VoteCandidateVM candidate = tickets.Values.ElementAt(0); CommentVM oldVote = tickets.Keys.ElementAt(0); candidate.RemoveVote(oldVote); tickets.Remove(oldVote); _allVoteColle.Remove(oldVote); } } else if (this.CanChangeVote && this.MaxVoteCount > 1) { //允許換票,多票制 } } //計票 bool isValidVote = false; foreach (VoteCandidateVM candidate in _voteCandidateColle) { if (candidate.Num == voteNum) { _voterTable.Add(vm, candidate); _allVoteColle.Add(vm); OnPropertyChanged(nameof(this.VoteCount)); candidate.AddVoter(vm); isValidVote = true; break; } } //計所有得票率 if (isValidVote) { foreach (VoteCandidateVM candidate in _voteCandidateColle) { candidate.Rate = Math.Round((double)candidate.Count / this.VoteCount, 4) * 100; } } return(isValidVote); } return(false); }