Xamarin.Forms
Xamarin.Forms copied to clipboard
Unable to get positon when dropping an item (Xam. Forms)
This issue has been moved from a ticket on Developer Community.
I have a simple app that allows me to drag and drop an image
MainPage.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="EventOrganiser.Views.MainPage">
<ContentPage.Content>
<AbsoluteLayout>
<Image Source="tab_about" WidthRequest="32" HeightRequest="32" x:Name="imgDrag">
<Image.GestureRecognizers>
<DragGestureRecognizer CanDrag="True" DropCompleted="DragGestureRecognizer_DropCompleted" DragStarting="DragGestureRecognizer_DragStarting"/>
<DropGestureRecognizer AllowDrop="True"/>
</Image.GestureRecognizers>
</Image>
</AbsoluteLayout>
</ContentPage.Content>
</ContentPage>
Code behind
namespace EventOrganiser.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class MainPage : ContentPage
{
MainViewModel ViewModel => App.Locator.Main;
public MainPage()
{
InitializeComponent();
BindingContext = ViewModel;
}
void DragGestureRecognizer_DropCompleted(object sender, DropCompletedEventArgs e)
{
var img = ((DragGestureRecognizer)sender). Parent as Image;
var xpos = img. X;
var ypos = img. Y;
ViewModel.HasStarted = false;
ViewModel.RecordPos(xpos, ypos);
}
void DragGestureRecognizer_DragStarting(object sender, DragStartingEventArgs e)
{
ViewModel.HasStarted = true;
}
void DropGestureRecognizer_Drop(object sender, DropEventArgs e)
{
var img = ((DragGestureRecognizer)sender). Parent as Image;
var xpos = img. X;
var ypos = img. Y;
ViewModel.HasStarted = false;
ViewModel.RecordPos(xpos, ypos);
}
}
}
When I drag the Image and release it, I check the X and Y pproperies but both are 0 irrespective of where the drop occurs in the view. There is no way either to find the X and Y positons in code (again, both are zero). If I set the view to be an AbsoluteLayout this also returns 0,0
Original Comments
Feedback Bot on 09/10/2020, 10:05 PM:
We have directed your feedback to the appropriate engineering team for further evaluation. The team will review the feedback and notify you about the next steps.
Rachel Kang [MSFT] on 13/10/2020, 09:47 AM:
Hi there! Have you tried making sure that the properties you are retrieving depends on the parameters (i.e. the DropCompletedEventArgs e) of the relevant method (DragGestureRecognizer_DropCompleted)? As an example, you may find our documentation here helpful: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/gestures/drag-and-drop#retrieve-image-or-text-data
In the future, please consider asking these types of (non-bug) questions in our forums (https://forums.xamarin.com/) / Stack Overflow. Thanks so much for understanding, and hope this helps! 😃
Original Solutions
Rachel Kang [MSFT] solved on 13/10/2020, 09:47 AM, 0 votes:
Hi there! Have you tried making sure that the properties you are retrieving depends on the parameters (i.e. the DropCompletedEventArgs e) of the relevant method (DragGestureRecognizer_DropCompleted)? As an example, you may find our documentation here helpful: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/gestures/drag-and-drop#retrieve-image-or-text-data
In the future, please consider asking these types of (non-bug) questions in our forums (https://forums.xamarin.com/) / Stack Overflow. Thanks so much for understanding, and hope this helps! 😃
Paul Johnson on 13/10/2020, 10:29 AM:
Neither of the events return the X and Y position of the dropped object. I refereed to the docs before filing the bug
related to efforts in #11663
@PureWeen @Redth I am happy to pick this one up if that's Ok as I need it for a current project unless a member of the team is already on it. I did look to adding it to the Community Toolkit but it's pretty deep in XF so really should live in the Main project, but if I do make the changes will it get pulled in or is it in transition to MAUI already?
@CliffAgius If you create a PR it'll definitely land in MAUI but then we'll have to see if it meets the bar for putting it into XF 5.0.1
We're trying to avoid any new APIs but this change should be fairly small yea?
@PureWeen I have created a PR #13660 that I hope allows the reporting of the Drop Location on screen, I have tested on UWP (The target I need urgently) and Droid but currently my Mac is broken so I am unable to test the iOS version just yet.
I am not sure on the naming of the DropX and DropY properties I called them that as I feel we may want to have DragX and DragY at a later date so this kind of made easier.
Thanks for this suggestion! As Xamarin.Forms is now in maintenance mode, this will not happen anymore for Xamarin.Forms. We're only adding bugfixes and stability fixes.
If this is still important to you, make sure to check the .NET MAUI repo and see if it's already on the roadmap. If not, feel free to open a discussion to discuss a change first or open an issue with a detailed feature request. Thanks!