I'm having weird behavior with my messagebox
here is the code:
private async void rate_Tap(object sender, System.Windows.Input.GestureEventArgs e) {
string id = (string)((Image)sender).Tag;
ignoreSelectionChanged = true;
MobileServiceCollection<rating, rating> items;
IMobileServiceTable<rating> itemTable = App.MobileService.GetTable<rating>();
items = await itemTable
.Where(Table => Table.userid == userId)
.ToCollectionAsync();
if (id != null) {
for (int i = 0; i < items.Count; i++) {
if (items[i].itemid == id) {
MessageBox.Show("You already giving your rating.");
i = items.Count;
return;
}
else {
RadMessageBox.Show(
new string[] { "very accurate", "not accurate" },
"Acurate?",
"Is this information accurate?", closedHandler: (args) => {
int buttonIndex = args.ButtonIndex;
if (buttonIndex == 0) {
clearListBox();
ratingPlus(id);
saveRating(id);
mvm.LoadDetailData();
}
if (buttonIndex == 1) {
clearListBox();
ratingMinus(id);
saveRating(id);
mvm.LoadDetailData();
}
}
);
}
}
}
}
What my code above dord is I trigger rate_Tap()
from my listbox that already contains image, and each time I tap it , it's supposed to check with my windows azure server and check if there is an itemid
that equals id
. Then I will show messagebox saying I already rated it and if there isn't any itemid
that equals id
then it will execute radmessagebox.
But it's not working that way: when it checks there is an itemid
that's equal to id
, it shows the messagebox and after that it show the radmessagebox.
Where did I go wrong?
Your "else" block contains the code that you want to execute after you've checked all items - not on each item.
I think you want:
if (items.Any(item => item.itemid == id))
{
MessageBox.Show("You already giving your rating.");
return;
}
RadMessageBox.Show(...);
// etc
Ideally, don't fetch all the previous ratings - change your query so that it includes the ID of the item you're trying to rate. After all, you only want to know whether or not you've already rated it - the rest of the information is pointless, so why fetch it all?
See more on this question at Stackoverflow