简体   繁体   English

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

[英]Resizing UI elements in UWP Canvas with ScrollViewer

I have a Canvas contained in a scrollveiwer so that it can be zoomed and panned. 我有一个Canvas包含在scrollveiwer中,以便可以缩放和平移它。 However, I am also trying to make it so that user input elements (textboxes, images, etc.) can be resized with pinch functionality. 但是,我也试图做到这一点,以便可以使用捏合功能调整用户输入元素(文本框,图像等)的大小。 Here is my XAML code for the basis of the interface: 这是我基于接口的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>

I also have this C# code to add a textbox and handle the manipulation events (drag/resize): 我也有以下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;
    }

This C# code works when the canvas is not contained in a scrollviewer. 当画布未包含在scrollviewer中时,此C#代码有效。 Any suggestions on how to make resizing (with touch/pinch functionality) work in a scrollviewer? 关于如何使大小调整(具有触摸/捏功能)在scrollviewer中起作用的任何建议?

Thanks! 谢谢!

Unfortunately, there is no good solution for your TextBox manipulation in a ScrollViewer . 不幸的是,在ScrollViewer没有很好的TextBox操作解决方案。

See the blog from @Rob Where did all my gestures go? 请参阅@Rob的博客我所有的手势都去了哪里?

ScrollViewer takes over the pointer handling to run its scrolling. ScrollViewer接管指针处理以运行其滚动。 Once the ScrollViewer is in charge it handles all of the pointer and manipulation events without the app having its own chance at them ScrollViewer负责后,它将处理所有指针和操作事件,而应用程序没有机会自己处理它们

... ...

Unfortunately there is no good solution if the app needs both scrolling and gestures (for example, to detect CrossSlides against the scrolling). 不幸的是,如果应用程序同时需要滚动和手势(例如,检测是否与滚动相对的CrossSlides),则没有好的解决方案。 In this case the only option to get the Pointer messages everywhere is to disable Direct Manipulation everywhere, but that disables scrolling as well. 在这种情况下,使指针消息无处不在的唯一选择是在各处禁用“直接操纵”,但这也将禁用滚动。 To get that back the app will need to detect the scrolling gestures itself and then navigate the ScrollViewer to the new location with ScrollToHorizontalOffset or ScrollToVerticalOffset or by updating the SelectedIndex. 为了找回这个应用程序,需要先检测滚动手势本身,然后使用ScrollToHorizo​​ntalOffset或ScrollToVerticalOffset或通过更新SelectedIndex将ScrollViewer导航到新位置。 This is tricky and will be noticeably slower than letting the ScrollViewer do its thing. 这很棘手,并且比让ScrollViewer做事情要慢得多。 It should be avoided if at all possible. 尽可能避免使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM