0

我在下面有我的代码,基本上是打印出图像。

private async void imageControl_PrintButtonClick(object sender, RoutedEventArgs e)
{
    var createBitmapTask = Task.Run(async () =>
    {
        var stream = await provider.OpenEntryAsRandomAccessStreamAsync(currentImageFile);
        var decoder = await BitmapDecoder.CreateAsync(stream);
        return await decoder.GetSoftwareBitmapAsync(
               BitmapPixelFormat.Bgra8,
               BitmapAlphaMode.Premultiplied);
    });
    var printHelper = new PrintHelper(printPanel);
    printHelper.OnPreviewPagesCreated += PrintHelper_OnPreviewPagesCreated;
    printPanel.Opacity = 1.0;
    var source = new SoftwareBitmapSource();
    var bitmap = await createBitmapTask;
    await source.SetBitmapAsync(bitmap);

    printImage.Source = source;
    printFileName.Text = "Hello";
    printImage.Height = bitmap.PixelHeight;
    printImage.Width = bitmap.PixelWidth;

    await printHelper.ShowPrintUIAsync("ZipPicView - " + currentImageFile.ExtractFilename(), true);
    printPanel.Opacity = 0;
}

private void PrintHelper_OnPreviewPagesCreated(List<FrameworkElement> obj)
{
    ContentDialog dialog = new ContentDialog();
}

但是,打印预览显示空白页。当我打印出来时,打印机什么也不做。

我尝试将printPanel(这是一个Grid对象)的不透明度更改为非零,并且图像确实显示在屏幕上。尽管如此,打印在输出页面上没有任何内容。

我确实注意到OnPreviewPagesCreated,它的 obj 参数在每次被调用时都有一个新对象。第一次调用有一个对象,其宽度和高度均为 NaN。我的猜测是因为容器没有大小,它无法确定内容大小。

下面是 XAML 文件。

<Page x:Name="page"
    x:Class="ZipPicViewUWP.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:ZipPicViewUWP"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:interactivity="using:Microsoft.Xaml.Interactivity"
    xmlns:behaviors="using:Microsoft.Toolkit.Uwp.UI.Animations.Behaviors"
    mc:Ignorable="d" KeyUp="page_KeyUp" Loaded="page_Loaded" SizeChanged="page_SizeChanged">
    <Canvas x:Name="canvas" SizeChanged="canvas_SizeChanged">
        <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" Height="1019" Width="1920">
            <Grid x:Name="printPanel" Opacity="0">
                <StackPanel x:Name="printContent"
                        Margin="0,0"
                        Orientation="Vertical">
                    <TextBlock x:Name="printFileName" />
                    <Image x:Name="printImage"
                       Stretch="Fill" />
                </StackPanel>
            </Grid>
            <SplitView x:Name="splitView" DisplayMode="CompactOverlay" PanePlacement="Left" CompactPaneLength="50" OpenPaneLength="300">
                <SplitView.Content>
                    <GridView x:Name="thumbnailGrid" />
                </SplitView.Content>
                <SplitView.Pane>
                    <Grid Background="{StaticResource SidebarBackground}">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="50" />
                            <RowDefinition Height="*" />
                            <RowDefinition Height="50" />
                        </Grid.RowDefinitions>
                        <StackPanel Orientation="Horizontal" Grid.Row="0">
                            <Button x:Name="subFolderButton" Width="50" Height="50" Background="Transparent" Click="subFolderButton_Click">
                                <SymbolIcon Symbol="List" />
                            </Button>
                            <TextBlock VerticalAlignment="Center" Margin="0,15">Folders</TextBlock>
                        </StackPanel>
                        <ScrollViewer Grid.Row="1">
                            <ListView x:Name="subFolderListCtrl" SelectionChanged="subFolderList_SelectionChanged" DataFetchSize="2" Margin="-10,0,0,0" />
                        </ScrollViewer>
                        <ProgressRing x:Name="thumbProgress" Height="30" Width="30" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Row="2" Margin="10,0,0,10" />
                    </Grid>
                </SplitView.Pane>
            </SplitView>
            <interactivity:Interaction.Behaviors>
                <behaviors:Blur x:Name="BlurBehavior" Duration="500" AutomaticallyStart="False" />
            </interactivity:Interaction.Behaviors>
        </Grid>
        <Border x:Name="imageBorder" Visibility="Collapsed">
            <Image x:Name="image" ManipulationMode="TranslateX" ManipulationCompleted="image_ManipulationCompleted" Tapped="image_Tapped">
                <interactivity:Interaction.Behaviors>
                    <behaviors:Blur x:Name="ImageTransitionBehavior" Duration="500" AutomaticallyStart="False" />
                </interactivity:Interaction.Behaviors>
            </Image>
        </Border>

        <local:ViewerControl x:Name="imageControl" Visibility="Collapsed" CloseButtonClick="imageControl_CloseButtonClick" NextButtonClick="imageControl_NextButtonClick" PrevButtonClick="imageControl_PrevButtonClick" SaveButtonClick="imageControl_SaveButtonClick" PrintButtonClick="imageControl_PrintButtonClick" />
        <Border x:Name="loadingBorder" Visibility="Collapsed">
            <ProgressRing IsActive="True" Width="100" Height="100" />
        </Border>
    </Canvas>
    <Page.TopAppBar>
        <CommandBar VerticalContentAlignment="Center" VerticalAlignment="Center">
            <CommandBar.Content>
                <TextBlock Margin="10,0,0,0" x:Name="filenameTextBlock" Text="&lt;None&gt;" UseLayoutRounding="True" />
            </CommandBar.Content>
            <AppBarToggleButton x:Name="fullscreenButton" Icon="FullScreen" Label="FullScreen" Checked="fullscreenButton_Checked" Unchecked="fullscreenButton_Unchecked" />
            <AppBarSeparator />
            <AppBarButton x:Name="openFileButton" Icon="OpenFile" Label="Open File" Click="openFileButton_Click" />
            <AppBarButton x:Name="openFolderButton" Icon="Folder" Label="Open Folder" Click="openFolderButton_Click" />
        </CommandBar>
    </Page.TopAppBar>
</Page>

源代码在这里:https ://github.com/wutipong/ZipPicViewCS/tree/master/ZipPicViewUWP

4

0 回答 0