屏幕快照Xamarin.Forms [英] Screen snapshot Xamarin.Forms
问题描述
我有以下Xaml代码:
I have the following Xaml code:
<StackLayout>
<Button Text="Take a picture" Clicked="Button_Clicked_1" />
<Grid x:Name="MainPageBluePrintGrid" BackgroundColor="BlueViolet" HeightRequest="100">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Label Text="A" Grid.Row="0" Grid.Column="0"/>
<Label Text="B" Grid.Row="0" Grid.Column="1"/>
<Label Text="C" Grid.Row="0" Grid.Column="2"/>
<Image x:Name="image1" Grid.Row="1" Grid.Column="0"/>
<Image x:Name="image2" Grid.Row="1" Grid.Column="1"/>
<Image x:Name="image3" Grid.Row="1" Grid.Column="2"/>
</Grid>
<StackLayout BackgroundColor="BurlyWood">
<Image x:Name="CapturedImage"/>
</StackLayout>
</StackLayout>
第二行是图像,由用户选择.是否可以仅截取GridView的第二行的屏幕截图,并将其显示在<Image x:Name="CapturedImage"/>
中.我已经搜索了如何执行此操作,但是我只遇到了全屏捕获的情况,这对我而言并不理想.
The second row is images, which are picked by the user. Is it possible to take a screenshot only of the second row of GridView, and display it in the <Image x:Name="CapturedImage"/>
. I have search how to do it, but I only came across full screen capture, which is not ideal in my case.
推荐答案
听起来像您想要创建特定Xamarin.Forms.View
的快照的声音.您应将第二个Grid.Row
用单个顶级View
包装,该顶级将包含图像作为子级.之后,您可以使用特定于平台的功能来创建快照.
Sounds like you want to create a snapshot of a specific Xamarin.Forms.View
. You should wrap the 2nd Grid.Row
by a single top level View
that will contain the images as children. After that you could use platform specific capabilities to create a snapshot.
Android
public void MakeViewShot(Xamarin.Forms.View view)
{
var nativeView = view.GetRenderer().View;
var wasDrawingCacheEnabled = nativeView.DrawingCacheEnabled;
nativeView.DrawingCacheEnabled = true;
nativeView.BuildDrawingCache(false);
var bitmap = nativeView.GetDrawingCache(false);
// TODO: Save bitmap and return filepath
nativeView.DrawingCacheEnabled = wasDrawingCacheEnabled;
}
iOS
public void MakeViewShot(Xamarin.Forms.View view)
{
var nativeView = Xamarin.Forms.Platform.iOS.Platform.GetRenderer(view).NativeView;
UIGraphics.BeginImageContextWithOptions(nativeView.Bounds.Size, opaque: true, scale: 0);
nativeView.DrawViewHierarchy(nativeView.Bounds, afterScreenUpdates: true);
var image = UIGraphics.GetImageFromCurrentImageContext();
UIGraphics.EndImageContext();
// TODO: Save bitmap and return filepath
}
用法示例:
<StackLayout
x:Name="secondRow"
Grid.Row="1">
<Image x:Name="image1" />
<Image x:Name="image2" />
<Image x:Name="image3" />
</StackLayout>
MakeViewShot(secondRow);
这篇关于屏幕快照Xamarin.Forms的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!