1

我是 Elmish.Xamarin 和 Xamarin 本身的新手。我正在尝试使用这个计算器示例构建一个应用程序。主要区别在于,在此示例中,UI 会根据用户输入进行更新,并且update函数是纯函数,仅评估模型的下一个状态:

let update msg model =
    match msg with
    | Clear -> Initial
    | Digit digit ->
        match model with
        | Initial | Error | Result _ -> Operand (double digit)
        | Operand op -> Operand (double (string op + string digit))
        | OperandOperator (operand, operator) -> OperandOperatorOperand (operand, operator, double digit)
        | OperandOperatorOperand (op1, operator, op2) -> OperandOperatorOperand (op1, operator, double (string op2 + string digit))
    | Operator operator ->
        match model with
        | Initial | Error -> model
        | Result operand // previously calculated result is now the first operand
        | Operand operand | OperandOperator (operand, _) -> OperandOperator(operand, operator) 
        | OperandOperatorOperand _ -> calculate model msg
        | Equals -> calculate model msg

我需要一个不同的场景:用户的输入不会直接影响 UI,而是存储用户的消息并按计划评估下一个状态,所以在我的应用程序的核心中,我传递了updateUI: gameState -> unitfunc:

let gameAgentFn (mailboxNetwork: MailboxNetwork) updateUi gameState cmd =
    let timerAgent = timerAgent mailboxNetwork
    match gameState.gameFrame with
    | Frame field ->
        let gameState = Game.updateGameState gameState cmd
        timerAgent.Post Next
        updateUi gameState
        gameState

问题是我不明白,要传递什么函数以及如何与这个设计建立交互,因为据我所见,它希望我传递纯函数来评估下一个状态,其余的都在下面完成。

4

0 回答 0