Condividi tramite


L'impostazione predefinita per MediaType nell'app di supporto per la stampa non viene applicata

Questo articolo consente di risolvere un problema a causa del quale l'impostazione predefinita per MediaType nell'app di supporto di stampa (PSA) non viene applicata in alcuni scenari specifici.

Sintomi

Prendi in considerazione lo scenario seguente:

  • Si sta sviluppando un PSA per stampanti basate su IPP.
  • Il PSA viene eseguito in Windows 10, versione 22H2 o Windows 11, versione 23H2.
  • È possibile aggiungere opzioni PageMediaType aggiuntive alle funzionalità del dispositivo di stampa (PDC) nel PrintSupportExtensionSession.PrintDeviceCapabilitiesChanged gestore eventi di PSA.

In questo scenario, l'opzione predefinita non è selezionata come previsto quando viene visualizzata la schermata delle impostazioni di stampa nel PSA per la prima volta. Una volta salvate le impostazioni, il problema non si verifica più.

Ad esempio, si aggiunge un oggetto ContosoMediaType alla PageMediaType funzionalità e si configura il ticket di stampa come indicato di seguito. In questo caso, , AutoSelectche è impostato sul valore predefinito di nello schema di true stampa, deve essere l'impostazione predefinita, ma viene usata un'altra opzione come impostazione predefinita.

<!-- media-type-supported -->
<psk:PageMediaType psf2:psftype="Feature">
    <psk:AutoSelect psf2:psftype="Option" psf2:default="true"/>  <!--Set the default value for AutoSelect-->
    <psk:PhotographicGlossy psf2:psftype="Option" psf2:default="false"/>
    <psk:Photographic psf2:psftype="Option" psf2:default="false"/>
    <contoso:ContosoMediaType psf2:psftype="Option" psf2:default="false"/>
</psk:PageMediaType>

Causa

Questo problema si verifica perché Windows.Graphics.Printing.Workflow.dll non converte correttamente il PDC in un ticket di stampa. Si tratta di un problema con il servizio Flusso di lavoro di stampa di Windows.

Soluzione alternativa

Per risolvere questo problema, salvare l'opzione predefinita nel PrintSupportExtensionSession.PrintDeviceCapabilitiesChanged gestore eventi di PSA e ripristinare il valore salvato durante l'inizializzazione della finestra di dialogo delle impostazioni di stampa. Gli esempi di codice nei passaggi seguenti si basano sugli esempi di PSA generici forniti da Microsoft.

  1. Aggiungere il codice seguente alla LocalStorageUtil classe nell'attività in background di PSA:

    // Method to check if the default option has already been set in the PrintTicket
    public static bool IsAlreadyLoadedDefaultValue()
    {
        try
        {
            return (bool)ApplicationData.Current.LocalSettings.Values["IsLoadedDefaultValue"];
        }
        catch (NullReferenceException e)
        {
            return false;
        }
    }
    
    // Method to indicate that the default option has been applied to the PrintTicket
    public static void LoadedDefaultValue(bool _bLoaded)
    {
        ApplicationData.Current.LocalSettings.Values["IsLoadedDefaultValue"] = _bLoaded;
    }
    
    // Method to save the default option value of the specified feature
    public static void SetPdcDefaultValue(string _Feature, string _DefaultValue)
    {
        System.Diagnostics.Debug.WriteLine("SetPdcDefaultValue: Feature=" + _Feature + ", Default=" + _DefaultValue);
        ApplicationData.Current.LocalSettings.Values[_Feature] = _DefaultValue;
    }
    
    // Method to return the default option value of the specified feature
    public static string GetPdcDefaultValue(string _Feature)
    {
        try
        {
            return (string)ApplicationData.Current.LocalSettings.Values[_Feature];
        }
        catch (NullReferenceException e)
        {
            System.Diagnostics.Debug.WriteLine(e.Message);
            return null;
        }
    }
    
  2. PrintSupportExtensionSession.PrintDeviceCapabilitiesChanged Nel gestore eventi chiamare il SaveDefaultValues() metodo per salvare i valori predefiniti impostati nel PDC.

    private void OnSessionPrintDeviceCapabilitiesChanged(PrintSupportExtensionSession sender, PrintSupportPrintDeviceCapabilitiesChangedEventArgs args)
    {
        var pdc = args.GetCurrentPrintDeviceCapabilities();
        // Add the custom namespace URI to the XML document.
        pdc.DocumentElement.SetAttribute("xmlns:contoso", "http://schemas.contoso.com/keywords");
        // Add the custom media type.
        AddCustomMediaType(ref pdc, "http://schemas.contoso.com/keywords", "contoso:ContosoMediaType");
    
        // Call the method to save the default values of the PDC.
        SaveDefaultValues(ref pdc);
    
        args.UpdatePrintDeviceCapabilities(pdc);
        args.SetPrintDeviceCapabilitiesUpdatePolicy(
            PrintSupportPrintDeviceCapabilitiesUpdatePolicy.CreatePeriodicRefresh(System.TimeSpan.FromMinutes(1)));
        args.GetDeferral().Complete();
    }
    
  3. Inizializzare gli elementi ComboBox durante l'installazione iniziale utilizzando i valori predefiniti salvati nella LocalStorageUtil classe .

    private ComboBox CreatePrintTicketFeatureComboBox(PrintTicketFeature feature, bool useDefaultEventHandler = true)
    {
        if (feature == null)
        {
            return null;
        }
    
        var comboBox = new ComboBox
        {
            // Header is displayed in the UI, on top of the ComboBox.
            Header = feature.DisplayName
        };
        // Construct a new list since IReadOnlyList does not support the 'IndexOf' method.
        var options = new ObservableCollection<PrintTicketOption>(feature.Options);
        // Provide the combo box with a list of options to select from.
        comboBox.ItemsSource = options;
        // Set the selected option to the option set in the print ticket.
        PrintTicketOption selectedOption;
    
        // Load the default value of the PDC from LocalStorageUtil only once.
        string defaultOption = LocalStorageUtil.GetPdcDefaultValue(feature.Name);
        if (!LocalStorageUtil.IsAlreadyLoadedDefaultValue() && defaultOption != null)
        {
            selectedOption = options[0];
            foreach (var option in options)
            {
                if (option.Name == defaultOption)
                {
                    selectedOption = option;
                    break;
                }
            }
        }
        else
        {
            var featureOption = feature.GetSelectedOption();
            try
            {
                selectedOption = options.Single((option) => (
                    option.Name == featureOption.Name && option.XmlNamespace == featureOption.XmlNamespace));
            }
            // Catch exceptions, because there can be multiple features with the "None" feature name.
            // We need to handle those features separately.
            catch (System.SystemException exception)
            {
                var nameAttribute = featureOption.XmlNode.Attributes.GetNamedItem("name");
                var attribute = featureOption.XmlNode.OwnerDocument.CreateAttribute("name");
    
                selectedOption = options.Single((option) => (
                    option.DisplayName == featureOption.DisplayName
                    && option.Name == featureOption.Name
                    && option.XmlNamespace == featureOption.XmlNamespace));
            }
        }
    
        comboBox.SelectedIndex = options.IndexOf(selectedOption);
    
        // Indicate that we have completed loading the default value from LocalStorageUtil.
        LocalStorageUtil.LoadedDefaultValue(true);
        // ...
    }