简体   繁体   English

屏幕快照 Xamarin.Forms

[英]Screen snapshot Xamarin.Forms

I have the following Xaml code:我有以下 Xaml 代码:

<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>        

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"/> .是否可以只截取 GridView 的第二行的屏幕截图,并将其显示在<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.我已经搜索了如何做,但我只遇到了全屏捕获,这对我来说并不理想。

Sounds like you want to create a snapshot of a specific Xamarin.Forms.View .听起来您想要创建特定Xamarin.Forms.View的快照。 You should wrap the 2nd Grid.Row by a single top level View that will contain the images as children.您应该将第二个Grid.Row包装在一个顶级View ,该View将包含作为子级的图像。 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 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
}

Usage example:用法示例:

<StackLayout
    x:Name="secondRow"
    Grid.Row="1">
    <Image x:Name="image1" />
    <Image x:Name="image2" />
    <Image x:Name="image3" />
</StackLayout>

MakeViewShot(secondRow);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM