What happens in the OnAppearing of Xamarin Forms? - xamarin

I have a Forms application which works good but I notice the background colors appear set incorrectly for a fraction of a second as the page appears.
I have this code for my OnAppearing
protected override async void OnAppearing()
{
base.OnAppearing();
Subscribe();
vm.Theme = Settings.th.ToString();
await SetCardButtons(Settings.cc.Text());
}
Can someone explain what happens in the base.OnAppearing() and should this be the first or last line in my override?

what happens in the base.OnAppearing()
OnAppearing is a virtual method in the Page base class, that does not contain any code (but that does not mean it does not do anything).
Depending upon which Page subclass you are using, different things might be performed, like the MasterDetailPage performs logic to determine if it should be displayed as a splitview (side-by-side) mode, the native iOS code would do this on an iPad, ....
should this be the first or last line in my override?
Normally this would be called first in your override to allow any custom subclass code to setup the UI properly.

Related

Navigation between views Xamarin Forms

I have simple Click event and want to navigate between views, but I get annoying slide up animation even that I have "false" as a parameter for animation in function. Is there any way to turn of animation effect when browsing between views in Xamarin Forms?
Thank you!
void Button_Clicked_Signup(System.Object sender, System.EventArgs e)
{
Navigation.PushModalAsync(new Signup(), false);
}
Your app is doing exactly what you're asking it to do. That annoying slide up animation is called Pushing a Modal which is happening since you are using the PushModalAsync function.
You need to the documentation and understand Hierarchical Navigation.
To fix it, you need to
Create a NavigationPage first
Pass in your current page into that
Use Navigation.PushAsync instead of Navigation.PushModalAsync

Odd behavior on orientation change with a listview

Not sure why this is happening, but (sometimes) when I change the orientation of a device, this is the output I get:
Only appears to be happening on iPad. Not iPhone. Any insights?
Could be a bug in the binding of a ObservableCollection to a ListView.
You could force a refresh in the ListView by overriding the OnSizeAllocated of the current ContentPage.
protected override void OnAppearing()
{
base.OnAppearing();
ExampleListView.ItemsSource = null;
ExampleListView.ItemsSource = ObservableCollection;
}
Ended up navigating back to the same page to avoid this. Seems something in my code or perhaps a limitation in Xamarin, I have a complex set up for this so that's possible.

Activity.RunOnUiThread() doesn't update view

There is activity, fragment and a viewmodel binded to them both. Fragment has event which gets raised multiple times by Timer, delegate inside fragment is subscribed to that event and updates single UI element with value from a viewmodel.
Delegate:
private void OnTimerUpdate(object o, EventArgs e) {
this.Activity.RunOnUiThread(() => {TimerTextView.Text = ViewModel.TimerValue;});
}
It is called multiple times as planned, however ui element is not updating, unless I'm interacting with a View (e.g. scrolling).
Previously both ui element and delegate was inside activity and was working properly, however when I refactored it to have separate fragment, this happened.
Have you tried calling TimerTextView.Invalidate() after changing the text?
Found answer here. Which I thought was weird, but actually worked - set width and height of textview I'm updating to some values instead of 'wrap_conent'.

How to make a seamless transition between WP7 splashscreen and first page with same splashscreen

I'm developing a Windows Phone application. When I launch, the splash screen is shown very shortly, and the MainPage.xaml is shown. However, in the MainPage, I setup the camera with the usual code:
if (PhotoCamera.IsCameraTypeSupported(CameraType.Primary))
{
_photoCamera.Initialized += OnCameraInitialized;
// And other event handling
viewfinderBrush.SetSource(_photoCamera);
}
This is simplified, but it all works. My problem is that this takes a while (mabye 0.5 - 1 seconds, I didn't time it exactly).
So what my user gets is a splashscreen that's so fast, he/she can't see it; and a first page that takes just that tiny fraction to experience it as slightly laggy/slow.
I'd like to change it. Have the splashscreen show until everything is initialized. This has the added benefit of branding for me, and a nice experience for the user.
I've tried this:
Add my splashscreen as image into my MainPage, on top of everything else and hide it when everything is initialized
Add my splashscreen in a popup on my MainPage, and hide the popup when everything is initialized (found that here)
This 'works', but I can see a black flash between the splashscreen and my image/popup. Is there a way to make this transition seamless? Or is this fairly normal behavior in WP7?
Your first option should work - but understand that in one trip through a UI-thread method, the UI doesn't actually update until all the code is executed. So break it up into pieces.
1) Load your MainPage.xaml, which has the splash image filling the screen by default
2) Add an event handler for both OnNavigatedTo and LayoutUpdated. When OnNavigatedTo is hit, set a flag to true. In LayoutUpdated, check for that flag to be true, set the flag to false, then run a Dispatcher.Invoke() call on the method you described above.
3) Remove the image or set it to collapsed after that method is completed.

ListViewItem ItemSelectionChangedEvent Fires 4 times [e.Selected fires twice] leads to Win32 Exception Unhandled

I am using a button and a listview to display a list of options to the user. Selection is made with a mouse click, the listview removes its self from the .Controls array + un-registers eventlistener and loads a new listview else where on the screen.
My problem is both listviews trigger e.selected twice:
' private void _lvKids_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
{
if (e.IsSelected)//fires twice per click
{
HideKidsList();//--REMOVE CURRENT LISTVIEW
ValidateUser();//CREATE NEW LISTVIEW
}`
If the button is clicked a second time to restart the process, it causes a win32 Exception. After much research, this exception is the often the cause of a memory leak. So I'm thinking memory leak?
When I first started, listboxes were used which worked perfectly. I'd love to able to use them, but my form has a graphic for a background and listbox doesn't. Listview does.
I don't have anyone to turn to so any thing you can offer would be appreciated.
Thanks;
Sam
An update if anyone else has the same issue. Selecting the listview item called for it to be removed from Controls array. Removing the listview also cause the selected item to be deselected, thus 4 calls to the handler.

Resources