我正在研发VS2015上的UWP选举指挥。我已经制作了候选类并将其属性设置如下:
public class Candidate
{
public int VoteCount { get; set; }
public string Name { get; set; }
public string Category { get; set; }
public CandidateSNo S_no { get; set; }
public CandidateCategoryNo Category_no { get; set; }
}
public enum CandidateCategoryNo
{
I,
II,
III,
IV,
V,
VI
}
public enum CandidateSNo
{
I,
II,
III
}
我已经创建了一个CandidateManager类来存储方法,在其中,我已经在一个方法中存储了一个可观察的集合,该方法包含每个候选项的5个属性。这是CandidateManager类:
private static ObservableCollection<Candidate> getCandidate()
{
var _candidate = new ObservableCollection<Candidate>();
_candidate.Add(new Candidate() { S_no = CandidateSNo.I, Category_no = CandidateCategoryNo.I, VoteCount = 0, Category = "President", Name = "A" });
_candidate.Add(new Candidate() { S_no = CandidateSNo.II, Category_no = CandidateCategoryNo.I, VoteCount = 0, Category = "President", Name = "B" });
_candidate.Add(new Candidate() { S_no = CandidateSNo.III, Category_no = CandidateCategoryNo.I, VoteCount = 0, Category = "President", Name = "C" });
return _candidate;
}
public static ObservableCollection<Candidate> GetAllCandidates()
{
ObservableCollection<Candidate> Candidates = CandidateManager.getCandidate();
return Candidates;
}
每当有人选择他们的名字按钮时,我希望每个候选人的投票数增加1。我在页面上制作了3个用于选择候选人的按钮和一个提交按钮以提交他们的选择。之后,结果文本块将显示选择了哪个候选项。这是主页的XAML:
<Button Name="Cand2" Width="100" Height="100" FontSize="20" HorizontalAlignment="Center" VerticalAlignment="Top" Click="Cand2_Click"/>
<Button Name="Cand3" Width="100" Height="100" FontSize="20" HorizontalAlignment="Right" VerticalAlignment="Top" Click="Cand3_Click"/>
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Width="200" Height="50" FontSize="30" Name="Res"/>
<Button Name="Submit" VerticalAlignment="Bottom" HorizontalAlignment="Center" Content="Submit" Click="Submit_Click" />
</Grid>
这是xaml.cs:
public MainPage()
{
this.InitializeComponent();
ObservableCollection<Candidate> Candidates = CandidateManager.GetAllCandidates();
Cand1.Content = CandidateManager.GetCandidateByCategoryAndSno(CandidateCategoryNo.I, CandidateSNo.I).Name;
Cand2.Content = CandidateManager.GetCandidateByCategoryAndSno(CandidateCategoryNo.I, CandidateSNo.II).Name;
Cand3.Content = CandidateManager.GetCandidateByCategoryAndSno(CandidateCategoryNo.I, CandidateSNo.III).Name;
}
private void Cand1_Click(object sender, RoutedEventArgs e)
{
ObservableCollection<Candidate> Candidates = CandidateManager.GetAllCandidates();
Candidate chosenOne = Candidates.Where(c => c.Name == Cand1.Content).FirstOrDefault();
chosenOne.VoteCount += 1;
Candidates.Where(c => c.Name == Cand1.Content).FirstOrDefault().VoteCount = chosenOne.VoteCount;
}
private void Cand2_Click(object sender, RoutedEventArgs e)
{
ObservableCollection<Candidate> Candidates = CandidateManager.GetAllCandidates();
Candidate chosenOne = Candidates.Where(c => c.Name == Cand2.Content).FirstOrDefault();
chosenOne.VoteCount += 1;
Candidates.Where(c => c.Name == Cand2.Content).FirstOrDefault().VoteCount = chosenOne.VoteCount;
}
private void Cand3_Click(object sender, RoutedEventArgs e)
{
ObservableCollection<Candidate> Candidates = CandidateManager.GetAllCandidates();
Candidate chosenOne = Candidates.Where(c => c.Name == Cand3.Content).FirstOrDefault();
chosenOne.VoteCount += 1;
Candidates.Where(c => c.Name == Cand3.Content).FirstOrDefault().VoteCount = chosenOne.VoteCount;
}
private void Submit_Click(object sender, RoutedEventArgs e)
{
ObservableCollection<Candidate> Candidates = CandidateManager.GetAllCandidates();
Candidate chosenOne = Candidates.Where(c => c.VoteCount == 1).FirstOrDefault();
if (chosenOne != null)
Res.Text = chosenOne.Name + " -- " + chosenOne.VoteCount.ToString();
else
Res.Text = "null";
}
我面临的问题是我无法检索更新的投票数,结果文本块显示为null。 这只是主要项目的一个小虚拟,但我在主程序中面临同样的问题。我希望在程序运行时更新候选人的votecount和其他属性,并在需要时检索它们。 为长代码道歉。希望你能帮忙..谢谢!
答案 0 :(得分:0)
您应该使用类级别变量进行可观察收集并对其进行更改。
您的代码存在问题,每个方法都有自己的本地可观察集合实例。我编辑了一小部分代码隐藏,请参阅
ObservableCollection<Candidate> Candidates = null;
public MainPage()
{
this.InitializeComponent();
Candidates = CandidateManager.GetAllCandidates();
Cand1.Content = CandidateManager.GetCandidateByCategoryAndSno(CandidateCategoryNo.I, CandidateSNo.I).Name;
Cand2.Content = CandidateManager.GetCandidateByCategoryAndSno(CandidateCategoryNo.I, CandidateSNo.II).Name;
Cand3.Content = CandidateManager.GetCandidateByCategoryAndSno(CandidateCategoryNo.I, CandidateSNo.III).Name;
}
private void Cand1_Click(object sender, RoutedEventArgs e)
{
Candidate chosenOne = Candidates.Where(c => c.Name == Cand1.Content).FirstOrDefault();
chosenOne.VoteCount += 1;
Candidates.Where(c => c.Name == Cand1.Content).FirstOrDefault().VoteCount = chosenOne.VoteCount;
}
private void Cand2_Click(object sender, RoutedEventArgs e)
{
Candidate chosenOne = Candidates.Where(c => c.Name == Cand2.Content).FirstOrDefault();
chosenOne.VoteCount += 1;
Candidates.Where(c => c.Name == Cand2.Content).FirstOrDefault().VoteCount = chosenOne.VoteCount;
}
private void Cand3_Click(object sender, RoutedEventArgs e)
{
Candidate chosenOne = Candidates.Where(c => c.Name == Cand3.Content).FirstOrDefault();
chosenOne.VoteCount += 1;
Candidates.Where(c => c.Name == Cand3.Content).FirstOrDefault().VoteCount = chosenOne.VoteCount;
}
private void Submit_Click(object sender, RoutedEventArgs e)
{
Candidate chosenOne = Candidates.Where(c => c.VoteCount == 1).FirstOrDefault();
if (chosenOne != null)
Res.Text = chosenOne.Name + " -- " + chosenOne.VoteCount.ToString();
else
Res.Text = "null";
}