How do I scroll back to top in Xamarin.Forms - xamarin

I have a WebView control in RelativeLayout. Sometimes Html is longer then display, so user can 'scroll' to the bottom. I want to add a button that would scroll back to top of the screen. But I'm not able to find any proper way to do this.
This button would also float at the bottom, that's why I'm using relative layout.
WebView neither RelativeLayout have property where I could scroll to top in my code-behind.
Is there any way to implement this?

You could use the WebView.Eval method. It's very simple, something like this would do:
MyButton.Clicked += (sender, e) => { MyWebView.Eval("window.scrollTo(0, 0)"); };

Related

Is there an event name for this action?

Is there a ScrollView event in xamarin that is triggered when the user forces scroll up and scroll is already at top? Many apps uses this as the "command" to update the page.
Currently, there is no PullToRefresh property or RefreshCommand for a ScrollView built into Xamarin.Forms. However, It does exist on ListView as shown in the docs, which you might be able to use instead depending on your needs.
Also, it does look like there is a PullToRefreshLayout opensource project created by James Montemagno you might be able to use to easily implement pull to refresh with a ScrollView, but it has been a while since it's last been updated.
You can use the Scrolled event handler. If the ScrollView is already positioned at the top of the contents and the user "pulls down" then Y amount will be either 0 or negative (I know this will work on Android and iOS, not sure about UWP and others).
scroll.Scrolled += (object sender, ScrolledEventArgs e) =>
{
if (e.ScrollY <= 0)
{
// scrolled when already at top of list
}
};

Xamarin.Forms MasterDetailPage with part of master visible on detail

I want to push (make visible) little part of master page to detail page like on pictures in links below. Is it possible in Xamarin.Forms?
Before swipe
After swipe
I'll be grateful for help.
Set
<ContentPage NavigationPage.HasNavigationBar="False">
Place a StackLayout or Grid that looks like the side menu shown in your image. Add a tap gesture recognizer to it.
slMenu.GestureRecognizers.Add(new TapGestureRecognizer
{
Command = new Command(() => ShowSideMenu(masterPage)),
});
private void ShowSideMenu(MasterDetailPage masterPage)
{
masterPage.IsPresented = !masterPage.IsPresented;
}
This is possible, but requires quite some work, since you can't access the width of the MasterDetailView directly.
In another post (see How to set width for MasterDetailPage in xamarin forms ) I have described how to change the width of the MasterDetail view.
While implementing that, I came across a behaviour like you want to have, though for me it was an error.
Basically the steps would be
Implement a custom version of the MasterDetail view where you can set up a custom width
Change the width of the master view so it is a bit wider than it should be
Adapt the calculations when collapsing and expanding the view so that they don't collapse the entire width
For instance if your Master View gets a width of 340 and you want the last 40 to show, make sure that when being collapsed it "only" moves by 300

Xamarin Forms: Hide a Stack While Scrolling a list

I have a Xamarin forms Page which is divided into 2 sections, the top 40% contains the Search controls inside a stack layout, the other 60% contains a list view. While scrolling the list view up I want to hide the Stack Layout at the top covering 40% of the screen.
I achieved to implement scroll events of the list from below reference:
https://github.com/velocitysystems/xf-controls/blob/master/XF.Controls/XF.Controls/Views/ListView.cs
I am able to hide the stackLayout but there is a lot of flickering. Any idea how can I stop the flickering? the output is not crisp and stable.
You can use animation TranslateTo for StackLayout.
Example:
OnScrolled()
{
Device.BeginInvokeOnMainThread(() => {
if (isScrollToUp)
HeaderStackLayout.TranslateTo(0,-500);
else
HeaderStackLayout.TranslateTo(0,500);
});
}
You could put the search controls as the header of the ListView:
https://developer.xamarin.com/guides/xamarin-forms/user-interface/listview/customizing-list-appearance/#Headers_and_Footers
As the header is a part of the ListView itself it will scroll as expected once you have enough content to scroll through.

Xamarin Forms label animation at bottom of screen

I'm essentially making a 'shopping cart' UI and I want it so that when the user hits the 'Add' button, a little tiny box-label appears at the bottom of the screen that says 'Added Item' or something like that.
My question is how to do that with my current set up. I am currently using a nested Grid inside of a Scroll view for the main content of the page. I want the box-label to fade in at the bottom of the screen and stay located at the bottom of the screen ontop of everything else even if you scroll, until the animation fades.
Now i figure it doesn't make sense to add it into the grid since the grid's end will be out of view in the scrolling part of the scroll view, and same for the Scroll View. I am considering nesting the entire scroll view inside of a stack layout but i fear the button will just be located at the end of the stack layout under the scroll view instead of on TOP of the scroll view. How do you recommend I achieve this effect?
I prefer not to use a custom renderer if possible due to my lack of experience in the three separate platforms.
Thanks
Make vertically oriented stack layout. When you need to add you animation add it programmatically to the stack. When it finishes remove it from stack. Your scroll view will not affect animation

change back icon in toolbar inside Xamarin FormsPage

Using xamarin forms (PCL),
I searched a lot but Can not find the exact solution,
Can i change the back button in toolbar or at least should i be able to change its color and the title color as well?
I am not sure for changing back button, but I was changing back button and title color.
An example, in App.cs file OnStart method I set that LoginPage is first page with black title, on this way
MainPage = new NavigationPage(new LoginPage()) { BarTextColor = Color.Black };
Please try something like this, I have some other examples if this one doesn't work.

Resources