图像中的绝对位置

时间:2016-08-05 20:24:16

标签: c# xaml uwp

有没有人可以解决这类问题?我有一个背景图像,当我改变分辨率时,我需要在该图像的同一位置使用组件,组件根据视图保持在同一点,但不是根据图像。我有响应度的问题。

有一些图片是一个例子:看看“句子”这一点。

enter image description here

enter image description here

还有xaml代码:

<ScrollViewer VerticalScrollBarVisibility="Auto">
        <Grid >
            <Image Source="Path" Stretch="Fill"/>
            <Canvas>

                <Ellipse Fill="White" Height="15" Width="15" Canvas.Left="170" Canvas.Top="103" />
                <TextBlock Text="Sentence" Foreground="Green" 
                           Width="150" Height="Auto" Canvas.Left="28" Canvas.Top="122" 
                           TextWrapping="WrapWholeWords" TextAlignment="Right"/>
             </Canvas>
        </Grid>
</ScrollViewer>

1 个答案:

答案 0 :(得分:1)

我认为您需要计算后面代码中的位置,并重置Canvas.LeftCanvas.Top属性,例如:

<ScrollViewer VerticalScrollBarVisibility="Auto">
    <Grid>
        <Canvas x:Name="canvas" SizeChanged="canvas_SizeChanged">
            <Canvas.Background>
                <ImageBrush ImageSource="Assets/111.png" />
            </Canvas.Background>
            <Ellipse x:Name="elps" Fill="White" Height="15" Width="15" Canvas.Left="170" Canvas.Top="103" />
            <TextBlock x:Name="tb" Text="Sentence" Foreground="Green"
                   Width="150" Height="Auto" Canvas.Left="28" Canvas.Top="122"
                   TextWrapping="WrapWholeWords" TextAlignment="Right" />
        </Canvas>
    </Grid>
</ScrollViewer>
代码背后的代码:

private double width;
private double height;

private void canvas_SizeChanged(object sender, SizeChangedEventArgs e)
{
    if (width == 0 || height == 0)
    {
        width = canvas.ActualWidth;
        height = canvas.ActualHeight;
    }
    else
    {
        var newwidth = canvas.ActualWidth;
        var newheight = canvas.ActualHeight;
        elps.Width = 15 * (newwidth / width);
        elps.Height = 15 * (newheight / height);
        Canvas.SetTop(elps, 103 * (newheight / height));
        Canvas.SetLeft(elps, 170 * (newwidth / width));
        Canvas.SetTop(tb, 122 * (newheight / height));
        Canvas.SetLeft(tb, 28 * (newwidth / width));
    }
}

我个人认为最好将EllipseTextBlock放在一个控件中并一起移动,这个计算只是一个示例,您可以自己计算。