滚动浏览器中的WP8画布 - 必须是后面的代码



我们有一个绝对布局的要求,该布局允许滚动其内容(子控制)。在应用程序运行期间,创建并随时创建了布局和所有子控制;结果,XAML不是创建布局的选择。

不幸的是,在后面的代码中,很难实现所需的滚动行为。与XAML不同,我们可以很简单地实现它。

以下XAML演示了所需的行为

<phone:PhoneApplicationPage
    x:Class="TestWP8App.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    shell:SystemTray.IsVisible="True">
<ScrollViewer HorizontalAlignment="Left" VerticalAlignment="Top" Width="480">
    <Canvas Height="1500">
        <Button Content="1" Height="300" Canvas.Left="10" Canvas.Top="10" Width="120"/>
        <Button Content="2" Height="300" Canvas.Left="10" Canvas.Top="110" Width="120"/>
        <Button Content="3" Height="300" Canvas.Left="10" Canvas.Top="210" Width="120"/>
        <Button Content="4" Height="300" Canvas.Left="10" Canvas.Top="310" Width="120"/>
        <Button Content="5" Height="300" Canvas.Left="10" Canvas.Top="410" Width="120"/>
    </Canvas>
</ScrollViewer>
</phone:PhoneApplicationPage>

将上述属性转换为C#代码,可以实现相同的"外观",只有内容永远不会滚动。

有人知道如何通过代码实现相同的结果吗?还是知道完全使用代码范围的示例的位置?

它的简单

            ScrollViewer sViewer = new ScrollViewer() {HorizontalAlignment=HorizontalAlignment.Left,VerticalAlignment=VerticalAlignment.Top,Width480 };
            Canvas cc = new Canvas() { Height=1500};
            //same for all the buttons 
            Button b1 = new Button() { Height=300,Width=120,Content="1"};
            Canvas.SetLeft(b1, 10);
            Canvas.SetTop(b1, 10);
            //adding the canvas into sViewer 
            sViewer.Content = cc;

希望它有帮助

XAML代码以简单的方式转换为C#代码。这是C#代码的片段,它将创建上述XAML:

var scrollViewer = new ScrollViewer
{
    HorizontalAlignment = HorizontalAlignment.Left,
    VerticalAlignment = VerticalAlignment.Top,
    Width = 480
};
var canvas = new Canvas
{
    Height = 1500
};
scrollViewer.Content = canvas;
for (var i = 1; i <= 5; ++i)
{
    var button = new Button
    {
        Width = 120,
        Height = 300
        Content = i.ToString()
    };
    Canvas.SetLeft(button, 10);
    Canvas.SetTop(button, (i - 1) * 100 + 10));
    canvas.Children.Add(button);
}

这个问题是亲子关系的问题。

最初我有一个scrollviewer(SV)和一个主画布(MC)。

问题是,即使将MC作为SV的内容设置为

解决方案是在3rd canvas(3c)中修复SV,使层次结构[parent-> child]:


3C-> sv-> mc

然后可以将宽度和高度应用于3C&amp;SV,让MC根据其内容自动化。

随着MC大于SV&amp;3C,允许滚动。

所有儿童都添加到MC

最新更新