【问题标题】:WPF Printing (XpsDocumentWriter) working in debug but not in deploymentWPF 打印(XpsDocumentWriter)在调试中工作但不在部署中
【发布时间】:2016-02-10 13:44:36
【问题描述】:

希望一些有经验的 WPF 开发人员以前遇到过这个问题。

背景:此信息可能不是帮助解决问题所必需的,但如果它是相关的。 我的解决方案包括三个项目。前端 GUI、业务逻辑服务和打印机服务。这三个项目通过命名管道进行 IPC。业务逻辑将标签类型和托盘 ID 交给打印逻辑。

问题: 然后打印逻辑创建标签并打印它(通过将其添加到打印机的打印队列中)正如标题所示,当我在 Visual Studio 中调试时,这一切正常.但是,当我在我的开发人员电脑上部署/安装服务 时,它无法正常工作。

更新:它没有抛出异常,但我只记录“关于将文档发送到打印机”而不是“将文档发送到打印机”行,所以它挂在 dw1.Write(fixedDoc);线

更多信息:我在打印项目/visual studio 2013 中使用 .Net 4.0

        public void printLabel(string labelType, string _palletID = null)
    {
        try
        {
            ILabelTemplate Label                = createLabel(labelType, _palletID);

            PrintDialog pd                      = new PrintDialog();
            FixedDocument fixedDoc              = new FixedDocument();
            PageContent pageContent             = new PageContent();

            FixedPage fixedPage                 = getFixedPage();
            fixedDoc.DocumentPaginator.PageSize = new System.Windows.Size(fixedPage.Width, fixedPage.Height);

            IXamlTemplate vm                    = CreateViewModel(Label);
            ILabelPrintDocument template        = CreateTemplate(Label);

            template.dockPanel.DataContext      = vm;
            template.dockPanel.Height           = fixedPage.Height;
            template.dockPanel.Width            = fixedPage.Width;
            template.dockPanel.UpdateLayout();


            fixedPage.Children.Add(template.dockPanel);
            ((System.Windows.Markup.IAddChild)pageContent).AddChild(fixedPage);
            fixedDoc.Pages.Add(pageContent);

            XpsDocumentWriter dw1 = PrintQueue.CreateXpsDocumentWriter(new System.Printing.PrintQueue(new System.Printing.PrintServer(), Label.PrinterName));
            Library.WriteErrorLog("About to send doc to printer");
            dw1.Write(fixedDoc);
            Library.WriteErrorLog("Sent doc to printer");


        }
        catch (Exception ex)
        {

            Library.WriteErrorLog(ex);
        }

【问题讨论】:

    标签: c# wpf printing


    【解决方案1】:

    已解决 ...有点

    在尝试不同的事情并阅读了几个小时后,我发现这是由于我的应用程序在调试时以我的身份运行,但在我部署它时作为本地系统运行。并且本地系统服务无法访问打印机等网络资源。尽管了解了这一点,但我还是开始了如何制作 C# 服务打印的道路。看了很多帖子之后好了(游戏太晚了,帮不上忙)

    Like thisthis one 我知道我走错了路。

    故事的寓意是,如果您正在阅读这篇文章,您可能还没有达到“使用 Win32 API(例如在 C/C++ 中)编写自己的打印 DLL,然后从您的使用 P/Invoke 服务”

    对我有用的解决方案不是将此项目作为通过我的 GUI 启动的服务运行。相反,我把它变成了一个仍然通过我的 GUI 启动和停止的进程。

    有问题的代码是

                    if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + "\\yourAppNameGoesHere.exe"))
                {
                    Process.Start(AppDomain.CurrentDomain.BaseDirectory + "\\yourAppNameGoesHere.exe");
                }
    

    然后当 GUI 关闭时我运行代码

            if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + "\\yourAppNameGoesHere.exe"))
            {
               Process[] myapps = Process.GetProcesses("yourAppNameGoesHere.exe");
               foreach (Process _p in myapps)
               {
                   _p.Kill();
               }
    
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-07-30
      • 1970-01-01
      • 1970-01-01
      • 2023-03-10
      • 2021-06-28
      • 2015-08-16
      • 1970-01-01
      相关资源
      最近更新 更多