2017-11-16 2 views
2

Das Problem liegt mit der ziehbaren Ansicht, im ersten Teil des Codes lokal: Draggableview, stürzt das Programm am Xaml Teil des Codes, aber ich kann nicht herausfinden, was das Problem ist. Ich habe die ziehbare Ansicht entfernt und versucht, Haltepunkte im nicht-xaml-Teil des Codes auszugeben, aber ich kann nicht herausfinden, was ein Problem verursacht.Draggable Boxview Absturz Fehler Xamarin

<ContentPage> 
    <ContentPage.Padding> 
     <OnPlatform x:TypeArguments="Thickness"> 
      <On Platform="iOS, Android" Value="0,40,0,0" /> 
     </OnPlatform> 
    </ContentPage.Padding> 
    <Grid BackgroundColor="White" ColumnSpacing="10" RowSpacing="10"> 
     <Label Text="Red" FontSize="Medium" HorizontalOptions="Center" /> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="*" /> 
      <RowDefinition Height="*" /> 
      <RowDefinition Height="*" /> 
      <RowDefinition Height="*" /> 
      <RowDefinition Height="*" /> 
      <RowDefinition Height="*" /> 
      <RowDefinition Height="*" /> 
      <RowDefinition Height="*" /> 
      <RowDefinition Height="*" /> 
      <RowDefinition Height="*" /> 
     </Grid.RowDefinitions> 

     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="*" /> 
      <ColumnDefinition Width="*" /> 
      <ColumnDefinition Width="*" /> 
     </Grid.ColumnDefinitions> 

     <BoxView Color="Black" Grid.Column="1" Grid.RowSpan="1"/> 
     <BoxView Color="Gray" Grid.Column="2" Grid.RowSpan="1"/> 
     <Label Text="9" Font ="60" Grid.Row="1" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" TextColor="Black" /> 
     <Label Text="8" Font ="60" Grid.Row="2" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" TextColor="Black" /> 
     <Label Text="7" Font ="60" Grid.Row="3" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" TextColor="Black" /> 
     <Label Text="6" Font ="60" Grid.Row="4" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" TextColor="Black" /> 
     <Label Text="5" Font ="60" Grid.Row="5" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" TextColor="Black" /> 
     <Label Text="4" Font ="60" Grid.Row="6" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" TextColor="Black" /> 
     <Label Text="3" Font ="60" Grid.Row="7" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" TextColor="Black" /> 
     <Label Text="2" Font ="60" Grid.Row="8" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" TextColor="Black" /> 
     <Label Text="1" Font ="60" Grid.Row="9" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" TextColor="Black" /> 
     <Label Text="0" Font ="60" Grid.Row="10" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" TextColor="Black" /> 

     <local:DraggableView x:Name="dragView" DragMode="LongPress" DragDirection="All" > 
      <local:DraggableView.Content> 
       <BoxView x:Name="image" ItemSelected="Handle_ItemSelected" /> 
      </local:DraggableView.Content> 
     </local:DraggableView> 
    </Grid> 

</ContentPage> 

Helfer

public enum DragDirectionType 
{ 
    All, 
    Vertical, 
    Horizontal 
} 
public enum DragMode 
{ 
    Touch, 
    LongPress 
} 

DraggableView

public partial class DraggableView : ContentView 
    { 
     public event EventHandler DragStart = delegate { }; 
     public event EventHandler DragEnd = delegate { }; 

     public static readonly BindableProperty DragDirectionProperty = BindableProperty.Create(
      propertyName: "DragDirection", 
      returnType: typeof(DragDirectionType), 
      declaringType: typeof(DraggableView), 
      defaultValue: DragDirectionType.All, 
      defaultBindingMode: BindingMode.TwoWay); 

     public DragDirectionType DragDirection 
     { 
      get { return (DragDirectionType)GetValue(DragDirectionProperty); } 
      set { SetValue(DragDirectionProperty, value); } 
     } 


     public static readonly BindableProperty DragModeProperty = BindableProperty.Create(
      propertyName: "DragMode", 
      returnType: typeof(DragMode), 
      declaringType: typeof(DraggableView), 
      defaultValue: DragMode.LongPress, 
      defaultBindingMode: BindingMode.TwoWay); 

     public DragMode DragMode 
     { 
      get { return (DragMode)GetValue(DragModeProperty); } 
      set { SetValue(DragModeProperty, value); } 
     } 

     public static readonly BindableProperty IsDraggingProperty = BindableProperty.Create(
      propertyName: "IsDragging", 
      returnType: typeof(bool), 
      declaringType: typeof(DraggableView), 
      defaultValue: false, 
      defaultBindingMode: BindingMode.TwoWay); 

     public bool IsDragging 
     { 
      get { return (bool)GetValue(IsDraggingProperty); } 
      set { SetValue(IsDraggingProperty, value); } 
     } 


     public void DragStarted() 
     { 
      DragStart(this, default(EventArgs)); 
      IsDragging = true; 
     } 

     public void DragEnded() 
     { 
      IsDragging = false; 
      DragEnd(this, default(EventArgs)); 
     } 

    } 
} 

