Own and release resources
A page can leave the screen and still be retained by an event subscription. Pair each acquisition with a release at the same lifetime boundary.
This example observes a model only while the label is active. The model and view run on the UI thread. Change the model's message while the page is inactive, then return: activation reads the latest value before drawing again.
using System;
using Xui.Core.UI;
// This model and its subscribers are used only on the UI thread.
public sealed class StatusModel
{
public string Message { get; private set; } = "Ready";
public event Action? Changed;
public void SetMessage(string value)
{
if (Message == value)
return;
Message = value;
Changed?.Invoke();
}
}
public sealed class StatusSubscription : Label
{
private readonly StatusModel model;
public StatusSubscription(StatusModel model)
{
this.model = model;
Text = model.Message;
}
protected override void OnActivate()
{
base.OnActivate();
model.Changed += UpdateText;
UpdateText();
}
protected override void OnDeactivate()
{
model.Changed -= UpdateText;
base.OnDeactivate();
}
private void UpdateText() => Text = model.Message;
}
Match the pair
Subscribe in OnActivate and unsubscribe in OnDeactivate when updates matter only while the page is active. The explicit UpdateText() also catches changes that happened during inactivity. Keep a named delegate when you must remove the same handler later; two newly created anonymous lambdas are not a reliable unsubscribe pair.
A child-owned editor and its parent may become unreachable together, but a singleton publisher can keep an entire removed page alive. Look for that lifetime mismatch when memory grows after repeated navigation.
Use OnAttach/OnDetach for resources that must survive temporary inactivity but not removal from the tree. Release pointer capture and focus when an interactive view deactivates. Cancel page operations as shown in Async work and the UI thread.
Respect the resource contract
Not every handle is yours to dispose. IImage does not implement IDisposable; platform image pipelines own catalog resources. ImageView drops its handle on deactivation. Do not add a fictional image.Dispose() call to shared code, or cast a borrowed image to a backend-specific owner merely to release it.
GPU objects and native wrappers have their own ownership contracts. If your code creates a disposable buffer, pipeline, or native owner, keep it under a deliberate page or window owner and release it after its final use. Never retain a frame's IContext for asynchronous drawing: the runtime lends it for the current rendering call.
Window and application DisposeQueue lists can hold owned IDisposable objects. Queues dispose in insertion order, and application exit is not guaranteed on every host. Use page/window teardown for resources that should end before process shutdown.
Verify the lifetime
Open the page, trigger its work, navigate away, and repeat. Check that the publisher's callbacks stop, no stale async result changes a new page, and no animation continues for the old view. Reopen it and confirm subscriptions resume exactly once. Garbage collection can reclaim unreachable managed objects; it cannot repair a live subscription or enforce native ownership for you.