使用ScrollViewer调整UWP Canvas中的UI元素大小

问题描述 投票:0回答:1

我有一个包含在scrollveiwer中的Canvas,因此可以进行缩放和平移。但是,我也试图通过捏合功能调整用户输入元素(文本框,图像等)的大小。这是我的XAML代码,用于界面的基础:

<Grid>
    <RelativePanel>
        <Button ... />
        <Button ... />
        <Button />

        <ScrollViewer
         VerticalScrollMode="Auto"
         HorizontalScrollMode="Auto"
         HorizontalScrollBarVisibility="Visible"
         VerticalScrollBarVisibility="Visible"
         ZoomMode="Enabled"
         MinZoomFactor="1"
         MaxZoomFactor="5"
         HorizontalAlignment="Left"
         VerticalAlignment="Top"
         RelativePanel.Below="AddTextButton">

            <Canvas Name="parentCanvas" Height="10000" Width="10000" >

                <InkCanvas Name="inkCanvas" Height="10000" Width="10000"
                        Visibility="Visible"   />

            </Canvas>

        </ScrollViewer>
    </RelativePanel>
</Grid>

我还有这个C#代码来添加文本框并处理操作事件(拖动/调整大小):

public void AddTextButton_Click(object sender, RoutedEventArgs e)
    {
        TextBox MyTextBox = new TextBox();
        MyTextBox.Background = new SolidColorBrush(Colors.White);

        MyTextBox.PlaceholderText = "Text";
        MyTextBox.Width = 250;
        MyTextBox.Height = 100;

        ManipulationMode = ManipulationModes.All;
        MyTextBox.RenderTransform = textBoxTransforms;
        AddHandler(ManipulationDeltaEvent, new ManipulationDeltaEventHandler(TextBox_ManipulationDelta), true);
        parentCanvas.Children.Add(MyTextBox);        
    }

    void TextBox_ManipulationDelta(object sender,
        ManipulationDeltaRoutedEventArgs e)
    {
        // Move the TextBox. 

        dragTextBox.X += e.Delta.Translation.X;
        dragTextBox.Y += e.Delta.Translation.Y;

        resizeTextBox.ScaleX *= e.Delta.Scale;
        resizeTextBox.ScaleY *= e.Delta.Scale;
    }

当画布未包含在scrollviewer中时,此C#代码可以正常工作。有关如何在滚动查看器中调整大小(使用触摸/捏合功能)的任何建议?

谢谢!

c# canvas uiscrollview resize windows-10-universal
1个回答
0
投票

不幸的是,没有很好的解决方案,你在TextBox操纵ScrollViewer

请参阅@Rob Where did all my gestures go?的博客

ScrollViewer接管指针处理以运行其滚动。一旦ScrollViewer负责,它就会处理所有指针和操作事件,而app不会有自己的机会

...

不幸的是,如果应用程序需要滚动和手势(例如,检测CrossSlides对滚动),则没有好的解决方案。在这种情况下,到处获取指针消息的唯一选择是在所有地方禁用直接操作,但也禁用滚动。要获得该功能,应用程序将需要检测滚动手势本身,然后使用ScrollToHorizo​​ntalOffset或ScrollToVerticalOffset或通过更新SelectedIndex将ScrollViewer导航到新位置。这很棘手,并且比让ScrollViewer做它的事情要慢得多。如果可能的话应该避免。

© www.soinside.com 2019 - 2024. All rights reserved.