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
Related
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.
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;
...
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?
I am trying to figure out a correct composition of views and subviews in Xamarin.Mac (monomac) application.
There are very little examples of Windows/Background View/Controls UI with Xamarin.Mac out there and those which I found (basically this project is the best documented one when it comes to Window/View composition) suffer from the same problem - its a static binding of a form Window -> Background view (bound in XCode) -> Controls under the View node. What's being displayed as controls on the view isn't dynamically added via C# and not even a separate view inside the background view.
What I need, is to create a Window, that has a background view that will be semitransparent and then have my application dynamically, depending of the app logic, inject different subviews to background view from C# code. I know that one can add a subview to a view in Xamarin.Mac, but when I try it in my case, the subview just isn't being shown:
GoFullscreen is where the window starts to transform:
private void GoFullScreen ()
{
// Set the window level to be above the menu bar
Window.Level = NSWindowLevel.Normal;
var backgroundView = new LeoBreakBackgroundView ();
backgroundView.SetBreakSubview (_currentBreakViewController);
Window.ContentView = backgroundView;
Window.ContentView.EnterFullscreenModeWithOptions (NSScreen.MainScreen, new NSDictionary());
// Show the window
Window.MakeKeyAndOrderFront (this);
}
At the point I call backgroundView.SetBreakSubview (_currentBreakViewController);
which is:
_currentBreakViewController = currentBreakController;
this.AddSubview (_currentBreakViewController.View);
I don't see the _currentBreakViewController.View being added to backgroundView, but the backgroundView is indeed added onto a window that goes fullscreen.
_currentBreakViewController is a NSViewController and LeoBreakBackgroundView you see in the first snippet is just an NSView without a controller.
Anyways, is it possible to control a View's subview dynamically from code in Xamarin.Mac?
I thought that layout is just a widget that keeps more widgets inside.
But now I find that I can't add it to other widgets using addWidget.
For instance how to add a layout to QSplitter?
QWidget has built in support for layouts through the layout() and setLayout(...) functions. The layout object controls the positioning of different child widgets that may be added to the layout. In addition, it makes sure to correctly size its parent widget based on the constraints specified. If the layout does not yet have a parent widget, then as soon as the layout is attached to a widget with setLayout(), it will set the size of that parent widget.
But, some widgets are more like a layout manager than a widget, such as QSplitter and QTabWidget. Consider, for example, QSplitter. Although a single widget, it presents two different areas that may be worked with. In this case, a single layout manager for two different areas doesn't really make sense. Like QSplitter, QTabWidget has some behaviors which make a single layout not only unnecessary but not useful.
I think it's the above melding of layout and widget that makes the separation of layout and widget sometimes confusing.
No, a layout is not a widget or a container. A layout is more like a "helper" that's attached to a window and figures out the best place to put each widget.
This example from the Qt docs should help (http://qt.nokia.com/doc/4.2/layout.html):
QWidget *window = new QWidget;
QPushButton *button1 = new QPushButton("One");
QPushButton *button2 = new QPushButton("Two");
QPushButton *button3 = new QPushButton("Three");
QPushButton *button4 = new QPushButton("Four");
QPushButton *button5 = new QPushButton("Five");
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(button1);
layout->addWidget(button2);
layout->addWidget(button3);
layout->addWidget(button4);
layout->addWidget(button5);
window->setLayout(layout);
window->show();
QLayouts are layout managers. They manage the positioning and resizing of widgets inside a parent widget. From the docs:
Layouts are an elegant and flexible way to automatically arrange child widgets within their container. Each widget reports its size requirements to the layout through the sizeHint and sizePolicy properties, and the layout distributes the available space accordingly.
For example, setting the layout of a widget to QHBoxLayout will result in its child widgets being laid out horizontally.
You can read more about it here.