Compartir a través de


IMarkupExtension<T>.ProvideValue Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Returns an object that is provided as the value of the target property for the markup extension.

Namespace:  System.Xaml
Assembly:  System.Windows (in System.Windows.dll)

Syntax

'Declaration
Function ProvideValue ( _
    serviceProvider As IServiceProvider _
) As T
T ProvideValue(
    IServiceProvider serviceProvider
)

Parameters

  • serviceProvider
    Type: System.IServiceProvider
    A service provider helper that can provide services for the markup extension.

Return Value

Type: T
The value to set on the property where the extension is applied.

Remarks

When a XAML processor processes a type node and member value that is a markup extension, it invokes the ProvideValue method of that markup extension and writes the result into the object graph or serialization stream. The XAML object writer passes service context to each such implementation through the serviceProvider parameter.

The following code shows an example of ProvideValue that does not use the serviceProvider parameter.

namespace FirstCME
{
    public class FirstMarkupExtension : IMarkupExtension<string>
    {
        public object Value1 { get; set; }
        public object Value2 { get; set; }

        public string ProvideValue(IServiceProvider serviceProvider)
        {
            double _val1 = Convert.ToDouble(Value1);
            double _val2 = Convert.ToDouble(Value2);
            return (_val1 + _val2).ToString();
        }
    }
}

Notes to Implementers

Common services returned by the default service provider that is typically available to a custom or existing IMarkupExtension<T> implementation for Silverlight include the following primary services.

Potentially, implementations of ProvideValue can ignore the serviceProvider parameter. This is viable for some basic scenarios, where no context at all is required for returning a value.

In typical usage, Silverlight XAML processing will provide a service provider to all value converter methods it invokes during XAML processing. However, for robustness, you should provide code paths for null values both for the service provider itself and for any requested service. Null values might occur if your markup extension is applied in some circumstance where the typical service support provided by a XAML parser infrastructure is not available.

Implementations that require a certain service to return a value are expected to throw exceptions if that service is not available. The recommended exception to throw is InvalidOperationException.

You may also throw exceptions if one of the arguments that your custom markup extension needs in order to provide a value is null, is invalid for its data type, or does not contain a value that your markup extension is capable of processing. The recommended exception to throw is InvalidOperationException.

Version Information

Silverlight

Supported in: 5

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.