【问题标题】:How to convert the contents in the Stacklayout to Pdf or png form in Xamarin forms如何在Xamarin forms中将Stacklayout中的内容转换为Pdf或png格式
【发布时间】:2022-12-07 10:20:25
【问题描述】:

我想将 StackLayout 中的内容转换为 .png 或 .pdf 格式,并且需要从我的 iOS 设备打印相同的内容。我的打印机将只支持图像和 pdf。我怎样才能以 xamarin 形式实现这一目标。请帮助我。这是我的 Xaml。

<StackLayout HorizontalOptions="FillAndExpand" Padding="20" VerticalOptions="CenterAndExpand">
        <Frame HasShadow="False" BorderColor="LightGray"
               CornerRadius="0" VerticalOptions="Center"
               HorizontalOptions="Center">
            <StackLayout HorizontalOptions="Center" Spacing="30">
                <Label HorizontalTextAlignment="Center">
                 <Label.FormattedText>
                     <FormattedString>
                         <Span Text="ID : " FontAttributes="Bold"
                               TextColor="Black"/>
                         <Span Text="1234" TextColor="Green" FontAttributes="Bold"/>
                     </FormattedString>
                 </Label.FormattedText>
                </Label>
                <Frame CornerRadius="1" BorderColor="Green"
                       HasShadow="False" WidthRequest="40" HorizontalOptions="Center">
                    <Image Source="ProfileImg" HeightRequest="70"
                           WidthRequest="50"/>
                </Frame>
                <Label Text="Xamarin" HorizontalTextAlignment="Center"
                       FontSize="40" TextColor="Black"
                       FontAttributes="Bold"/>
            
                <Label HorizontalTextAlignment="Center">
                    <Label.FormattedText>
                        <FormattedString>
                            <Span Text="Phone No : " TextColor="Gray" FontAttributes="Bold"/>
                            <Span Text="1234567890" TextColor="Green" FontAttributes="Bold"/>
                        </FormattedString>
                   </Label.FormattedText>
               </Label>
           </StackLayout>
        </Frame>
    </StackLayout>

【问题讨论】:

  • Xamarin Essentials 有截图功能

标签: c# image pdf xamarin.forms xamarin.ios


【解决方案1】:

正如@Jason 所说,您可以使用Xamarin.Essentials: Screenshot。试试下面的代码:

    async void TapGestureRecognizer_Tapped(System.Object sender, System.EventArgs e)  // i save the png when tapping the frame
    {
        var screenshot = await Screenshot.CaptureAsync();
        var stream = await screenshot.OpenReadAsync();

        string fileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "mypic.png");

        using (var fileStream = new FileStream(fileName, FileMode.Create, FileAccess.Write))
        {
            stream.CopyTo(fileStream);
        } 
    }

在 iOS 中,您可以通过文件应用程序找到 png。

你还应该设置LSSupportsOpenDocumentsInPlace关键和应用程序支持 iTunes 共享关键是的.

更多信息可以参考Xamarin.Essentials: ScreenshotFile system access in Xamarin.iOS

希望对你有效。

【讨论】:

    猜你喜欢
    • 2015-06-11
    • 2017-12-09
    • 1970-01-01
    • 2020-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-03
    • 2017-09-29
    相关资源
    最近更新 更多