This is a migrated thread and some comments may be shown as answers.

Invoking commands from code behind

3 Answers 118 Views
PdfViewer
This is a migrated thread and some comments may be shown as answers.
Paul Ridden
Top achievements
Rank 1
Veteran
Paul Ridden asked on 08 Apr 2019, 09:16 AM

I would like to invoke the NavigateToNextPageCommand, NavigateToPreviousPageCommand and NavigateToPageCommand commands from code behind however I am having a bit of trouble.

I have successfully invoked the NavigateToNextPageCommand using the following code :-

if (PdfViewerControl2.NavigateToNextPageCommand.CanExecute(null))
{

PdfViewerControl2.NavigateToNextPageCommand.Execute(null);

CurrentPageNumber++;

SetCanExecutes(BindingContext as SmartDocViewModel);
}

However, using a similar approach for the NavigateToPreviousPageCommand, the CanExecute method against the command returns false.

Also I am trying to invoke the NavigateToPageCommand in such a way that it acts as a beginning/end command, so I pass either 1 or the VisiblePagesCount value (last page) to the NavigateToPageCommand command however it does not change the page.

The provided sample uses the commands directly via buttons in the XAML however due to my use case I need to invoke the commands in code behind.

Would you be able to help me?

3 Answers, 1 is accepted

Sort by
0
Paul Ridden
Top achievements
Rank 1
Veteran
answered on 08 Apr 2019, 10:54 AM

After further investigation there seems to be a bug with the previous page command.

I was initially using a 2 page PDF however I have now moved on to you use 3, 4, 5 and 10 page PDFs and they are all highlight the same issue.

Taking the 10 page PDF (http://www.hse.gov.uk/pubns/indg143.pdf) as an example, I can move forward, from one page to the next getting to the 10th page. However when I invoke the NavigateToPreviousPageCommand while on the 10th page I am taken back to the 8th page of the PDF. The same thing happens on 3, 4 and 5 page PDFs, I am always taken back to the second from last page (3 page to 1st, 4 page to 2nd and 5 page to 3rd).

I still can not figure out how to get the NavigateToPageCommand  to work.

0
Paul Ridden
Top achievements
Rank 1
Veteran
answered on 08 Apr 2019, 12:36 PM

After using JustDecompile, I have found that the type of the parameter that needs to be passed into the NavigateToPageCommand.Execute method is a string. Also, I need to use the Document.Pages.Count property and not the VisiblePagesCount. Therefore the following now works for me :-

PdfViewerControl2.NavigateToPageCommand.Execute(
viewModel.AtBeginning ?
PdfViewerControl2.Document.Pages.Count.ToString() :
"1");

0
Petar Marchev
Telerik team
answered on 10 Apr 2019, 03:07 PM
Hi Paul,

Thank you for the explanations, this gave me a good idea of how you are using things now, and the result you are after.

I want to quickly explain the idea behind the VisiblePagesStartIndex and VisiblePagesCount properties. Let's say that I have loaded a document with a thousand pages. The pages are kept in a collection and the 1st page is at index 0 in this collection. The VisiblePagesStartIndex property contains the index of the first page that is visible on the screen.  Now, depending on the pages height, one or more pages may be in the visible area, so the VisiblePagesCount is the number of visible pages on the screen. Let's say there are 2 pages on screen (8 and 9), then the VisiblePagesStartIndex is 7 and the VisiblePagesCount is 2.

Note that even if you scroll to the very bottom of page with index 7, it will still be the first visible page, so the VPSI property will be 7. If at this point you execute navigate-back, then you would be taken to page with index 6. However, to a viewer it may not be obvious that he was seeing page with index 7 because much of his screen was page with index 8 and he may be surprised to see page with index 6.

I suspect that a similar thing is happening on your side, so I suggest you take a closer look at the start index and the pages count and see if they match what is actually displayed on screen.

About the navigate-to-page command - after taking your input into consideration, we now think that there is a bug in it. Presently, it takes the page number and navigates to it. However, here the page number is not the index. Also it expects a string and not an int. Both of these make are strange. I logged this in our feedback portal where you can track its status. The fix will be available with the next official release (mid of May). I have also updated your Telerik points for helping us improve our product.

Additionally, I did some tests with the navigate-to-previous and navigate-to-next page commands and did not find any bugs there.

I hope I was able to address all your inquiries, let me know if I missed something or if you need more information.

Regards,
Petar Marchev
Progress Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
PdfViewer
Asked by
Paul Ridden
Top achievements
Rank 1
Veteran
Answers by
Paul Ridden
Top achievements
Rank 1
Veteran
Petar Marchev
Telerik team
Share this question
or