Good Afternoon Folks,希望有人可以帮我一把。
新编码所以正在开展一个侧面项目,让我保持活跃。然而,在Stackoverflow上发现了这篇文章,它有我需要的功能
How can I make a Rectangle on a canvas which is draggable?
MouseLeftButtonDown不可用,它是否已在Blank UwP项目中被取代?所以我修改了它如下。
然而,矩形只能在一个方向上来回对角移动。
我错过了什么。
public sealed partial class MainPage : Page
{
bool drag = false;
Windows.UI.Input.PointerPoint startPoint;
public MainPage()
{
this.InitializeComponent();
}
private void mnuGas1_Click(object sender, RoutedEventArgs e)
{
var cargo = new Rectangle();
//Properties
cargo.Height = 100;
cargo.Width = 100;
cargo.Fill = new SolidColorBrush(Windows.UI.Colors.HotPink);
//Handlers
cargo.PointerPressed += Cargo_PointerPressed;
cargo.PointerMoved += Cargo_PointerMoved;
cargo.PointerReleased += Cargo_PointerReleased;
Canvas.SetLeft(cargo, 0);
Canvas.SetTop(cargo, 0);
containerContainer.Children.Add(cargo);
}
private void Cargo_PointerReleased(object sender, PointerRoutedEventArgs e)
{
drag = false;
//throw new NotImplementedException();
}
private void Cargo_PointerMoved(object sender, PointerRoutedEventArgs e)
{
if (drag)
{
Rectangle cargoDrag = sender as Rectangle;
Windows.UI.Input.PointerPoint newPoint = e.GetCurrentPoint(containerContainer);
var left = Canvas.GetLeft(cargoDrag);
var top = Canvas.GetLeft(cargoDrag);
Canvas.SetLeft(cargoDrag, left + (newPoint.RawPosition.X - startPoint.RawPosition.X));
Canvas.SetTop(cargoDrag, top + (newPoint.RawPosition.Y - startPoint.RawPosition.Y));
startPoint = newPoint;
}
//throw new NotImplementedException();
}
private void Cargo_PointerPressed(object sender, PointerRoutedEventArgs e)
{
drag = true;
startPoint = e.GetCurrentPoint(containerContainer);
log.Text = "Clicked";
//throw new System.NotImplementedException();
}
}
}
enter<Page
x:Class="App2.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App2"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="AntiqueWhite" VerticalAlignment="Center">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Image Source="Assets/PrinceDeck.png" Grid.Row="1" Grid.Column="0" MaxWidth="1250"></Image>
<Canvas Name="containerContainer" Grid.Row="1" Grid.Column="0" MaxWidth="1250" Canvas.Left="0" Canvas.Top="0" ></Canvas>
<Rectangle Fill="LightBlue" Grid.Column="0" Grid.Row="0" Height="300"/>
<Rectangle Fill="LightBlue" Grid.Column="0" Grid.Row="2" Height="300"/>
<Rectangle Fill="Pink" Grid.Row="1" Grid.Column="1" Height="Auto"/>
<Rectangle Fill="CornflowerBlue" Grid.Column="1" Grid.Row="0" Height="300"/>
<Rectangle Fill="CornflowerBlue" Grid.Column="1" Grid.Row="2" Height="300"/>
<StackPanel Grid.Column="1" Grid.Row="1" >
<Button Content="Cargo Boxes" Width="Auto"></Button>
<Button Content="Cargo Baskets" Width="Auto"></Button>
<Button Content="Chemical Tanks" Width="Auto"></Button>
<Button Content="Closed Mud Skips" Width="Auto"></Button>
<Button Content="Dry Goods Container" Width="Auto"></Button>
<Button Content="Drum Baskets" Width="Auto"></Button>
<Button Content="FIBC Containers" Width="Auto"></Button>
<Button Content="Gas Racks" Width="Auto">
<Button.Flyout>
<MenuFlyout>
<MenuFlyoutItem Name="mnuGas1" Text="1150mm" Click="mnuGas1_Click"/>
<MenuFlyoutItem Text="1462mm"/>
</MenuFlyout>
</Button.Flyout>
</Button>
<Button Content="Helifuel Tanks" Width="Auto"></Button>
<Button Content="Half Heights" Width="Auto"></Button>
<Button Content="Lifting Frames" Width="Auto"></Button>
<Button Content="Mini Containers" Width="Auto"></Button>
<Button Content="Open Tops" Width="Auto"></Button>
<Button Content="Pipe Baskets" Width="Auto"></Button>
<Button Content="Tubular Transportation Frames" Width="Auto"></Button>
<TextBlock Name="log" Grid.Column="0" Grid.Row="2" Height="300"/>
</StackPanel>
</Grid>
代码