Binding updates to shell component pages delayed - xamarin

So I have a Shell that has a home page and a settings page.
The settings page makes changes to a singleton service that the home's ViewModel is binded to (MVVM).
I have set up break points on the PropertyChangedEvents of elements on the Home page and they hit as soon as changes are made to the service's properties and their PropertyChanged is executed.
However, upon going back to the home page via the Shell, the page's UI only updates when the page is active and visible. This causes fields to flash when they change. But the breakpoints suggest that they should have updated before.
Is there a way to force refresh the UI before or are there any other alternatives?
For now I have made my view perform a Fade In animation so that changes to the ViewModel don't cause flashing.

Even I have a Xamarin Forms Shell app, and I also had the same issue, So what you can do is, Make the common properties Global as in make those properties in App.xaml.cs and mark them as static.
So when the User changes the property on settings change the App.PropertyName.
Then on the OnApearing method of homepage call a method on the ViewModel say LoadDataValues() and read the App.PropertyName and modify the UI as needed.
What this will do is just before the HomePage appears it will keep the UI of HomePage ready.
Let me know if you have further issues.

Related

Change Sidedrawer content dynamically

I´m at that point on my app, where the user Logged in, receive the data from the server and now i need to make same changes. One of the changes is change the button that appears on my Sidedrawer saying Log In to Log Out and vice-versa when the user Logs Out...I could talk about other changes but i think the main thing is...
How do I access the Sidedrawer content in order to change/add buttons. I already entered the app-root.xml and made same testing adding the navigatingTo="onNavigatingTo" function and also in the .js file just to see if it responds, but it doesn´t...
How do i perform this?
You can show/hide your button via visibility, text binding or via a structural directive like *ngIf (if using Angular). For example, take a look here - I am showing/hiding a button based on whether the user is logged in or not (here is the related code-behind code).
The above example is using an Angular directive and can be applied only in Angular based applications, but with the same logic, you can substitute ****ngIf*** with visibility and achieve the same in TypeScript or plain JavaScript app.
The easiest way to do it is put the frame inside drawer content area, then navigate frame to another page.

NativeScript: how to save and restore a page state?

When we navigate to a page it reloads every time. If I previously created some views dynamically in it either programatically or using ui/builder all of this will be lost. Is there a way to save / cache the state of the page and restore it after the page reloading?
Set a boolean variable when you have loaded a page. Whenever navigating back to that page, check the status of the boolean variable in the pageLoad/onNavigatedTo hooked up functions before the actual code in that page.
I'm afraid of that there is no built-in feature for that.
What you can do is to store/reload manually your screen's data in application-settings at the unloaded and navigatingTo events.

Avoid re-display of menu using MVC

My team is creating an MVC4 application which has an upper menu structure that contains menu items specific for the currently logged-in user, and displays the content for the selected menu item below. The menus are displayed in a partial view "_MainMenu" and the MainMenuController Index action is responsible for getting the menu data from the database and returning the menu partial view. The main layout "_Layout" renders the menu by calling #Html.Action("Index", "MainMenu").
We want to avoid hitting the database to get the menu structure data every time the page is refreshed, and we also ideally want to avoid any visible reload of the menu structure when a different menu item is selected.
I've been looking into AJAX, and I've implemented something where selecting a new menu item will load the content for that item below the menu by replacing a div with ID="mainContent" with the content as a partial view (this div lives on the loaded view "MainContent"). In this way, the menu is never re-loaded and the page never refreshes completely (only the different partial content views are swapped in and out as different menu items are selected).
This solution works, but I'm not sure if it's really best practice, or if there's a better solution involving caching. One of the problems with the AJAX solution is the URL never changes from "http://MySite/MainContent". This means that clicking the Back button on the browser doesn't work as expected, and also hitting F5 to refresh the browser page loses the currently displayed page. The refresh issue can probably be resolved by remembering the selected menu item, but I'm still not convinced this isthe way AJAX was intended to be used.
Can someone please tell me if they think caching would be a better solution for this scenario based on their experience.
If the user's menu items are not likely to change, then you could use a simple static Dictionary (the long is the user id) to cache the data in the backend, and refresh it periodically.
Generally, if you do any caching you have to make sure the cached data gets refreshed when underlying data is updated. Therefore it's only simple and safe if you have only one app accessing your DB. If there are multiple apps, then you'll have to add a mechanism to keep the cache in sync.
In project I'm working on we've stored User-Role-Module entities and relationships in DB, and bound module permissions to roles rather than users. Since role permissions aren't likely to change, they're safe to cache.

New Instance of the page on Navigation

I have a few pages in an Application that require A-Synchronous calls to be made for about 2-3 minutes to get Synchronized, the user may navigate away from that page during Synchronization and can come back again after visiting multiple pages and the sync continues all the time he is on other pages as well, when I go to a page from sync-page and press the Back button everything works fine.. but when i go to a page and navigate back to sync-page from Application Bar a new Instance of the Page is created and the Sync is just like Re-started.
Now i know every thing is working fine since new instance of a page is created when i call NavigationService.Navigate() , but what should i do in this scenario ? How to get the old instance of a page if it is there ?
Thanks...
You can't get an "old" instance of a page and it's not guaranteed that a backwards navigation will reload the previous instance of the page, it may be a new instance of the same page, but restored to the same state (assuming you saved any).
If you are trying to provide backwards navigation from the application bar then a) you probably shouldn't because that's what the back button is for, and b) you should make sure you use NavigationService.GoBack() instead of NavigationService.Navigate() because Navigate will always launch a new instance of your page.
If the page you want to get to is not the previous page, then it sounds like you are trying to implement non-linear navigation for which there is a recipe on the App Hub.
By the sounds of your scenario, you should handle this long running process separately (away from the view) and then display it's progress or results in a view when the user navigates to the relevant page.

Using MVVM Light messaging instead of query strings in Windows Phone

I am trying to use MVVM light messaging to send a value from one page to another during Navigation (for example, send the id of an item that was selected to an edit page). So the list page's viewmodel sends a message and then sends a navigation message to the view which redirects to the edit page. The edit page's viewmodel gets created only when the navigation to the page happens. So when I register for this event in the edit page viewmodel, I never get the message? What is the best solution for this?
Thanks in advance.
Your best solution would be to use the querystring instead of messaging. If you don't use the querystring, you'll have to deal with situations like the application being deactivated (tomestoned), then the user clicking "back" and your application loads the second page without receiving the message.
However, if you want to continue down this path, you can modify your ViewModelLocator such that your page's ViewModel is created immediately (in ctor for instance) instead of as needed. Since the ViewModelLocator is created as soon as your App.xaml is loaded, you know that any view models will be created immediately. As long as your view model is registering for messages in it's constructor it should receive the message.

Resources