我已经开发了几年的wpf应用程序,我曾经使用以下代码来平铺图像并将其用作背景图案:
<ImageBrush ImageSource="/Images/GroupPattern.png" TileMode="Tile" ViewportUnits="Absolute" Viewport="0,0,32,32"/>
然而,在一些研究之后我没有找到任何东西,平铺似乎在UWP中不起作用。
那么,有人会有什么相同的东西吗?
谢谢!
答案 0 :(得分:1)
正如@JohnnyWestlake所通知的,您可以使用Microsoft Win2D库。只需向您的xaml添加此代码段即可:
<Page .....
xmlns:win2d="using:Microsoft.Graphics.Canvas.UI.Xaml"
.....>
<Grid>
<win2d:CanvasControl CreateResources="BackgroundCanvas_CreateResources"
Draw="BackgroundCanvas_Draw" />
</Grid>
</Page>
并在.xaml.cs文件中如下所示:
using Microsoft.Graphics.Canvas;
using Microsoft.Graphics.Canvas.Brushes;
using Microsoft.Graphics.Canvas.UI;
using Microsoft.Graphics.Canvas.UI.Xaml;
.......
private CanvasBitmap backgroundImage;
private CanvasImageBrush backgroundBrush;
private void BackgroundCanvas_CreateResources(CanvasControl sender, CanvasCreateResourcesEventArgs args)
{
args.TrackAsyncAction(Task.Run(async () =>
{
string uri = "ms-appx:///Assets/Images/GroupPattern.png";
backgroundImage = await CanvasBitmap.LoadAsync(sender, new Uri(uri));
backgroundBrush = new CanvasImageBrush(sender, backgroundImage);
// Set the brush's edge behaviour to wrap, so the image repeats if the drawn region is too big
backgroundBrush.ExtendX = backgroundBrush.ExtendY = CanvasEdgeBehavior.Wrap;
backgroundBrush.Transform = System.Numerics.Matrix3x2.CreateScale(0.5f);
}).AsAsyncAction());
}
private void BackgroundCanvas_Draw(CanvasControl sender, CanvasDrawEventArgs args)
{
var session = args.DrawingSession;
session.FillRectangle(new Rect(new Point(), sender.RenderSize), backgroundBrush);
}
希望有帮助)