When you are working with APIs from web services, you probably ran already into the same problem that I did recently: how to detect if we are on the last page of possible API results.
Some APIs (like WordPress) use tokens to be sent as parameter with your request, and if the token is null or empty you know that you have reached the last page. However, not all APIs are working that way (for example UserVoice).
As I am rewriting Voices Admin to be a Universal app, I came up with a simple but effective helper method that allows me to easily detect if I am on the last page. Here is what I did:
public static bool IsLastPage(int total, int countperpage, int current)
bool value = false;
if (current < Convert.ToInt32(Math.Ceiling(Convert.ToDouble(total)/countperpage)))
value = false;
if (current == Convert.ToInt32(Math.Ceiling(Convert.ToDouble(total)/countperpage)))
value = true;
As you can see, I need the number of total records that can be fetched (which is returned by the API) and the property for the number per page (which is one of the optional parameters of the API). On top, I need the current page number to calculate where I am (which is also an optional parameter of the API and returned by the API result).
Now I simply need to divide the total records by the result count per page to get how many pages are used. Using the Math.Ceiling() method, I always get the correct number of pages back. What does the Math.Ceiling() method do? It just jumps up to the next absolute number, also known as “rounding toward positive infinity”.
Example: if you have 51 total records and a per page count of 10, the division will return 5.1 (which means there is only one result on the 6th page). However, we need an absolute number. Rounding the result would return 5 pages, which is wrong in this case. The Math.Ceiling() method however returns the correct value of 6.
Setting the method up as a static Boolean makes it easy to change the CanExecute property of a button for example, which will be automatically disabled if we just have loaded the last page (page 6 in my case).
As always, I hope this is helpful for some of you.
Recently, I started to create a real world app to demonstrate the usage of my WordPressUniversal library. While I was working on it, I also decided to extend the Splahscreen which is mandatory for universal apps.There are several reasons to extend the splash screen, the most obvious is to hide initial data loading.
The only sample I found had a separate solution for the Windows and the Windows Phone application, and I decided to put them together to use the advantages of the Shared project that comes with every universal app.
The first step is to add your Splashscreen images to both projects. To do so, open their Package.appxmanifest files and go to the ‘Visual Assets’ tab. Add your images:
Now add a UserControl to your shared project and rename the control type to “Grid” in the XAML file. After that, let us design the UI that is shown to the user when he starts our app:
<!--Windows needs a Canvas-->
<!--Windows Phone needs a Viewbox-->
<!--this StackPanel holds our ProgressRing that is telling the user we are doing some work-->
This XAML code is the base for our extended Splashscreen. It defines a row height of 180 to place the StackPanel that holds the ProgressRing at the bottom of our control. The main difference here is that Windows Apps use a Canvas while Windows Phone uses Viewbox to hold our Splashscreen Image. We need to set the Visibility on both to collapsed as our code decides which one will be displayed on Launch.
In our code page, we have a bit more work to do. Let’s go through it step by step:
Make sure you have added the following Namespaces:
Note: if you do not have a Common folder in your Shared Project, you can add it manually and copy the classes from the demo project attached to this project. Don’t forget to change the Namespaces of the classes to your project name after you added them.
The next step is to declare some objects that we need for the extended Splashscreen:
The Rect is used to place the Splashscreen Image in it and will be sized by our code which we will add later. The dismissed bool is used to determine when the system Splashscreen gets dismissed. The rootFrame is used for the navigation to our first application page, while the splash is used to read the needed values for placing our image into the Rect.
After we have declared those objects, we need to overload the main class of our control:
public ExtendedSplash(SplashScreen splashcreen, bool loadstate)
The splashcreen tells us the coordinates and the height of the splash image. You can use the loadstate bool to determine data that needs to be restored by the SuspensionManager, if needed.
As users are snapping our apps or rotating the device, we need to handle this. To do so, we need to add the following line to our constructor:
Window.Current.SizeChanged += new WindowSizeChangedEventHandler(ExtendedSplash_OnResize);
Add this code to handle the resizing:
if (splash != null)
splashImageRect = splash.ImageLocation;
If the user causes the WindowSizeChanged event to fire, the splash will be loaded again and submits new coordinates to our splash object. To properly handle the positioning, we are using the PositionImage() method:
As you can see, we use the preprocessor directive WINDOWS_PHONE_APP to declare the height and width of the Viewbox. If it is not running on Windows Phone, we are setting the canvas coordinates and size via this method. This is not the only point where we need this method, as you will see later in this post.
Let’s go back to our constructor and add this line to load the system’s Splashscreen values into our object:
splash = splashcreen;
Now that we have these values, we are finally able to receive the coordinates and declare which Splashscreen will be used:
if (splash != null)
//handle the dismissing of the system's splash
splash.Dismissed += new TypedEventHandler<SplashScreen, Object>(DismissedEventHandler);
//get the system's splashscreen values
splashImageRect = splash.ImageLocation;
//decide which control is used to display the splashscreen image and size the progressRing
Win_Splash_Img.Visibility = Visibility.Collapsed;
WP_Splash_Img.Visibility = Visibility.Visible;
progressRing.Height = 50;
progressRing.Width = 50;
Win_Splash_Img.Visibility = Visibility.Visible;
WP_Splash_Img.Visibility = Visibility.Collapsed;
progressRing.Height = 70;
progressRing.Width = 70;
//position and size the image properly
Let me explain what I have done here. First we need to check if our splash object has a value. Then, we need to handle the Dismissing of the system’s Splashscreen with this TypedEventHandler:
After that, I am declaring when to show Windows Phone’s Viewbox and Windows’ Canvas as well as setting the size of the ProgressRing. The last step is to position and size the image again with our PositionImage() method. This will cause the app to switch to our extended Splashscreen.
Now we have to declare a new Frame instance and begin to load our data:
// this frame acts as navigation context
rootFrame = new Frame();
//start loading your data:
For this demo, I used a delay to keep the ProgressRing spinning:
async void LoadData()
progressText.Text = "sleeping, please wait until I wake up...";
//using a delay to keep the progress ring spinning for this demo
//Navigate to the first application page after all work is done
Window.Current.Content = rootFrame;
That’s all of the code we need inside our ExtendSplash control. If you now build the project, it will compile, but nothing will happen. other than going directly to the MainPage.
We need to add some additional code to our App.xaml.cs in the OnLaunched event:
//only show the splash if the app wasn't running before
if (e.PreviousExecutionState != ApplicationExecutionState.Running)
//check the loadstate if necessary
bool loadState = (e.PreviousExecutionState == ApplicationExecutionState.Terminated);
//create a new instance of our ExtendedSplash
ExtendedSplash extendedSplash = new ExtendedSplash(e.SplashScreen, loadState);
//declare our extendedSplash as root content
rootFrame.Content = extendedSplash;
If you hit the debug button, you should have similar results to these:
For your convenience, I created a small demo project. Download it here.
As always, I hope this blog post is helpful for some of you. If you have questions or feedback, feel free to leave a comment below.