我正在尝试将纹理添加为滚动查看器的背景。我希望背景与内容一起移动,但我希望它是fiexd。不使用视差效果......
到目前为止,我得到的是:
<!-- 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)中看到的那样,我不得不将HorizontalOffset乘以2以获得内容修复的背景。但是当我移动滚动条的内容时,背景不是固定在内容的位置,移动有点不同,有点慢,然后在正确的位置结束动画。
关于如何获取或者默认窗口是否“强制”使用视差背景的任何建议?
谢谢,
答案 0 :(得分: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>
我希望很快能有更好的解决方案。