Utilities
CascadingValueProvider
BitCascadingValueProvider replaces a stack of nested CascadingValue components with a single list, so a whole set of values reaches the descendant components from one place. Each value carries its own cascaded type, an optional name and the IsFixed and Enabled flags, and can be given eagerly, deferred until it is needed, re-read on every render or told to watch itself for changes. The list is written inline with the Values parameter or built up with the ValueList parameter, a later value shadows an earlier one of the same type or name, and changing a value refreshes the consumers on its own.
Notes
Every value here becomes a real CascadingValue component, so everything that holds for a hand-written one holds here too: consumers match by type and by name, a value that changes re-renders only the components that consume it, and an IsFixed value is never watched. Reach for a root-level cascading value (AddCascadingValue on the service collection) instead when the same value has to reach every component of the app rather than one subtree.
Usage
Every example is live. Open its code to see exactly what produced the component running underneath.
Basic
Values
ValueList
Nesting
ValueType
IsFixed
Enabled
Notifications
Auto notifications
Lazy values
Computed values
API
Every parameter, public member, sub-class and enum this component exposes.
BitCascadingValueProvider parameters
| Name | Type | Default value | Description |
|---|---|---|---|
| ChildContent | RenderFragment? | null | The content to which the values should be provided. |
| Values | IEnumerable<BitCascadingValue>? | null | The cascading values to be provided for the children. These values are provided after (so they take precedence over) the ones of the ValueList parameter. |
| ValueList | BitCascadingValueList? | null | The cascading value list to be provided for the children. These values are provided before (so they can be overridden by) the ones of the Values parameter. |
BitCascadingValue properties
Defines a value that can be cascaded to descendant components.
| Name | Type | Default value | Description |
|---|---|---|---|
| Value | object? | null | The value to be provided. Assigning a value that is not compatible with the ValueType throws an ArgumentException, and assigning a different value raises the Changed event. A lazy factory runs the first time it is read, a computed one on every read. |
| Name | string? | null | The optional name of the cascading value. An empty or white-space name is treated as no name at all, and the consumers match it case-insensitively. Renaming a live value re-creates the underlying CascadingValue component so that the consumers are matched again under the new name. |
| IsFixed | bool | false | If true, indicates that Value will not change, so consumers are not subscribed for change notifications. Toggling it re-creates the underlying CascadingValue component. |
| Enabled | bool | true | Determines whether this cascading value is provided to the children. A disabled value is skipped as if it was never added, so an outer or root level cascading value of the same type or name shows through. |
| AutoNotify | bool | false | Watches the cascaded value itself, so an INotifyPropertyChanged or INotifyCollectionChanged value raises Changed on its own. The subscription is only held while a provider is listening, so the cascaded object never keeps this value alive. |
| ValueType | Type | Value?.GetType() | The type to use as the TValue of the CascadingValue component. It is read-only and defaults to the runtime type of the value, so it must be provided explicitly for null values, nullable value types, base types and interfaces. |
| IsValueCreated | bool | true | Whether the value is already available. It is only false for a lazily created value whose factory has not run yet. |
| IsComputed | bool | false | Whether the value is produced by a factory that runs on every read rather than being stored once, which is what the Computed factory methods create. |
| Changed | event Action<BitCascadingValue>? | Raised whenever the value changes, which is what lets the hosting BitCascadingValueProvider re-render and push the new value down to the consumers on its own. | |
| ChangedAsync | event Func<BitCascadingValue, Task>? | The awaitable counterpart of Changed, which is what the provider subscribes to and what makes NotifyChangedAsync complete only once the re-render is done. | |
| NotifyChanged() | void | Raises the Changed and ChangedAsync events on demand, which is how a cascaded object that is mutated in place is pushed down to the consumers. | |
| NotifyChangedAsync() | Task | The awaitable form of NotifyChanged, whose task completes once every listening provider has re-rendered, like CascadingValueSource.NotifyChangedAsync does. | |
| From<T>(T value, string? name = null, bool isFixed = false, bool enabled = true) | BitCascadingValue | Creates a cascading value whose ValueType is the static type of T. | |
| Fixed<T>(T value, string? name = null, bool enabled = true) | BitCascadingValue | Creates a fixed (IsFixed) cascading value whose ValueType is the static type of T. | |
| Lazy<T>(Func<T> valueFactory, string? name = null, bool isFixed = false, bool enabled = true) | BitCascadingValue | Creates a cascading value whose value is produced by the factory the first time it is actually needed, so a disabled or shadowed value is never built. The factory runs at most once. | |
| Computed<T>(Func<T> valueFactory, string? name = null, bool isFixed = false) | BitCascadingValue | Creates a cascading value that is re-read from the factory every time it is provided, so one long lived value keeps tracking the state it is derived from. | |
| Observed<T>(T value, string? name = null, bool enabled = true) | BitCascadingValue | Creates a cascading value with AutoNotify turned on, so a value reporting its own mutations refreshes the consumers without any call to NotifyChanged. |
BitCascadingValueList properties
A helper class to ease the using of a list of the BitCascadingValue.
| Name | Type | Default value | Description |
|---|---|---|---|
| Add<T>(T value, string? name = null, bool isFixed = false, bool enabled = true) | void | Adds a typed BitCascadingValue to the list, cascading the value as the static type of T. | |
| Add(BitCascadingValue? value) | void | Adds an already created BitCascadingValue to the list. A null item is ignored. | |
| Add(object? value, Type valueType, string? name = null, bool isFixed = false, bool enabled = true) | void | Adds a BitCascadingValue with an explicit ValueType to the list, for when the cascaded type is only known at runtime. | |
| AddIf<T>(bool condition, T value, string? name = null, bool isFixed = false, bool enabled = true) | void | Adds a typed BitCascadingValue to the list only when the given condition is true. | |
| AddIf(bool condition, BitCascadingValue? value) | void | Adds an already created BitCascadingValue to the list only when the given condition is true, which paired with a lazy value keeps the value of a conditional entry from being built at all. | |
| AddFixed<T>(T value, string? name = null) | void | Adds a fixed (IsFixed) typed BitCascadingValue to the list. | |
| AddFixed(object? value, Type valueType, string? name = null) | void | Adds a fixed (IsFixed) BitCascadingValue with an explicit ValueType to the list. | |
| AddLazy<T>(Func<T> valueFactory, string? name = null, bool isFixed = false, bool enabled = true) | void | Adds a typed BitCascadingValue whose value is produced by the factory the first time it is actually needed. The factory runs at most once. An overload taking an explicit ValueType is available as well. | |
| AddComputed<T>(Func<T> valueFactory, string? name = null, bool isFixed = false) | void | Adds a typed BitCascadingValue that is re-read from the factory on every render, so a list built once keeps tracking the state its values are derived from. An overload taking an explicit ValueType is available as well. | |
| AddObserved<T>(T value, string? name = null, bool enabled = true) | void | Adds a typed BitCascadingValue that watches the value itself, so an INotifyPropertyChanged or INotifyCollectionChanged object refreshes the consumers on its own. | |
| Find<T>(string? name = null) | BitCascadingValue? | Finds the entry that the given type and name resolve to, which is the last one matching both, since that is the one shadowing all the others. An overload taking an explicit ValueType is available as well. | |
| Contains<T>(string? name = null) | bool | Whether the list holds an entry of the static type of T carrying the given name, regardless of whether it is enabled. | |
| Remove<T>(string? name = null) | bool | Removes every entry of the static type of T carrying the given name, and reports whether anything was removed. An overload taking an explicit ValueType is available as well. | |
| Set<T>(T value, string? name = null, bool isFixed = false, bool enabled = true) | void | Replaces every entry of the static type of T carrying the given name with a new one, or adds it when the list has none, so the list ends up with exactly one entry per type and name. |
Feedback
Found a mistake, a gap, or something that could be clearer? Every page and every component is one click from its source.