Xamarin Forms Custom Control using renderer - xamarin

I would like to create custom controls using xamarin android visual studio. I have created this as sample project using xamarin forms and it works fine: http://blog.falafel.com/learning-xamarin-custom-renderers-in-xamarin-forms/
Now my req is I want to create something like:
Above control I have created using fragment - android native controls. I want to create now exactly same in Xamarin Forms using Renderer.

The following link is a great tutorial to get you into how to write custom renderers, along with the code sample here.
If you follow the essence of there example you will see how to write an Android renderer and be able to test it, however for what you are trying to do you have to make some alterations.
You will want to specify the assembly ExportRendererAttribute to expose your custom renderer and allow this to be registered for use. This will then make your class implementation callable when it is encountered rendering a Xamarin.Forms page.
As you are creating a composite control you will want to derive also from ViewRenderer<> passing your custom View that is shared across all platform implementations, and the base control that you will use for hosting your new composite control (look at your sample composition that you already have done and take the top-most element).
Unlike the example in the link you will not override the Draw method as you are not going to be painting directly onto the Canvas. Instead you will create your layout following what you have already done, but you will place this code within the override OnElementChanged().

Related

Xamarin Drag and drop with a drop zone

I am trying to implement drag and drop to create a grammatical exercise in which you have to complete the sentence with the right word, dragging the right answer in the right spot (as shown in figure below).
I tried to achieve that by using custom renderer, creating some extended native views that override the methods to implement their dragging and dropping (like OnTouchEvent for Android, and TouchesBegan, TouchesMoved, TouchesEnded for iOS). In both platform I am able to drag the custom native views, but only in iOS I am able to drop them into the specific destination view that I want. In Android it seems to be a conflict with Xamarin Forms view coordinates and the native Android coordinates used in the android custom renderer. Even if I try to "adjust" them, (by manually inserting the coordinates that I want the view to be dropped in) it seems like that the check if(destinationRect.Contains(myTouchX, myTouchY) doesn't work proper.
Git repository example here: Git example
Does anyone know how to solve that?
Or does anyone know how to implement drag and drop (with a proper dropzone to check) in any other easiest way?
In Android, it provide the View.StartDrag(ClipData, View+DragShadowBuilder, Object, Int32) Method method to start a drag and drop operation.
You could check the MS document.
View.StartDrag(ClipData, View+DragShadowBuilder, Object, Int32) Method:
https://learn.microsoft.com/en-us/dotnet/api/android.views.view.startdrag?view=xamarin-android-sdk-9
View.StartDragAndDrop(ClipData, View+DragShadowBuilder, Object, Int32) Method:
https://learn.microsoft.com/en-us/dotnet/api/android.views.view.startdraganddrop?view=xamarin-android-sdk-9
Based on my search, this blog would be helpful for this method.
https://pumpingco.de/blog/adding-drag-and-drop-to-your-android-application-with-xamarin/
You could download the source file from the GitHub.
https://github.com/Pumpingcode/Xamarin-DragAndDropDemo

EventHandlerBehavior vs EventToCommandBehavior

In Xamarin Forms what is the difference between the EventHandlerBehavior and EventToCommandBehavior? I have been using the former successfully, but the latter was suggested to me to use instead. EventToCommandBehavior is less verbose which is a good thing.
About difference between EventHandlerBehavior and EventToCommandBehavior, I have some point, you can take a look:
For EventHandlerBehavior, you need to install Behaviors.Forms in your project firstly, behaviors let you add functionality to controls without having to subclass them. Instead, the functionality is implemented in a behavior class and attached to the control as if it was part of the control itself. Behaviors enable you to implement code that you would normally have to write as code-behind, because it directly interacts with the API of the control in such a way that it can be concisely attached to the control and packaged for reuse across more than one app.
For EventToCommandBehavior,the EventToCommandBehavior is a custom class, there are need you create EventToCommandBehavior class derives from the BehaviorBase class firstly, then implementing Bindable Properties, for detailed info, you can take a look:
https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/behaviors/reusable/event-to-command-behavior
So I suggest you can use EventHandlerBehavior, because you don't need to create many code behind.
2021 Edit
EventHandlerBehavior from community-toolkit is now equivalent to EventHandlerBehavior.
Documentation:
https://learn.microsoft.com/en-us/xamarin/community-toolkit/behaviors/eventtocommandbehavior
In Xamarin Forms what is the difference between the EventHandlerBehavior and EventToCommandBehavior?
Eventhandler like:
OnTextChanged, OnSelectedIndexChanged etc are defined behind the C# code file of XAML. These methods are completely tied to your controls you define behind your pages/controls.
However the Behaviors in Xamarin allows us to ATTACH them to any View. Your behavior will know which controls it is being attached to. Hence, for those Events like OnTextChanged/OnSelectedIndexChanged can be controlled from the Behaviors.
EventToCommandBehaivor: As we already know that Behavior get attached to any view; you have to define the behavior as such that It will expose the BINDABLE property for Commands. EventToCommandBehavior is just a normal behavior control which supports binding the Command and the Eventname. Which in runtime, whenever the Event for the control get fired, your behavior will execute the Command attached making it more ViewModel friendly.
Remember if you use Events; you are writing them behind the code base than ViewModel your logics are in two separate places. If you want to properly separate the logics from View and Model then then you have to use EventToCommandBehavior.
Let me know if you need more info.

Does Cocoa have something like WinAPI's DrawThemeBackground (a function that draws native controls)?

Windows API has very powerful functions that allow to draw native controls in all states:
DrawThemeBackground()
DrawThemeText()
Here's a really nice example of their usage:
Windows Visual Themes: Gallery of Parts and States?
Does Cocoa have something similar?
Apple has "soft deprecated" NSCell and the various control-specific subclasses, but you can use them for this. You create one, configure its properties, and then tell it to draw in a frame in a view.
Getting the properties just right can be tedious. You might want to create a control of the relevant kind in a test project's view (using IB) and then interrogate its cell's properties programmatically. And/or configure such a control to use a custom subclass of the corresponding cell class which logs which methods are called on it with what parameters in various scenarios.

What is imgMain control in visual Basic 6?

I'm using a VB6 application as a reference and have come across imgMain. I'm assuming its an image control, however, I cannot find the object on any of the forms. It's used to load the image. I'm just not sure what's going on with this. I'm assuming its an IMAGE CONTROL, but I cannot find this on any of the forms? it's mentioned a lot of times in teh application as it lets you view incoming faxes and stuff.
Without having a look on source code i assume It might be custom user control that was created for some reasons probably to extend basic picturebox control.
One thing i can advise you is to further inspect code and dig to code of this custom control. If code for this control is not available then see what methods and properties instances of that particular control uses and compare them with regular picturebox.

Codedom at runtime

I am building a code generation tool using vs 2010 visualisation & modelling sdk. One of the things that I need to generate is a simple data capture ui (windows forms, preferably using devexpress layout control), with capture fields & labels for each property in a class.
I can use t4 text templates to do this, but I am conscious that I will be re-inventing the wheel if I do this - a simpler approach would be to add controls to a form constructed at runtime and allow the appropriate codedomseriaizer to generate the form.designer.cs code for me - is this possible?
To summarise, I would like to build a form at runtime, and retrieve a string of the designer.cs file..
Graham
Could you not simply leave the runtime generation code in use, and design only the other parts of the form using the designer? This way the autogenerated controls will be automatically updated if new properties are added to the class.

Resources