OnScroll Event in ListView using Xamarin Forms - xamarin

I have a listView and when the receive a message, the listview should scroll to the end only, if it already in the end.
To make it clear, if the user is seeing old message, the listview shouldn't scroll to the end automatically, but if the is already on the on the listview when receiving a message the listview should automatically scroll to the end.
So I have to get the actual position of the ListView, I didn't see any property or method similar to get this position on the API.
So I was thinking if is possible to override an event like OnScroll, so I can save the current position of the ListView without using custom render.
This can be done using XamarinForms?

ListView doesn't fire Scrolled events like ScrollView does, but it does fire ItemAppearing and ItemDisappearingevents that you may be able to use to keep track of which items are currently visible.

Related

MAUI CarouselView: how to imitate swipe effect in code? Swipe animation does not happen

.NET Maui CarouselView. In certain situations I want my app to take the user to the next card automatically. If I update CarouselView.Position or CarouselView.CurrentItem in code behind, it "jumps" to the next card immediately, no animation. Is it possible to imitate user's swipe? Or as a workaround, maybe somehow apply non-native-CarouselView animation manually to the CarouselView. Please advise.
The CarouselView contains a ScrollTo method that will animate the scroll for you. You either scroll to an index or a specific item.
Give your CarouselView a name in the XAML, and then in the code behind call the ScrollTo.
To scroll to an index:
carouselView.ScrollTo(6);
To scroll to a specific item:
var viewModel = BindingContext as MyViewModel;
var item = viewModel.Items.FirstOrDefault(m => m.Name == "TheBest");
carouselView.ScrollTo(item);
These methods have to be called from the code behind, so if you're using a MVVM approach, you'll need to fire an event or command from your VM for your code behind to act on.
For additional info, take a look at the ScrollTo method docs from Microsoft.

multiple ScrollView not working on xamarin forms

Multiple ScrollView is not working on my content page Xamarin forms.so how to solve this?
i added two scrollviiew in my content page,but the second one is not scrolling.
Do not put a ListView inside a ScrollView.
Avoid placing a ListView inside a ScrollView for the following reasons:
The ListView implements its own scrolling.
The ListView will not receive any gestures, as they will be handled
by the parent ScrollView.
The ListView can present a customized header and footer that scrolls
with the elements of the list, potentially offering the functionality
that the ScrollView was used for. For more information see Headers
and Footers.
If you need more ListView on some Page you should use one ListView with Group.

WebBrowser as ListItem and scroll

This question about WP8 project.
I have a LongListSelector with pictures and html formatted text below. Currently each pictire implemented as an Item of the list. The last Item with browser has its own template and shows WebBrowser instead of picture.
I am expected this item behave such as TextBox which I can scroll by interacting with LongListSelector.
But this WebBrowser has it's own scroll inside and intercepts any surrounding scrolling events. Thus whenever I scroll down the list to show WebBrowserControl to the whole screen, I can't go back to the pictures.
Please help me with this issue. What is the appropriate solution? I need a separate pictures since want to implement "Clicked" logic on top of it. And I want to have WebBrowser too (but prefer to behave it similar to RichTextBox (which I can't find in WP8))
USE ListBox.ItemTemplate along with DataTemplate and inside dataTemplate we could custom create xaml tag !

Toggling PanoramaItem Visibility via Delegate

I have a peculiar problem in Windows Phone Development. I have 4 panorama items each of them containing a webBrowser control. On the start of the application, I have only the first panorama item visible while the remaninig are in collapsed state.
Based on the interaction in the first webBrowser, we Notify the WP7 app (webBrowser.ScriptNotify event) and decide which panoramaitems to display. The visiblity is set in the delegate that handles the ScriptNotify event.
the issue I am facing is that though i set the visibility in the delegate to Visible, it doesn't show up in the Panorama. I have tried using Dispatcher in the delegate to change visibility but it hasn't helped:
Deployment.Current.Dispatcher.BeginInvoke(() => {
discussions.Visibility = System.Windows.Visibility.Visible;
});
Can someone suggest what i could be doing incorrectly?
First of all, you shouldn't use a WebBrowser control inside a Panorama. It's very bad performance.
Secondly, Panorama and PivotItems don't have a collapsed state.
And thirdly, the dispatcher have nothing to do with this (unless you're not running the code on the UI thread).
So what you need to do, is to add the PanoramaItems dynamically to the Panorama control. This can be done by databinding (recommended), or directly from C#.
I don't know about performace but i'm sure that a PanoramaItem has a collapsed *visibility* state i tried to toggle it from code and it works like a charm if the initial state is visible.
But if the initial state is collapsed when the panorama loads then it doesn't work anymore. I guess it is because if it is collapsed then it is not included in the panorama and so it won't be visible when you set it to visible.
Maybe that's a bug or i don't know but it is a bit awkward.
To add the PanoramaItem to the Panorama could work.
I'm sorry for not answering the question but the other way around, enforcing the problem :) .
I do have the same problem, one PanoramaItem with an ItemsControl databounded to a collection of the ViewModel. The PanoramaItem visibility property is databounded to {Binding} and a CollectionToVisibility converter is used. During debug with a breakpoint set inside the converter code I managed to see that the return value is ok but the PanoramaItem is not visible when the collections has items.
My investigation took me to realize that in fact when the first get to the Collection occours the return value is null because the Collection data comes from a async call to a service and the converter return value is Visibility.Collapsed, and only when the Collection is populated and the PropertyChanged event is raised, the second get to the Collection property triggers the databind refresh and the return value of the converter is now Visibility.Visible, this leeds me to think that the PanoramaItem isn't included in the Panorama control tree during the applytemplate because the visibility is set to collapsed, and after that the UI never loads the PanoramaItem again.
I made a test to verify this scenario returning in the get of the Collection property an hardcoded list of items so that the first get have items in it and the converter returns Visible in the first get request.Everything works like a charm.I can even string the collection out of items and it gets collapsed and the other way around.
All this is pure Xaml, no code behind.
The intent of this is to hide PanoramaItems that for some reason does not have content to show.
Basically, the panorama keeps an internal list of visible item. This list isn't updated when you set an item to visible. The easiest way to force the control to update this list is setting the MainItem property. So after setting the visibility of your panorama item, just add the following line:
yourPanorama.DefaultItem = yourPanorama.DefaultItem;
(given that the panorama is called yourPanorama)

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