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

MAUI页面间数据共享方案咨询:计算器历史与计算页数据互通

MAUI计算器页面数据共享的简便方案

针对你开发带历史记录计算器的需求,以下几种方案比你提到的两种更简便:

1. 单例共享ViewModel(推荐)

创建一个包含计算历史的ViewModel,通过依赖注入注册为单例,让计算页面和历史页面共用这个实例,数据实时同步,完全不需要页面跳转传参或读写文件。

实现步骤:

  • 先写共享的ViewModel:
public class HistoryViewModel : ObservableObject
{
    private ObservableCollection<string> _calculationHistory = new();
    public ObservableCollection<string> CalculationHistory
    {
        get => _calculationHistory;
        set => SetProperty(ref _calculationHistory, value);
    }

    public void AddToHistory(string calculation)
    {
        CalculationHistory.Add(calculation);
    }
}
  • 在MauiProgram.cs里注册单例:
builder.Services.AddSingleton<HistoryViewModel>();
  • 计算页面注入ViewModel,计算完成后添加记录:
private readonly HistoryViewModel _historyVM;

public CalculatorPage(HistoryViewModel historyVM)
{
    InitializeComponent();
    _historyVM = historyVM;
}

private void OnCalculateClicked(object sender, EventArgs e)
{
    // 替换成你的实际计算逻辑
    string calculationRecord = $"{Num1.Text}+{Num2.Text}={result}";
    _historyVM.AddToHistory(calculationRecord);
}
  • 历史页面直接绑定ViewModel的集合,自动刷新:
<ListView ItemsSource="{Binding CalculationHistory}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <TextCell Text="{Binding}" />
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

这种方式符合MVVM架构,数据同步及时,适合大多数项目场景。

2. WeakReferenceMessenger消息传递

如果不想用ViewModel单例,可以用MAUI内置的消息传递机制,计算页面发送记录,历史页面接收并更新列表,耦合度低,实现简单。

实现步骤:

  • 定义消息类:
public class CalculationRecordMessage
{
    public string Record { get; set; }
}
  • 计算页面发送消息:
private void OnCalculateClicked(object sender, EventArgs e)
{
    string record = $"{Num1.Text}*{Num2.Text}={result}";
    WeakReferenceMessenger.Default.Send(new CalculationRecordMessage { Record = record });
}
  • 历史页面订阅消息,更新本地列表:
private ObservableCollection<string> _historyList = new();

public HistoryPage()
{
    InitializeComponent();
    HistoryListView.ItemsSource = _historyList;
    
    // 订阅消息
    WeakReferenceMessenger.Default.Register<CalculationRecordMessage>(this, (_, message) =>
    {
        _historyList.Add(message.Record);
    });
}

3. App类全局集合(快速原型用)

如果是小型项目或者快速做原型,直接在App类里定义静态集合,两边直接调用即可,最省事但不适合大型项目(全局变量不利于维护)。

实现步骤:

  • 在App.xaml.cs里添加静态集合:
public partial class App : Application
{
    public static ObservableCollection<string> CalculationHistory { get; set; } = new();
}
  • 计算页面添加记录:
private void OnCalculateClicked(object sender, EventArgs e)
{
    string record = $"{Num1.Text}-{Num2.Text}={result}";
    App.CalculationHistory.Add(record);
}
  • 历史页面绑定这个静态集合:
<ListView ItemsSource="{x:Static local:App.CalculationHistory}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <TextCell Text="{Binding}" />
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.08 23:10:44