2017-06-20 41 views
1

我有一個與應用程序一樣高並具有50寬度的網格。我在左上方有一個按鈕,寬度也是50。我想通過用鼠標拖動它沿垂直左軸移動此按鈕。但它應該可以正常點擊。我試圖用微軟的拖放式樣本來做到這一點,但我想實現的過程並不完全是拖放式的。我如何通過在通用Windows應用程序中使用XAML和C++ - cx作爲代碼來實現這一點?UWP/C++ - cx - 通過拖動在垂直軸上移動按鈕

我的XAML的代碼網格/按鈕:

<Grid x:Name="Grid1" Width="50" > 
<Button x:Name="btnMove" Content="Move me!" Background="PaleGreen" Click="btnMove_Click" VerticalAlignment="Top" HorizontalAlignment="Left" Width="50" Height="50"></Button> 
</Grid> 

回答

4

爲了您的需求,您可以通過使用ManipulationDelta類移動在縱軸上的按鈕。你可以用下面的代碼來實現它。 更多請參考Handle pointer input。這裏是official code樣本。

MainPage::MainPage() 
{ 
    InitializeComponent(); 
    InitManipulationTransforms(); 
    btnMove->ManipulationDelta += ref new ManipulationDeltaEventHandler(this, &MainPage::btnMove_ManipulationDelta); 
    btnMove->ManipulationMode = ManipulationModes::TranslateX; 
} 

void App14::MainPage::InitManipulationTransforms() 
{ 
    transforms = ref new TransformGroup(); 
    previousTransform = ref new MatrixTransform(); 
    previousTransform->Matrix = Matrix::Identity; 

    deltaTransform = ref new CompositeTransform(); 

    transforms->Children->Append(previousTransform); 
    transforms->Children->Append(deltaTransform); 

    // Set the render transform on the rect 
    btnMove->RenderTransform = transforms; 
} 

void App14::MainPage::btnMove_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e) 
{ 

} 
void MainPage::btnMove_ManipulationDelta(Platform::Object^ sender, ManipulationDeltaRoutedEventArgs^ e) 
{ 


    previousTransform->Matrix = transforms->Value; 

    // Get center point for rotation 
    Point center = previousTransform->TransformPoint(Point(e->Position.X, e->Position.Y)); 
    deltaTransform->CenterX = center.X; 
    deltaTransform->CenterY = center.Y; 

    // Look at the Delta property of the ManipulationDeltaRoutedEventArgs to retrieve 
    // the rotation, scale, X, and Y changes 
    deltaTransform->Rotation = e->Delta.Rotation; 
    deltaTransform->TranslateX = e->Delta.Translation.X; 
    deltaTransform->TranslateY = e->Delta.Translation.Y; 
} 

你可以通過修改按鈕ManipulationMode更改按鈕的滾動方向。

btnMove->ManipulationMode = ManipulationModes::TranslateY; 

enter image description here

+0

非常感謝,這是工作的罰款。但我仍然有一個問題。有了這段代碼,按鈕就可以脫離實體邊界,然後你就不能讓他回來,他會在邊界後面打勾。我該如何解決 ?我如何設置邊界? – David

+0

willouble height =(dynamic_cast (Window :: Current-> Content)) - > ActualHeight; ' 我聲明瞭框架的當前高度,在代碼中的確切位置是否放置了之前發佈的if結構? – David