Blazor 相当于 WPF ShowDialog()?

D. *_*ile 6 c# blazor blazor-server-side

我正在尝试将一些代码从 WPF 迁移到 Blazor。WPF 代码依赖 ShowDialog() 来显示模式对话框并暂停执行,直到模式关闭。是否有(服务器端)Blazor 等效项允许基于 C# 控制流,例如,用户是否在模式对话框中单击了确认与取消?

Mic*_*ton 13

你可以添加一个按钮

<button class="btn btn-primary"
  @onclick="AddNewForecast">
  Add New Forecast
</button>
Run Code Online (Sandbox Code Playgroud)

调用一个将值设置为 true 的方法

bool ShowPopup = false;
void AddNewForecast()
{
    // Open the Popup
    ShowPopup = true;
}
Run Code Online (Sandbox Code Playgroud)

并且该值包含在使用引导程序模态控件(class="modal")的代码中:

        @if (ShowPopup)
        {
            <!-- This is the popup to create or edit a forecast -->
            <div class="modal" tabindex="-1" style="display:block" role="dialog">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h3 class="modal-title">Edit Forecast</h3>
                            <!-- Button to close the popup -->
                            <button type="button" class="close"
                                    @onclick="ClosePopup">
                                <span aria-hidden="true">X</span>
                            </button>
                        </div>
                        <!-- Edit form for the current forecast -->
                        <div class="modal-body">
                            <input class="form-control" type="text"
                                   placeholder="Celsius forecast"
                                   @bind="objWeatherForecast.TemperatureC" />
                            <input class="form-control" type="text"
                                   placeholder="Fahrenheit forecast"
                                   @bind="objWeatherForecast.TemperatureF" />
                            <input class="form-control" type="text"
                                   placeholder="Summary"
                                   @bind="objWeatherForecast.Summary" />
                            <br />
                            <!-- Button to save the forecast -->
                            <button class="btn btn-primary"
                                    @onclick="SaveForecast">
                                Save
                            </button>
                        </div>
                    </div>
                </div>
            </div>
        }
Run Code Online (Sandbox Code Playgroud)

您将有一个“模态”弹出窗口

  • 我想这就能达到他的目的了。但是,当他使用 WPF 时,他很可能使用 MVVM,因此通常在视图模型中会有一些代码显示“ShowMessage”,并在该对话框之后继续执行。所以这是代码流程上的细微差别。也许在思考观点划分和逻辑划分时也是有区别的。 (2认同)