locked
PAGE SETUP (AGAIN) RRS feed

  • Question

  • The C# Print Sample does not include the capability to setup the page margins; back in July I submitted a query on this issue (see the thread PAGE SETUP) and Tim Heuer provided a great solution that in fact worked perfectly ... however, after upgrading to Win 8.1 and VS 2013 the same technique gives the following error message:

    "There's a problem with this setting. Continue with the existing settings or try again."

    This error message is erratic: sometimes it appears near the top margin, someties near the left margint, sometimes near all the margin fields. Here is a snapshot of the relevant code, copied from Tim Heuer's:

    protected override void PrintTaskRequested()
    {
       
    PrintTask printTask = null;
        printTask = e.Request.CreatePrintTask(
    "C# Printing SDK Sample",
            sourceRequestedArgs => {
              
    IList<string> displayedOptions = printTask.Options.DisplayedOptions;
              
              
    // add items for page margins
              
    PrintTaskOptionDetails optionDetails =
                  
    PrintTaskOptionDetails.GetFromPrintTaskOptions(printTask.Options);
               optionDetails.CreateTextOption(
    "idTopMargin", "Top margin (inches)");
               displayedOptions.Add(
    "idTopMargin");
               optionDetails.Options[
    "idTopMargin"].TrySetValue("1.0");
    (same for Bottom, Left, Right)
              
    // set handler for option changing
               optionDetails.OptionChanged += OnOptionDetailsOptionChanged;
               ...
    }

    // validate change in options
    async void OnOptionDetailsOptionChanged(PrintTaskOptionDetails sender, PrintTaskOptionChangedEventArgs args)
    {
       
    if (args.OptionId == null) return;
       
    // else
       
    string optionid = args.OptionId.ToString();
       
    string strValue = sender.Options[optionid].Value.ToString();
       
    string errorText = String.Empty;
       
    double value = 0;
       
    switch (optionid)
        {
           
    case "idTopMargin":
           
    case "idBottomMargin":
           
    case "idLeftMargin":
           
    case "idRightMargin":
                
    if (!Double.TryParse(strValue, out value))
                   errorText =
    "Value must be numeric";
               
    else if (value < 0 || value > 2)
                   errorText =
    "Value must be between 0 and 2";
               
    break;
        }
        sender.Options[optionid].ErrorText = errorText;
       
    // if there is no error, then invalidate the preview
       
    if (String.IsNullOrEmpty(errorText))
        {
           
    await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
            { printDocument.InvalidatePreview(); });
        }
    }

    I verified that if I comment out the line 'optionDetails.OptionChanged += OnOptionDetailsOptionChanged;' the error message will not display (but of course the margin fields do not work either); if I disable the function 'OnOptionDetailsOptionChanged' by placing an immediate return as first instruction, the error messages continue to appear.

    Any suggestion?

    Wednesday, December 25, 2013 10:59 AM

All replies