所以我有一个UWP
项目,我处理一组房间的预订。我从Json API
.
我想做一个loop
检查房间是否被预订或不是每分钟或类似的东西,但我不知道该怎么做。
这就是我如何获得所有带有预订的房间以及它们的所有属性:
public async void addroom()
{
string url = "https://api.booking.com/api/company/07ce8f7c-f3d3-4df2-84bd-33f8fc263deb/rooms";
HttpClient client = new HttpClient();
string response = await client.GetStringAsync(url);
List<Class2> data = JsonConvert.DeserializeObject<List<Class2>>(response);
foreach (Class2 room in data)
{
string booking = $"https://api.booking.com/api/company/07ce8f7c-f3d3-4df2-84bd-33f8fc263deb/rooms/{room.id}/bookings";
HttpClient BookingClient = new HttpClient();
string BookingResponse = await BookingClient.GetStringAsync(booking);
List<Bookings> bookings = JsonConvert.DeserializeObject<List<Bookings>>(BookingResponse);
room.Bookings = bookings;
string id = room.id;
string name = room.name;
int seats = room.seats;
Uri Img = room.ImageUrl;
List<Roomattribute> roomattrib = room.roomAttributes;
var NewRoom = new Room
{
RoomID = id,
RoomName = name,
FrontImage = Img,
Seats = seats,
};
foreach (var books in bookings)
{
string note = books.note;
DateTime TimeFrom = books.timeFrom;
DateTime TimeTo = books.timeTo;
Class2 BookRoom = books.room;
string BookId = books.id;
DateTime Now = new DateTime(2018, 04, 25, 09, 40, 00);
var BeforeEnd = books.timeTo.Subtract(Now).Subtract(TimeSpan.FromMinutes(15));
var BeforeBegin = books.timeFrom.Subtract(Now).Subtract(TimeSpan.FromMinutes(15));
if (books.timeFrom <= Now && books.timeTo > Now)
{
ToRed();
DispatcherTimer ColorTimer = new DispatcherTimer();
ColorTimer.Interval = BeforeEnd;
ColorTimer.Tick += (sender, args) =>
{
ToYellow();
ColorTimer.Stop();
};
ColorTimer.Start();
}
else if (books.timeTo == Now)
{
ToGreen();
}
else
{
DispatcherTimer ColorTimer = new DispatcherTimer();
ColorTimer.Interval = BeforeBegin;
ColorTimer.Tick += (sender, args) =>
{
ToYellow();
ColorTimer.Stop();
};
ColorTimer.Start();
}
}
foreach (var attri in roomattrib)
{
int attriId = attri.id;
string attriName = attri.name;
int attriIcon = attri.icon;
if (room.roomAttributes.Any(a => a.id == 1))
{
NewRoom.Tv = Visibility.Visible;
}
else if (room.roomAttributes.Any(a => a.id != 1))
{
NewRoom.Tv = Visibility.Collapsed;
}
if (room.roomAttributes.Any(a => a.id == 2))
{
NewRoom.Wifi = Visibility.Visible;
}
else if (room.roomAttributes.Any(a => a.id != 2))
{
NewRoom.Wifi = Visibility.Collapsed;
}
if (room.roomAttributes.Any(a => a.id == 3))
{
NewRoom.Projector = Visibility.Visible;
}
else if (room.roomAttributes.Any(a => a.id != 3))
{
NewRoom.Projector = Visibility.Collapsed;
}
if (room.roomAttributes.Any(a => a.id == 4))
{
NewRoom.Wboard = Visibility.Visible;
}
else if (room.roomAttributes.Any(a => a.id != 4))
{
NewRoom.Wboard = Visibility.Collapsed;
}
}
Rooms.Add(NewRoom);
}
}
现在我的所有代码都运行良好(除此之外,所有预订都转到所有房间,但这是题外话......),当一个房间空置时,它有一个绿色LinearGredientBrush
,当一个房间被预订时,它正在改变颜色变为红色,当房间无人居住 15 分钟后,颜色变为黄色。
我需要检查的是,例如,如果一个房间在时间用完之前被取消。
我在想把所有这些都放在一个For loop
可能的解决方案中:
var BeforeEnd = books.timeTo.Subtract(Now).Subtract(TimeSpan.FromMinutes(15));
var BeforeBegin = books.timeFrom.Subtract(Now).Subtract(TimeSpan.FromMinutes(15));
if (books.timeFrom <= Now && books.timeTo > Now)
{
ToRed();
DispatcherTimer ColorTimer = new DispatcherTimer();
ColorTimer.Interval = BeforeEnd;
ColorTimer.Tick += (sender, args) =>
{
ToYellow();
ColorTimer.Stop();
};
ColorTimer.Start();
}
else if (books.timeTo == Now)
{
ToGreen();
}
else
{
DispatcherTimer ColorTimer = new DispatcherTimer();
ColorTimer.Interval = BeforeBegin;
ColorTimer.Tick += (sender, args) =>
{
ToYellow();
ColorTimer.Stop();
};
ColorTimer.Start();
}
我希望我对这个问题的描述足够好,并且很高兴能在我的问题上得到一些帮助。
提前致谢!