Xamarin Drag and drop with a drop zone - xamarin

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

Related

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.

NSCollectionView finished drop event

I'm working on the app that displays svg icons inside NSCollectionView. I want to implement drag&drop feature to allow users drags svg to Finder or another app. There are I have two questions:
1.) To implement drag to another app I'm using NSFilenamesPboardType for registerForDraggedTypes and in collectionView:writeItemsAtIndexes:toPasteboard: create temp file and write it URL to [pasteboard writeObjects:#[fileURL]]; because collectionView:namesOfPromisedFilesDroppedAtDestination:forDraggedItemsAtIndexes:not called in this case. Is it correct implementation? I cannot find how to implement drag&drop for svg in another way.
2.) How I can receive callback that drop ended? NSTableView in data source has method tableView:draggingSession:endedAtPoint:operation which determines end of drag. For NSCollectionView this method not documented, but exist in NSCollectionViewDelegate. Can I use it and app will pass the review?
Thanks for any advice
There is an NSCollectionViewDelegate method that was available with 10.7:
- collectionView:draggingSession:endedAtPoint:dragOperation:
It calls the method AFTER the item is dropped to receiving application.
collectionView:namesOfPromisedFilesDroppedAtDestination:forDraggedItemsAtIndexes: will be called if you use NSFilesPromisePboardType. You create the files after the drop. If the other application doesn't accept promised files, this won't work.
It looks like Apple forgot to add the new drag and drop delegate methods of 10.7 to the documentation of NSCollectionViewDelegate. You can use them.

Add a second UIViewController within a single-view application

I have a single-view application open. I need to have two storyboard views (UIViewControllers), because one is a Table View, but when one of the Table View elements is selected it brings up another screen with a normal UIViewController. Is there any way of creating a second storyboard view (UIViewController)?
Of course! Those templates Xcode provides are in no way "set-in-stone". When Xcode created your Single-View Template, it just gave you a base for creating something more. You can create additional classes, view controllers, views, and resources (other than what Xcode starts you off with in a few ways).
To add another View Controller to your Storyboard, just drag and drop one from the Object Library:
It seems like you do not have a basic understanding of using or developing with Xcode. I would recommend reading over all of Apple's documentation on Xcode before going any further. The Building A User Interface section may be of particular interest to you.

How to create a view for a single control?

What is the best way to create a view for a single control that I need to load into a Shell region in a Prism app. I know I can wrap the control in a UserControl, but I suspect there may be a better way.
I am working on a demo app to learn Prism 4. Each module will load a navigation button into an ItemsControl in a region in the Shell. These navigation buttons will function like the Mail, Calendar, and other buttons in Outlook.
I am creating the view in each module that will hold the module's navigation button. The simplest way to create the view seems to be to wrap it in a UserControl. My question is this: Is there a better way to do it? Thanks for your help.
If you need graphical control, what you are doing is the way to go. If you find yourself making all of the buttons look the same (copy - pasting) you might find that a menu registration service is the way to go.
You'd have a service like IMenuService that you register with your container and modules can come around and register menu items to. You can then create buttons for the module. I've provided a sample for this here:
http://dl.getdropbox.com/u/376992/CAGMenus.zip
Your question, though, seems to be about whether or not you need to wrap a control in a UserControl to register them with a region? If that's the question, I believe the answer is no, although you might amend your question to tell us what you are running up against that makes you think this.
I ended up wrapping the control in a UserControl, and it seems to work fine. I am still interested in seeing if there is a better way to load the button, so I will hold this question open for a few days.
Edit 02/22/2011: I tried using a control without a UserControl wrapper, and I got the following error:
Library project file cannot specify ApplicationDefinition element.
I wrapped the control in a UserControl and the error went away.

Cocoa Interface Builder's 'Attributes Inspector' like window

I'm making a Cocoa application, and I would like a panel like the 'Attributes Inspector' in Interface Builder. So with big tabs on the top and collapsable/expandable groups. Does anyone know how I can do this?
This is an image of the Attributes Inspector:
Attributes Inspector http://developer.apple.com/documentation/DeveloperTools/Conceptual/XcodeQuickTour/Art/hello_win_attributes.jpg
So I actually want to make a window like the one shown in the image above.
InspectorKit is FOSS on github.
There's no built-in Cocoa controls to do this. You're going to have to write some custom views which replicate the functionality.
There some good advice for creating custom controls in the answers to this question: Looking for info on custom drawing of interface components (Cocoa)
If you need additional help, I recommend you ask smaller, more specific questions explaining what you've tried and what hasn't worked.
I've written some custom classes to do this- it ended up being less work than I expected. I broke it down into two separate components which can be used independently- the first handles the icons at the top and performs the view switching and the second handles the expandable panes:
My code is available at github and is under the BSD 2-clause license.

Resources