如何处理silverlight 5中的打印对话框关闭事件

时间:2014-01-09 00:01:52

标签: silverlight printing silverlight-5.0

我是Silverlight的新手。我需要在打印时删除DatePicker图标。所以我写了这段代码来打印时删除图标。如果单击打印对话框中的打印按钮,它可以正常工作。但是,如果我关闭打印对话框日期选择器图标不会出现。我必须在打印对话框的关闭事件中编写代码。我找不到打印对话框的事件。

这是打印代码:

    private StackPanel Downloaded_Data(StackPanel Sp_Element)
    {
        foreach (UIElement ele in Sp_Element.Children)
        {
            if (ele is FirstPageTC)
            {
                if ((ele as FirstPageTC).Content is StackPanel)
                    Downloaded_Data((ele as FirstPageTC).Content as StackPanel);
                if ((ele as FirstPageTC).Content is Grid)
                    Download_Grid((ele as FirstPageTC).Content as Grid);
            }
            if (ele is Blank_Page)
            {
                if ((ele as Blank_Page).Content1 is StackPanel)
                    Downloaded_Data((ele as Blank_Page).Content1 as StackPanel);
                if ((ele as Blank_Page).Content1 is Grid)
                    Download_Grid((ele as Blank_Page).Content1 as Grid);
            }


            if (ele is DatePicker)
            {
                DatePicker s = ele as DatePicker;
                s.Style = App.Current.Resources["DateStyle"] as Style;
               // s.Background = new SolidColorBrush(Color.FromArgb(255, 143, 188, 143));
            }

            if (ele is StackPanel)
            {
                Downloaded_Data(ele as StackPanel);
            }
            if (ele is Grid)
            {
                Download_Grid(ele as Grid);
            }
        }
        return Sp_Element;
    }

    public void Download_Grid(Grid grid_down)
    {
        foreach (UIElement ele in grid_down.Children)
        {
            if (ele is Border)
            {
                UIElement ele_b = (ele as Border).Child;
                if (ele_b is DatePicker)
                {
                    DatePicker s = ele_b as DatePicker;

                    s.Style = App.Current.Resources["DateStyle"] as Style;
                   // s.Background = new SolidColorBrush(Color.FromArgb(255, 143, 188, 143));
                }

                //here
                if (ele_b is Grid)
                {
                    Download_Grid(ele_b as Grid);
                }
                if (ele_b is StackPanel)
                {
                    Downloaded_Data(ele_b as StackPanel);
                }
            }
        }
    }

    private void Downloaded_Datepicker(StackPanel Sp_Element)
    {
        foreach (UIElement ele in Sp_Element.Children)
        {
            if (ele is FirstPageTC)
            {
                if ((ele as FirstPageTC).Content is StackPanel)
                    Downloaded_Datepicker((ele as FirstPageTC).Content as StackPanel);
                if ((ele as FirstPageTC).Content is Grid)
                    Download_GridDate((ele as FirstPageTC).Content as Grid);
            }
            if (ele is Blank_Page)
            {
                if ((ele as Blank_Page).Content1 is StackPanel)
                    Downloaded_Datepicker((ele as Blank_Page).Content1 as StackPanel);
                if ((ele as Blank_Page).Content1 is Grid)
                    Download_GridDate((ele as Blank_Page).Content1 as Grid);
            }


            if (ele is DatePicker)
            {
                DatePicker s = ele as DatePicker;

                s.Style = App.Current.Resources["DateNormal"] as Style;
            }

            if (ele is StackPanel)
            {
                Downloaded_Datepicker(ele as StackPanel);
            }
            if (ele is Grid)
            {
                Download_GridDate(ele as Grid);
            }
        }

    }

    public void Download_GridDate(Grid grid_down)
    {
        foreach (UIElement ele in grid_down.Children)
        {
            if (ele is Border)
            {
                UIElement ele_b = (ele as Border).Child;
                if (ele_b is DatePicker)
                {
                    DatePicker s = ele_b as DatePicker;
                    s.Style = App.Current.Resources["DateNormal"] as Style;

                }

                //here
                if (ele_b is Grid)
                {
                    Download_GridDate(ele_b as Grid);
                }
                if (ele_b is StackPanel)
                {
                    Downloaded_Datepicker(ele_b as StackPanel);
                }
            }
        }
    }

    public void PrintPage(StackPanel sp_Print)
    {
        foreach (UIElement sp_Element in sp_Print.Children)
        {
            if (sp_Element is StackPanel)
            {
                StackPanel printableSpnel=Downloaded_Data(sp_Element as StackPanel);
                //sp_Printing[count] = sp_Element as StackPanel;
                //count = count + 1;     
                sp_Printing[count] = printableSpnel;
                count = count + 1;
            }
        }
       WithDatePickerIcon = sp_Print as StackPanel;
        print_Doc.PrintPage += new EventHandler<PrintPageEventArgs>(print_Doc_PrintPage);
        print_Doc.Print("REOSK Printing");
    }

    void print_Doc_PrintPage(object sender, PrintPageEventArgs e)
    {

        if (count == i)
        {                
            e.PageVisual = sp_Printing[i];
            e.HasMorePages = false;
            Downloaded_Datepicker(WithDatePickerIcon as StackPanel);
        }            
        else
        {      
            e.PageVisual = sp_Printing[i];                
            e.HasMorePages = true;
            i = i + 1;
            Downloaded_Datepicker(WithDatePickerIcon as StackPanel); 
        }
    }

1 个答案:

答案 0 :(得分:0)

我不能说我完全理解你的代码,但有一个关键的问题可能是你的问题:

除非在BeginPrint上调用PrintDocument事件,否则您不应该在假设打印开始时修改程序的任何状态(甚至不禁用打印按钮或指示任何内容)。然后,您在EndPrint事件中恢复正常。

因此,大多数PrintPage方法(不应将其称为PrintPage*s*?)应该存在于侦听BeginPrint事件的处理程序中。

打印api的工作客户需要收听所有这三个事件。打印以BeginPrint开始,而不是拨打Print:用户可以通过点击取消来保证打印失败。