Class InfiniteQueryViewModel<TArgs, TData, TPageParam>
- Namespace
- DotNetQuery.Mvvm
- Assembly
- DotNetQuery.Mvvm.dll
An INotifyPropertyChanged view model wrapping an IInfiniteQuery<TArgs, TData, TPageParam> for MVVM-based UI frameworks (MAUI, WPF, WinUI, UNO, Avalonia). Exposes the paginated query state as bindable properties and marshals all change notifications onto the UI thread via an IUiDispatcher.
The view model holds a live subscription to State for its entire lifetime — this is what keeps the underlying cache entry retained and triggers deferred stale fetches. Dispose the view model when its page is torn down; a forgotten dispose keeps the cache entry alive indefinitely.
Pages are not flattened: TData is a single page and is not necessarily
enumerable. Flatten in the owning page view model, e.g. by syncing an
ObservableCollection<T> from Pages off
StateChanged.
public class InfiniteQueryViewModel<TArgs, TData, TPageParam> : BindableBase, INotifyPropertyChanged, IDisposable where TData : class
Type Parameters
TArgsThe type of the parameters passed to the fetcher.
TDataThe type of a single page of data. Constrained to reference types.
TPageParamThe type of the page parameter (cursor/offset).
- Inheritance
-
InfiniteQueryViewModel<TArgs, TData, TPageParam>
- Implements
- Inherited Members
Constructors
InfiniteQueryViewModel(IInfiniteQuery<TArgs, TData, TPageParam>, IUiDispatcher?)
Wraps an existing infinite query. The view model does not take ownership:
Dispose() releases only the view model's own state subscription,
and the caller remains responsible for disposing query.
public InfiniteQueryViewModel(IInfiniteQuery<TArgs, TData, TPageParam> query, IUiDispatcher? dispatcher = null)
Parameters
queryIInfiniteQuery<TArgs, TData, TPageParam>The infinite query to wrap.
dispatcherIUiDispatcherThe UI-thread dispatcher. When
null, Current is captured; construct the view model on the UI thread or pass a dispatcher explicitly.
InfiniteQueryViewModel(IQueryClient, InfiniteQueryOptions<TArgs, TData, TPageParam>, IUiDispatcher?)
Creates a new infinite query observer via CreateInfiniteQuery<TArgs, TData, TPageParam>(InfiniteQueryOptions<TArgs, TData, TPageParam>) and wraps it. The view model owns the created query and disposes it in Dispose(). This is the recommended path for page view models that receive an IQueryClient through dependency injection.
public InfiniteQueryViewModel(IQueryClient client, InfiniteQueryOptions<TArgs, TData, TPageParam> options, IUiDispatcher? dispatcher = null)
Parameters
clientIQueryClientThe query client used to create the observer.
optionsInfiniteQueryOptions<TArgs, TData, TPageParam>The infinite query options.
dispatcherIUiDispatcherThe UI-thread dispatcher. When
null, Current is captured; construct the view model on the UI thread or pass a dispatcher explicitly.
Properties
CancelCommand
Triggers Cancel(). Enabled while any fetch is in flight.
public ICommand CancelCommand { get; }
Property Value
CurrentState
The raw state snapshot, for bindings and converters that need the whole state.
public InfiniteQueryState<TData, TPageParam> CurrentState { get; }
Property Value
- InfiniteQueryState<TData, TPageParam>
Error
The exception from the most recent failed fetch. null when not in a failure state.
public Exception? Error { get; }
Property Value
FetchNextPageCommand
Triggers FetchNextPage(). Enabled only
when HasNextPage is true and no fetch is currently in flight.
public ICommand FetchNextPageCommand { get; }
Property Value
FetchPreviousPageCommand
Triggers FetchPreviousPage(). Enabled only
when HasPreviousPage is true and no fetch is currently in flight.
public ICommand FetchPreviousPageCommand { get; }
Property Value
HasData
true when at least one page has been successfully loaded.
public bool HasData { get; }
Property Value
HasError
true when Error is not null.
public bool HasError { get; }
Property Value
HasNextPage
true when a next page is available to fetch.
public bool HasNextPage { get; }
Property Value
HasPreviousPage
true when a previous page is available to fetch.
public bool HasPreviousPage { get; }
Property Value
IsFailure
true when the most recent fetch failed.
public bool IsFailure { get; }
Property Value
IsFetching
true while a full refetch is in flight. See IsFetchingNextPage and
IsFetchingPreviousPage for pagination fetches, which do not set this.
public bool IsFetching { get; }
Property Value
IsFetchingNextPage
true while a next-page fetch is in progress. Existing pages remain visible.
public bool IsFetchingNextPage { get; }
Property Value
IsFetchingPreviousPage
true while a previous-page fetch is in progress. Existing pages remain visible.
public bool IsFetchingPreviousPage { get; }
Property Value
IsIdle
true when the query is idle.
public bool IsIdle { get; }
Property Value
IsLoading
true only during the first load — fetching with no pages loaded yet.
Bind a full-page loading indicator to this and a subtle refresh indicator to
IsFetching.
public bool IsLoading { get; }
Property Value
IsSuccess
true when the most recent fetch succeeded.
public bool IsSuccess { get; }
Property Value
PageParams
The page parameters used to fetch each page in Pages.
PageParams[i] was the parameter used to fetch Pages[i].
public IReadOnlyList<TPageParam> PageParams { get; }
Property Value
- IReadOnlyList<TPageParam>
Pages
All currently loaded pages, ordered from oldest to newest.
public IReadOnlyList<TData> Pages { get; }
Property Value
- IReadOnlyList<TData>
Query
The wrapped query — the escape hatch for members without a bindable counterpart (Invalidate(), Detach(), or direct Rx composition via Success, Failure, Settled).
public IInfiniteQuery<TArgs, TData, TPageParam> Query { get; }
Property Value
- IInfiniteQuery<TArgs, TData, TPageParam>
RefetchCommand
Triggers Refetch() for the whole page set. Disabled while any fetch is in flight.
public ICommand RefetchCommand { get; }
Property Value
Status
The current lifecycle status of the query.
public QueryStatus Status { get; }
Property Value
Methods
Dispose()
Releases the view model's state subscription — which lets the cache entry's eviction clock start — and disposes the wrapped query if this view model created it.
public void Dispose()
Dispose(bool)
Releases resources. Override in derived view models to dispose additional state; always call the base implementation.
protected virtual void Dispose(bool disposing)
Parameters
SetArgs(TArgs)
Sets the args that drive the cache key and fetching. When args change, the query switches to the cache entry for the newly derived key and triggers a fetch if the query is enabled.
public void SetArgs(TArgs args)
Parameters
argsTArgs
SetEnabled(bool)
Enables or disables the query. When false, fetching is suspended even if the query
is invalidated or new args are pushed. Pass true to resume.
public void SetEnabled(bool enabled)
Parameters
enabledbool
Events
StateChanged
Raised on the UI thread after every PropertyChanged notification for an applied state. Use this — instead of a second raw subscription to Query's state — for page-specific work such as syncing an ObservableCollection<T> from Pages.
public event EventHandler? StateChanged