How to obtain Android native StackView from Xamarin.Forms.StackLayout? - xamarin

I need to obtain the native Android StackView control from Xamarin.Forms.StackLayout instance. Here is some pseudo-code to explain the problem:
void ProcessMySL(Xamarin.Forms.StackLayout sl) {
var renderer =
Xamarin.Forms.Platform.Android.RendererFactory.GetRenderer(sl);
...
}
When I examine renderer object, it is of type Xamarin.Forms.Platform.Android.VisualElementRenderer. However, it does not have any property such as Control to get me the native StackView object.
I am wondering how can I obtain the native view. Regards.

Xamarin.Forms.Platform.Android.VisualElementRenderer. However, it does not have any property such as Control to get me the native StackView object.
Refering to the official document of Renderer:Renderer Base Classes and Native Control. StackLayout is using ViewRenderer and is thus rendered into a View object, not a StackView.
Besides, using Xamarin.Forms.Platform.Android.RendererFactory.GetRenderer(sl) only return a DefaultRenderer, which doesn't expose the Control property. So, please create a custom renderer instead.

It appears Xamarin.Forms.StackLayout in not rendered as Android's StackView control but a more generic ViewGroup object. To get the Android native view, the following code would work:
var renderer = RenderFactory.GetRenderer(stackLayout);
Android.Views.View view = renderer.ViewGroup;
...

Related

How can I replace the SearchBar icon in a Xamarin.Forms custom renderer for GTK?

I am working on a Xamarin project that includes a build for GTK. I am attempting to create a custom renderer for many of the Controls, but am having trouble finding, accessing and changing the properties for the control. For example, I would like to replace the "magnifying glass" icon for the SearchBar control with something more similar to the default icon on the Android platform.
I've created the custom renderer:
namespace MyProject.GTK.CustomRenderers
{
public class CustomSearchBarRenderer : Xamarin.Forms.Platform.GTK.Renderers.SearchBarRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<SearchBar> e)
{
base.OnElementChanged(e);
var searchBar = Control;
// How do I replace the image?
}
}
}
but from there I am at a loss as there are practically no resources on custom renderers for GTK. I've tried looking at the GTK.Renderers.SearchBarRenderer to see if the class its derived from contains any useful properties or methods, as well as trying to find something meaningful in the GTK documentation and the repository for the Xamarin.Forms.GTK package, to no avail. I'm just not really sure how to understand the inner workings of the controls in this build so I can't figure out what I should even be looking for. Any pointers or resources for this or any GTK specific custom renderer work would be much appreciated.
You can check Xamarin Forms GTK
SearchBar is implmented by the use of element called SearchEntry which uses ImageButton and the icon is set by below code
_searchButton.ImageWidget.Pixbuf = RenderIcon("gtk-find", IconSize.SmallToolbar, null); // Search icon
Refer
SearchEntry.GTK
SearchBar.GTK
This should help you begin modifying, if you can get access to SearchEntry in your custom renderer you can change icon, otherwise you will have to create your own search bar, which takes lot of effort.

RadListView scrollPosition property for android

I'm looking for a workaround for using RadListView's scrollPosition property in Android, as it is available only in iOS.
Trying to look for some properties in android's RecyclerView widget docs
Any insights?
Check this answer: https://stackoverflow.com/a/38248034
Since RadListView code is closed source, you'd probably have to inspect it to check what classes it's using underneath. As it extends RecyclerView, you could try radlistview.android.getLayoutManager(), find out which class it is and call the equivalent method to get the view id (each LayoutManager has it's own findFirst...() method).

How to render views in compile time in Xamarin.Forms

I'm trying to render some controls myself in the compile time. Meaning that i'm trying to create for instance a Xamarin.Forms.Button and get it's native Windows.UI.Xaml.Control.Buttonimage (or FormsButton to be accurate)all in compile time.
I was trying to apply this on a ContentView so by applying this theory, this is the code to accomplish that:
var contentView = Element.Content;
var renderer = contentView.GetOrCreateRenderer();
var nativeControl = renderer.ContainerLayout;
By doing so i get a native UWP FrameworkElement with all the properties of the ContentView like the background and so on. However, i don't get the children rendered. That actually makes sense because basically you render just the ContentView. But i have no idea how to render all it's children. Hope that the solution applies to any XamarinForms control that has a Children (or a Content) property

Xamarin forms ListView inside ScrollView issue

In my Xamarin forms application, there are multiple ListView controls inside a ScrollView. But in android the scrolling is not working for ListView. Is there any alternative solution?
You SHOULD NOT include ListViews into ScrollView as it system will confuse scrolling behavior of those two.
You need to redesign your page with this in mind.
Example:
1) Use ListViews inside StackLayout
2) Use TableViews inside ScrollView
You can simply do with set 'NestedScrollingEnabled' property true for native side.
For xamarin forms you can create a custom renderer and set 'NestedScrollingEnabled' property true
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.ListView> e)
{
base.OnElementChanged(e);
if (e.NewElement != null)
{
var listView = this.Control as Android.Widget.ListView;
listView.NestedScrollingEnabled = true;
}
}
ListView implements its own scrolling that might conflict with the ScrollView.
If you need to be able to scroll both lists in the same ScrollView you could for example create custom views (instead of using cells), placing them in a StackLayout inside a ScrollView
More about ListView performance, they even explain why you shouldn't place a ListView inside a ScrollView
Have you looked into using one listview with groups instead of multiple listviews?
I use any row of grid it will fix to that height & listview will not scroll

Xamarin set content view width to root view

I'm using Xamarin Auto Layouts with Storyboard on Visual Studio 2015.
I have a root view, Scroll View and Content View.
I want the application to only scroll on the y-axis but scale on the x-axis.
I want to do something like this.
Adding a view to a scroll view that will stretch to fill available width
Which is setting the width of the content view to the root view.
But using Xamarin this does not seem possible.
So in this example, if I run the application, I have to scroll to the right to see the right most label. I want all three labels to show up on the iPhone, by scaling down on the x-axis.
I have also tried using the "I" handle on the content view in storyboard UI Designer of Xamarin VS, but it doesn't identify the root view.
If it's not possible using storyboard, I will settle with code.
I have tried this.
public override void ViewDidLoad()
{
ContentView.Frame = new CoreGraphics.CGRect(ContentView.Frame.X, ContentView.Frame.Y, View.Frame.Width, ContentView.Frame.Height);
//ScrollView.TranslatesAutoresizingMaskIntoConstraints = false;
base.ViewDidLoad();
// Perform any additional setup after loading the view, typically from a nib.
}
But it doesn't do anything to help me.
How can I scale the UI down on the X-axis while on top of a scroll view using Xamarin.ios?

Resources