屏幕快照Xamarin.Forms

时间:2018-09-03 14:57:37

标签: c# xamarin xamarin.forms screenshot

我有以下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>        

第二行是图像,由用户选择。是否可以仅对GridView的第二行截屏,并将其显示在<Image x:Name="CapturedImage"/>中。我已经搜索了如何执行此操作,但是我只遇到了全屏捕获的情况,这对我而言并不理想。

1 个答案:

答案 0 :(得分:1)

您想要创建特定return [ 'distance' => 'required_without:distance_type | different: distance_type', 'distance_type' => 'required_without:distance | different: distance', ]; 快照的声音。您应将第二个Xamarin.Forms.View包裹在单个顶层Grid.Row上,该顶层将包含图像作为子图像。之后,您可以使用特定于平台的功能来创建快照。

Android

View

iOS

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

用法示例:

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
}