0

所以我有一个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();
  }

我希望我对这个问题的描述足够好,并且很高兴能在我的问题上得到一些帮助。

提前致谢!

4

2 回答 2

2

无需DispatcherTimer为每个预订单独创建一个,您可以只创建一个按时间间隔触发一次的单个预订,例如每分钟一次(取决于您希望看到颜色变化的频率)。

Tick处理程序中,您可以检查每个房间:

  • 预订处于活动状态:红色- 可以通过foreach对给定房间的预订进行简单循环来完成此检查
  • 预订在不到 15 分钟内开始:黄色- 再次简单foreach循环,检查所有预订的开始时间,如果距离不到 15 分钟,我们有一个匹配
  • 否则:绿色
于 2018-06-20T04:38:31.463 回答
1

DispatcherTimer.Tick事件在Interval中指定的时间过去后触发。Tick以相同的间隔继续触发,直到调用Stop方法、应用程序终止或应用程序被挂起(触发 Suspending)。所以你可以把循环放在这里并指定内部是一分钟。

此外,如果您希望您的应用程序可以在最小化或锁定屏幕下运行,您可以使用扩展执行来实现它。请参阅主题推迟应用程序暂停并延长执行

- -更新 - -

private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
    //Get current managed thread ID
    Debug.WriteLine(Environment.CurrentManagedThreadId);
    DispatcherTimer timer = new DispatcherTimer();
    timer.Interval = TimeSpan.FromMinutes(1);
    timer.Tick += async (ob, arg) =>
    {
        Debug.WriteLine(Environment.CurrentManagedThreadId);
    //You can update the booking room color here
    //TODO get data and update room color

    //You can also update the booking room color using Dispatcher.RunAsync method. 
    //This is alternative to update the data on the Tick event above TODO part directly .

        //await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
        //{
        //    Debug.WriteLine(Environment.CurrentManagedThreadId);
        //    //TODO get data and update room color
        //});
    };
    timer.Start();
}

可以看到CoreDispatcher.RunAsync(CoreDispatcherPriority, DispatchedHandler)方法。

于 2018-06-25T07:47:13.570 回答