0

我正在尝试将纹理作为背景添加到滚动查看器。我希望背景随内容移动,但我希望它固定。不使用视差效果...

到目前为止,我得到的是:

   <!-- Background -->
   <Canvas ZIndex="-1" x:Name="backgroundCanvas">
   </Canvas>

       <ScrollViewer
            x:Name="myGridScrollViewer"
            AutomationProperties.AutomationId="GridScrollViewer"
            Grid.Row="1"
            Margin="0,-4,0,0"
            Style="{StaticResource HorizontalScrollViewerStyle}">
            <StackPanel Orientation="Horizontal">
                <StackPanel Margin="116,4,0,0" Orientation="Horizontal" Height="586" VerticalAlignment="Top">
                    <Border x:Name="HeroImage" VerticalAlignment="Top"  Width="700" Height="550" Background="Yellow" Margin="0,0,15,0" ></Border>
                    <Border VerticalAlignment="Top"  Width="350" Height="400" Background="White" Margin="0" ></Border>
                </StackPanel>
                <GridView
                    x:Name="recipeGridView"
                    AutomationProperties.AutomationId="MyGridView"
                    AutomationProperties.Name="MyItems"
                    TabIndex="1"
                    Grid.Row="1"
                    Margin="65,0,116,46"
                    ItemsSource="{Binding Path=Items}"
                    ItemTemplate="{StaticResource myGridViewTemplate}"
                    VirtualizingStackPanel.VirtualizationMode="Recycling"
                    SelectionMode="None"
                    IsItemClickEnabled="True"
                    ItemClick="ItemDetailClick" VerticalContentAlignment="Top"/>
            </StackPanel>
        </ScrollViewer>

后面的代码:

public ItemsList()
{
    this.InitializeComponent();

    UpdateBackground(0);
    myGridScrollViewer.ViewChanged += UpdateBackgroundImagePosition;
}

private void UpdateBackground(double offset)
{
    BitmapImage im = new BitmapImage(new Uri("ms-appx:///Assets/tileable_bg.png"));
    var brush = new ImageBrush();
    brush.ImageSource = im;

    backgroundCanvas.SetValue(Canvas.WidthProperty, Window.Current.Bounds.Width);
    backgroundCanvas.SetValue(Canvas.HeightProperty, Window.Current.Bounds.Height);
    backgroundCanvas.SetValue(Canvas.MarginProperty, new Thickness(offset, 0, 0, 0));
    backgroundCanvas.SetValue(Canvas.BackgroundProperty, brush);
}

private void UpdateBackgroundImagePosition(object sender, ScrollViewerViewChangedEventArgs e)
{
    UpdateBackground(-((ScrollViewer)sender).HorizontalOffset*2);
}

正如您在事件侦听器 (UpdateBackgroundImagePosition) 中看到的那样,我必须将 Horizo​​ntalOffset 乘以 2 才能使背景与内容固定。但是当我移动滚动条的内容时,背景并没有固定在内容的位置,移动有点不同,有点慢,然后在正确的位置结束动画。

关于如何获得它的任何建议,或者默认情况下窗口是否“强制”使用视差背景?

谢谢,

4

1 回答 1

1

由于 Windows 8 Metro 中尚不支持 Tiled Brush,因此似乎可行但不是很好的解决方案是:

     <ScrollViewer
        x:Name="itemsGridScrollViewer"
        AutomationProperties.AutomationId="GridScrollViewer"
        Grid.Row="0"
        Margin="0,-4,0,0"
        Style="{StaticResource HorizontalScrollViewerStyle}">

        <!-- Main Grid: Wrapper for all the content -->
        <Grid x:Name="MainGrid">
            <Grid x:Name="TiledBackground" />

            <GridView x:Name="ContentPanel" .... />
        </Grid>
     </Scrollviewer>
  • 网格“TiledBackground”包含填充 Scrollviewer 所需的尽可能多的单元格。这是在代码隐藏中实现的。

我希望尽快有更好的解决方案。

于 2012-08-01T23:54:26.770 回答