Dies ist der Android-Teil des Codes

[assembly: ExportRenderer(typeof(DraggableView), typeof(DraggableViewRenderer))] 

namespace BabakusXamarin.Droid 
{ 
    public class DraggableViewRenderer : VisualElementRenderer<Xamarin.Forms.View> 
    { 
     float originalX; 
     float originalY; 
     float dX; 
     float dY; 
     bool firstTime = true; 
     bool touchedDown = false; 

     protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.View> e) 
     { 
      base.OnElementChanged(e); 


      if (e.OldElement != null) 
      { 
       LongClick -= HandleLongClick; 
      } 
     } 

     private void HandleLongClick(object sender, LongClickEventArgs e) 
     { 
      var dragView = Element as DraggableView; 
      if (firstTime) 
      { 
       originalX = GetX(); 
       originalY = GetY(); 
       firstTime = false; 
      } 
      dragView.DragStarted(); 
      touchedDown = true; 
     } 

     protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) 
     { 
      var dragView = Element as DraggableView; 
      base.OnElementPropertyChanged(sender, e); 

     } 
     protected override void OnVisibilityChanged(AView.View changedView, [GeneratedEnum] ViewStates visibility) 
     { 
      base.OnVisibilityChanged(changedView, visibility); 
      if (visibility == ViewStates.Visible) 
      { 



      } 
     } 
     public override bool OnTouchEvent(MotionEvent e) 
     { 
      float x = e.RawX; 
      float y = e.RawY; 
      var dragView = Element as DraggableView; 
      switch (e.Action) 
      { 
       case MotionEventActions.Down: 
        if (dragView.DragMode == DragMode.Touch) 
        { 
         if (!touchedDown) 
         { 
          if (firstTime) 
          { 
           originalX = GetX(); 
           originalY = GetY(); 
           firstTime = false; 
          } 
          dragView.DragStarted(); 
         } 

         touchedDown = true; 
        } 
        dX = x - this.GetX(); 
        dY = y - this.GetY(); 
        break; 
       case MotionEventActions.Move: 
        if (touchedDown) 
        { 
         if (dragView.DragDirection == DragDirectionType.All || dragView.DragDirection == DragDirectionType.Horizontal) 
         { 
          SetX(x - dX); 
         } 

         if (dragView.DragDirection == DragDirectionType.All || dragView.DragDirection == DragDirectionType.Vertical) 
         { 
          SetY(y - dY); 
         } 

        } 
        break; 
       case MotionEventActions.Up: 
        touchedDown = false; 
        dragView.DragEnded(); 
        break; 
       case MotionEventActions.Cancel: 
        touchedDown = false; 
        break; 
      } 
      return base.OnTouchEvent(e); 
     } 

     public override bool OnInterceptTouchEvent(MotionEvent e) 
     { 

      BringToFront(); 
      return true; 
     } 

    } 


} 

Antwort

1

Das Problem mit der legt ziehbare Ansicht, im ersten Teil des Codes lokal: draggableview, stürzt das Programm am Xaml Teil des Codes, aber ich kann nicht herausfinden, was das Problem ist.

Das Problem ist: Es gibt keine ItemSelected in BoxView. Durch das Entfernen von ItemSelected="ItemSelected" funktionieren Ihre XAML-Codes.

Einzelheiten zu BoxView finden Sie unter BoxView.

+0

Danke! Das hat es behoben. Kannst du auch sehen, warum der Boxview in der oberen rechten Ecke erscheint? Ich habe eine grid.colomn und grid.row dafür hinzugefügt, funktioniert auch nicht, um es überhaupt zu ziehen. Code funktionierte früher mit einem Bild. Es ruft das ontouch-Ereignis richtig auf, aber ich denke, das Problem ist, dass die Ansicht nicht aktualisiert wird? Könnte sein? –

+0

Können Sie mir dabei helfen? :) –

+0

Könnten Sie bitte einen neuen Fall dafür öffnen? Ich werde später nachsehen. –