Usando LibraryStacks em um ScatterView na superfície
-
06-07-2019 - |
Pergunta
Estamos tentando descobrir como arrastar um item a partir de um recipiente LibraryStack em um ScatterView, como a forma como o visualizador de fotos aplicações de exemplo de trabalho. Atualmente, o item só voa de volta para o LibraryStack depois de arrastá-lo para fora. Nós podemos arrastar e soltar itens para outros LibraryStacks ou LibraryBars.
Aqui está um exemplo do que estamos a tentar:
<s:SurfaceWindow x:Class="Idia_seminar.SurfaceWindow1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="http://schemas.microsoft.com/surface/2008"
Title="Idia_seminar"
>
<s:SurfaceWindow.Resources>
<ImageBrush x:Key="WindowBackground" Stretch="None" Opacity="0.6" ImageSource="pack://application:,,,/Resources/WindowBackground.jpg"/>
</s:SurfaceWindow.Resources>
<Grid Background="{StaticResource WindowBackground}" >
<s:ScatterView Name="scatterView1" AllowDrop="True">
<s:SurfaceButton Name="surfaceButton1">Button</s:SurfaceButton>
<s:LibraryStack AllowDrop="True">
<s:LibraryStackItem Content="hello"></s:LibraryStackItem>
</s:LibraryStack>
</s:ScatterView>
</Grid>
</s:SurfaceWindow>
Obrigado!
Solução
Este é certamente factível. Eu cozinhei-se um exemplo que permite que você arraste os itens a partir de uma librarybar localizado a ScatterView e soltar itens no ScatterView onde aparecem como novas scatterviewitems.
Eu não tenho certeza onde você errou, mas para arrastar / soltar para o trabalho, o seguinte tem que acontecer:
- O destino de soltar deve ter AllowDrop conjunto para true
- O destino de soltar deve ser visível para bater testes (normalmente realizado através da criação de um fundo diferente de nulo - eu uso transparente)
- O destino de soltar deve manipular o evento Gota e fazer algo inteligente com os dados
Aqui está o meu XAML:
<s:ScatterView AllowDrop="True" Background="Transparent"
x:Name="scatterView" s:SurfaceDragDrop.Drop="scatterView_Drop">
<s:SurfaceButton Name="surfaceButton1">Button</s:SurfaceButton>
<s:LibraryStack>
<s:LibraryStackItem Content="Hello"></s:LibraryStackItem>
</s:LibraryStack>
</s:ScatterView>
</s:ScatterView>
E, em código, manipular o evento Gota ??p>
private void scatterView_Drop(object sender, SurfaceDragDropEventArgs e)
{
Console.WriteLine("Got drop: " + e.Cursor.Data);
var newItem = new ScatterViewItem();
// Rely on .ToString() on the data. A real app would do something more clever
newItem.Content = e.Cursor.Data;
// Place the new item at the drop location
newItem.Center = e.Cursor.GetPosition(scatterView);
// Add it to the scatterview
scatterView.Items.Add(newItem);
}
Obviamente, o código acima não manipula arrastando itens de volta para o librarybar. Deixo isso como um exercício para o leitor; -)
O seguinte guia MSDN é algo que eu definitivamente acho que você deve ler: http: //msdn.microsoft.com/en-us/library/ee804812.aspx
Outras dicas
Você precisa definir o pincel de base sobre o ScatterView para uma cor, ou seja transparente por isso mesmo a eventos de queda de apoio.
Você também precisa usar SurfaceDragDrop.
SurfaceDragDrop.AddDropHandler (scatterView1, OnCursorDrop); AddHandler (ScatterViewItem.ScatterManipulationStartedEvent, novo ScatterManipulationStartedEventHandler (OnManipulationStarted));
private void OnManipulationStarted(object sender, RoutedEventArgs args)
{ ScatterViewItem svi = args.OriginalSource como ScatterViewItem; if (SVI! = null) // && DragDropScatterView.GetAllowDrag (SVI)) { svi.BeginDragDrop (svi.DataContext); } }
private void OnCursorDrop(object sender, SurfaceDragDropEventArgs args)
{ SurfaceDragCursor droppingCursor = args.Cursor;
// Add dropping Item that was from another drag source.
if (!scatterView1.Items.Contains(droppingCursor.Data)){
scatterView1.Items.Add(droppingCursor.Data);
var svi = scatterView1.ItemContainerGenerator.ContainerFromItem(droppingCursor.Data) as ScatterViewItem;
if (svi != null){
svi.Center = droppingCursor.GetPosition(scatterView1);
svi.Orientation = droppingCursor.GetOrientation(scatterView1);
svi.Height = droppingCursor.Visual.ActualHeight;
svi.Width = droppingCursor.Visual.ActualWidth;
svi.SetRelativeZIndex(RelativeScatterViewZIndex.Topmost);
}
}
}
Isso tudo é basicamente a partir de um exemplo no SDK, não me lembro qual deles é com tristeza.
Cheers,
Stian Farstad