You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET Maui如何获知子页面返回并处理其返回数据?

.NET Maui子页面返回通知父页面的解决方案

针对你遇到的子页面返回时父页面无法收到通知并处理数据的问题,这里提供3种实用方案,直接基于你的现有代码修改:

方案1:等待导航任务完成后读取共享服务数据

PushAsync返回的Task会在子页面返回时完成,父页面可以通过等待这个任务,在任务结束后读取共享服务里的返回数据。

修改父页面的跳转代码:

// 把async void改成async Task,避免异步陷阱
private async Task NavigateToPage()
{
    MyItem item = _myItemList.Where(x => x.KeyString == "1958").First();
    _SharedService.Add<MyItem>("SharedServiceItemKey", item);

    // 直接await PushAsync,无需嵌套MainThread(PushAsync本身就在UI线程执行)
    await App.Current.MainPage.Navigation.PushAsync(new TestPage(_SharedService, item), true);
    
    // 子页面返回后,这里会继续执行,读取返回数据
    if (_SharedService.TryGet<MyItem>("SharedServiceReturnItemKey", out var updatedItem))
    {
        // 处理编辑后的数据,比如更新列表
        var index = _myItemList.FindIndex(x => x.KeyString == updatedItem.KeyString);
        if (index != -1)
        {
            _myItemList[index] = updatedItem;
            // 通知UI更新,比如刷新CollectionView等
        }
        // 读取后可以移除共享服务中的数据,避免残留
        _SharedService.Remove("SharedServiceReturnItemKey");
    }
}

子页面返回代码保持不变即可,注意确保_item是编辑后的最新对象。

方案2:使用事件委托传递数据

子页面定义一个事件,父页面跳转时订阅该事件,子页面返回前触发事件把编辑后的数据传递给父页面,无需依赖共享服务。

子页面修改:

// 定义事件,用于传递编辑后的MyItem
public event Action<MyItem>? ItemUpdated;

private readonly MyItem _item;

// 构造函数保留现有逻辑
public TestPage(ISharedService sharedService, MyItem item)
{
    InitializeComponent();
    _SharedService = sharedService;
    _item = item;
    // 绑定编辑控件到_item,确保修改后的数据同步到_item
}

private async void NavigateBack()
{
    // 触发事件,传递编辑后的数据
    ItemUpdated?.Invoke(_item);
    await Shell.Current.GoToAsync("..");
}

父页面修改:

private async Task NavigateToPage()
{
    MyItem item = _myItemList.Where(x => x.KeyString == "1958").First();
    
    var testPage = new TestPage(_SharedService, item);
    // 订阅子页面的事件
    testPage.ItemUpdated += OnItemUpdated;
    
    await App.Current.MainPage.Navigation.PushAsync(testPage, true);
}

// 事件处理方法,接收编辑后的数据
private void OnItemUpdated(MyItem updatedItem)
{
    // 处理数据,比如更新列表
    var index = _myItemList.FindIndex(x => x.KeyString == updatedItem.KeyString);
    if (index != -1)
    {
        _myItemList[index] = updatedItem;
        // 更新UI
    }
}

方案3:使用MessagingCenter发送消息(跨页面通信)

利用MAUI内置的MessagingCenter,子页面返回时发送包含更新数据的消息,父页面订阅该消息即可接收。

父页面修改:

protected override void OnAppearing()
{
    base.OnAppearing();
    // 订阅消息,指定消息标识和数据类型
    MessagingCenter.Subscribe<TestPage, MyItem>(this, "ItemUpdatedMessage", (sender, updatedItem) =>
    {
        // 处理更新的数据
        var index = _myItemList.FindIndex(x => x.KeyString == updatedItem.KeyString);
        if (index != -1)
        {
            _myItemList[index] = updatedItem;
            // 更新UI
        }
    });
}

protected override void OnDisappearing()
{
    base.OnDisappearing();
    // 取消订阅,避免内存泄漏
    MessagingCenter.Unsubscribe<TestPage, MyItem>(this, "ItemUpdatedMessage");
}

private async Task NavigateToPage()
{
    MyItem item = _myItemList.Where(x => x.KeyString == "1958").First();
    _SharedService.Add<MyItem>("SharedServiceItemKey", item);

    await App.Current.MainPage.Navigation.PushAsync(new TestPage(_SharedService, item), true);
}

子页面修改:

private async void NavigateBack()
{
    // 发送消息,传递编辑后的数据
    MessagingCenter.Send(this, "ItemUpdatedMessage", _item);
    await Shell.Current.GoToAsync("..");
}

内容的提问来源于stack exchange,提问作者Skyraider

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 11:41:22