在c#XAML App中进行简单打印?

时间:2015-07-14 12:45:56

标签: c# xaml windows-8

所以我需要从Windows 8应用程序(c#Xaml)打印一个标签,而且我发现的所有样本都过于复杂,符合我的需求。 我的内容是一个单独的页面,包含一个文本块和一个图像,当页面加载时填充,我需要它做的就是打印该页面。是否有简化的方法用于单个简单页面的打印(即,不使用RichTextBox和溢出等)。感兴趣的是,这是我需要打印的页面:

<Page
x:Class="Storeageapp.OutputforLabel"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Storeageapp"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Width="300" Height="200">
<Grid x:Name="printableArea">

    <Grid.RowDefinitions>

        <RowDefinition Height="3*"/>
        <RowDefinition Height="3*"/>

    </Grid.RowDefinitions>


    <StackPanel x:Name="header" Grid.Row="0" Grid.ColumnSpan="2" Height="60"  Visibility="Collapsed">

    </StackPanel>

    <TextBlock x:Name="UID" Text="Hello World" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="24" />

    <Image Source="" x:Name="scenarioImage"  HorizontalAlignment="Center" Grid.Row="1" Grid.Column="0" Margin="10"/>

    <StackPanel x:Name="footer"  Grid.Row="4" Grid.Column="0" VerticalAlignment="Top" Visibility="Collapsed">

    </StackPanel>
</Grid>
</Page>

如果有某种方法可以将其打印成图像,我会感到高兴,但我无法解决这个问题。 谢谢。 编辑:这是在Windows商店应用程序抱歉,而不是WPF。

1 个答案:

答案 0 :(得分:1)

您可以使用PrintDialog Class在WPF中执行基本打印。从链接页面中的示例:

class ListFetchAsyncTask extends AsyncTask<String, Integer, List<MemoInfo>> {

    @Override
    protected void onPreExecute() {

        listView.setFastScrollAlwaysVisible(true);

    }

    protected List<MemoInfo> doInBackground(String... params) {

        Cursor cursor = dbHandler.getFullListCursor();
        if (cursor != null) {

            if(memoList != null)
            {
                if(!memoList.isEmpty())
                {
                    memoList.clear();
                }
            }
            else
                memoList = new  ArrayList<MemoInfo>();

            if (cursor.moveToFirst())
            {
                do {

                    // Get version from Cursor
                    String memoText = cursor.getString(cursor.getColumnIndex("text"));


                    String createdDate = cursor.getString(cursor.getColumnIndex("createdDate"));

                    Integer id = cursor.getInt(cursor.getColumnIndex("_id"));

                    MemoInfo memo = new MemoInfo();
                    memo.set_id(id);
                    memo.set_memotext(memoText);
                    memo.set_createdDate(createdDate);
                    memoList.add(memo);

                    // move to next row
                } while (cursor.moveToNext());
            }
        }

        return memoList;

    }

    @Override
    protected void onPostExecute(List<MemoInfo> list) {
        adapter = new CustomAdapter(getApplicationContext(), list);
        listView.setAdapter(adapter);
    }
}

但是,可以使用甚至更少的代码进行打印:

private void InvokePrint(object sender, RoutedEventArgs e)
{
    // Create the print dialog object and set options
    PrintDialog pDialog = new PrintDialog();
    pDialog.PageRangeSelection = PageRangeSelection.AllPages;
    pDialog.UserPageRangeEnabled = true;

    // Display the dialog. This returns true if the user presses the Print button.
    Nullable<Boolean> print = pDialog.ShowDialog();
    if (print == true)
    {
        XpsDocument xpsDocument = new XpsDocument("C:\\FixedDocumentSequence.xps", FileAccess.ReadWrite);
        FixedDocumentSequence fixedDocSeq = xpsDocument.GetFixedDocumentSequence();
        pDialog.PrintDocument(fixedDocSeq.DocumentPaginator, "Test print job");
    }
}