簡體   English   中英

MVVM多對話框令人頭疼

[英]MVVM multiple Dialogs headache

我正在使用caliburn micro。 我的問題是如何管理對話框。 最大的問題是,因為當不使用窗口時,您的代碼不會停止並等待。

所以我做了這樣的事情。

public void ShowDialog(IScreen dialogModel, Action<object> callback = null)
{
    ActivateItem(dialogModel);

    if (callback != null)
        dialogModel.Deactivated += delegate { callback(dialogModel); };

}

這有很多問題。例如,在某些情況下,我想顯示對話框A,然后在回調中想顯示對話框B,這是有問題的。為了避免重復,我必須為DoSomething編寫一個額外的函數。我松開所有其他局部變量。.當需要更多級別時,問題更大。

    showDialog(A, (cb)=>{

    if(...) {

    showDialog(B,(cb2)=>{
       DoSomething();

    });

    }
    else{
    DoSomething();
    }
});

也因為我想一次顯示一個對話框,所以我擴展了Collection.OneActive。 但這也有問題。 在停用事件回調中,如果我願意,我無法關閉所有功能! 因為它保留在內存中,所以觸發了“停用”后的下一個引用,即使您清除它,它也會再次出現。

如何在對話框之間移動時使用類來跟蹤狀態信息,而不是像原始示例中那樣嵌套閉包?

我認為您走在正確的道路上,但是似乎您有兩個問題:

  1. 您正在進行的嵌套數量不足以使代碼清晰。
  2. 您需要一種更好的方法來捕獲對話框之間的局部變量和狀態信息。

為了解決第一個問題,我建議將您的邏輯分解為不同的方法。 每次停用對話框時,您都可以使用一種方法來處理隨后應執行的邏輯。

要解決第二個問題,您可以嘗試創建一個類,該類負責存儲要在對話框之間傳遞的信息。 此類的實例可以作為參數傳遞給將在停用對話框后執行的每個方法。

這是完成此操作的方法:

導體類

public class DialogTestsViewModel : Conductor<object>.Collection.OneActive
{
    /// <summary>
    /// Shows a dialog and executes its callback if necessary.
    /// </summary>
    /// <param name="dialogModel">The dialog view model to be shown.</param>
    /// <param name="callback">The callback to be executed when dialog is closed.</param>
    public void ShowDialog(IScreen dialogModel, Action callback = null)
    {
        // Show the dialog.
        ActivateItem(dialogModel);

        // If there is a callback, call it when dialog is closed / deactivated.
        if (callback == null) return;
        dialogModel.Deactivated += (sender, args) => callback();
    }

    /// <summary>
    /// This method kicks off the dialog chain.
    /// </summary>
    public void ShowFirstDialog()
    {
        // Create a new context. This will hold state information
        // as it is passed between dialogs.
        var context = new TestDialogContext();

        // Create the first dialog's view model.
        var viewModel = new FirstDialogViewModel();

        // Show the first dialog.
        ShowDialog(viewModel, () => OnFirstDialogDeactivated(viewModel, context));
    }

    /// <summary>
    /// Logic to be executed when the first dialog is closed.
    /// </summary>
    /// <param name="viewModel">The first dialog's view model.</param>
    /// <param name="context">The state information.</param>
    private void OnFirstDialogDeactivated(FirstDialogViewModel viewModel, TestDialogContext context)
    {
        // Check the view model here and store state information inside the context.
        if (viewModel.SomethingIsChecked)
        {
            context.ShouldShowSecondDialog = true;
        }

        // Use information in the view model or the context to decide if we should show the next dialog.
        // You could also make a decision about which dialog to show next here.
        if (context.ShouldShowSecondDialog)
        {
            var secondDialog = new SecondDialogViewModel();
            ShowDialog(secondDialog, () => OnSecondDialogDeactivated(context));
        }
    }

    /// <summary>
    /// Logic to be executed when the second dialog is closed.
    /// </summary>
    /// <param name="context">The state information.</param>
    private void OnSecondDialogDeactivated(TestDialogContext context)
    {
        // Do more stuff.
    }
}

對話框上下文類

在這里您將存儲需要在對話框之間傳遞的狀態信息。 我僅在此處包括一個屬性作為示例,但是您可以在此處添加很多信息。

/// <summary>
/// State information to be passed between dialogs.
/// </summary>
public class TestDialogContext
{
    public bool ShouldShowSecondDialog { get; set; }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM