我试图显示引导程序模式,然后绑定其按钮。但是我无法通过显示模态的第一步。我正在使用.net core 3.1的Blazor客户端模板。我有一个名为Modal.razor的页面,其中包含从getbootstrap.com找到的引导程序模式。
@if (Show)
{
<div class="modal" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>Modal body text goes here.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary">Save changes</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
}
@code {
[Parameter]
public bool Show { get; set; } = false;
}
我在index.razor文件中称模态
@page "/"
<button @onclick="(()=>switchModal=!switchModal)">Switch Modal</button>
<Modal Show="switchModal"/>
@code{
bool switchModal = false;
}
您可能会说应在此处调用StateHasChanged。但是,即使我将模式代码复制并粘贴到index.razor中,也看不到任何内容。
答案 0 :(得分:11)
基于Kyle的答案,这是我对Blazor的第一个实验:使模式对话框组件采用任何标记或组件。
<div class="modal @modalClass" tabindex="-1" role="dialog" style="display:@modalDisplay; overflow-y: auto;">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">@Title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close" @onclick="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
@Body
</div>
<div class="modal-footer">
@Footer
</div>
</div>
</div>
</div>
@if (showBackdrop)
{
<div class="modal-backdrop fade show"></div>
}
@code {
[Parameter]
public RenderFragment Title { get; set; }
[Parameter]
public RenderFragment Body { get; set; }
[Parameter]
public RenderFragment Footer { get; set; }
private string modalDisplay = "none;";
private string modalClass = "";
private bool showBackdrop = false;
public void Open()
{
modalDisplay = "block;";
modalClass = "show";
showBackdrop = true;
}
public void Close()
{
modalDisplay = "none";
modalClass = "";
showBackdrop = false;
}
}
@page "/"
<h1>Hello, world!</h1>
Welcome to your new app.
<button class="btn btn-primary" @onclick="() => modal.Open()">Modal!</button>
<Modal @ref="modal">
<Title>This is a <em>Title!</em></Title>
<Body>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Omnes enim iucundum motum, quo sensus hilaretur.
<i>Quis istud possit, inquit, negare?</i>
<mark>Ego vero isti, inquam, permitto.</mark> Duo Reges: constructio interrete.
</p>
<FetchData />
<dl>
<dt><dfn>Stoici scilicet.</dfn></dt>
<dd>An hoc usque quaque, aliter in vita?</dd>
<dt><dfn>Erat enim Polemonis.</dfn></dt>
<dd>Quod cum accidisset ut alter alterum necopinato videremus, surrexit statim.</dd>
</dl>
</Body>
<Footer>
<button type="button" class="btn btn-primary">Save changes</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal" @onclick="() => modal.Close()">Close</button>
</Footer>
</Modal>
@code {
private Modal modal { get; set; }
}
答案 1 :(得分:5)
同样以 Kyle 的回答为基础,如果您在显示和类调整之间放置一个短暂的延迟,您可以维持引导淡入淡出效果。
@code {
...
public async Task OpenModal()
{
ModalDisplay = "block;";
await Task.Delay(100);//Delay allows bootstrap to perform nice fade animation
ModalClass = "show";
StateHasChanged();
}
public async Task CloseModal()
{
ModalClass = "";
await Task.Delay(250);
ModalDisplay = "none;";
StateHasChanged();
}
}
我也将 ModalClass 和 ModalDisplay 变量应用于背景元素
<div class="modal-backdrop fade @ModalClass" style="display: @ModalDisplay"></div>
我相信 bootstrap 可以通过这种方式更好地识别触发动画的状态变化
答案 2 :(得分:2)
可能有更好的方法来执行此操作,但这是一个使您入门的有效示例:
页面:
@page "/modal-test"
<BlazorApp1.Components.Modal @ref="Modal"></BlazorApp1.Components.Modal>
<button @onclick="() => Modal.Open()">Open Modal</button>
@code {
private BlazorApp1.Components.Modal Modal { get; set; }
}
组件:
<div class="modal @ModalClass" tabindex="-1" role="dialog" style="display:@ModalDisplay">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>Modal body text goes here.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary">Save changes</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal" @onclick="() => Close()">Close</button>
</div>
</div>
</div>
</div>
@if (ShowBackdrop)
{
<div class="modal-backdrop fade show"></div>
}
@code {
public Guid Guid = Guid.NewGuid();
public string ModalDisplay = "none;";
public string ModalClass = "";
public bool ShowBackdrop = false;
public void Open()
{
ModalDisplay = "block;";
ModalClass = "Show";
ShowBackdrop = true;
StateHasChanged();
}
public void Close()
{
ModalDisplay = "none";
ModalClass = "";
ShowBackdrop = false;
StateHasChanged();
}
}
要解决此问题的另一种方法是使用JSInterop tp调用$('#modalId')。modal()
通过执行以下操作,可以使组件的每个版本都有唯一的ID:
<div id="bootstrap-modal-@Guid"
,然后使用保存的ID通过jQuery调用.modal()。
答案 3 :(得分:1)
只为背景阴影添加淡入淡出类:
tempApp
答案 4 :(得分:0)
Kyle 的组件运行良好,但有人知道如何使用 jqueryUi draggable()/resizeable() 函数将可拖动和可调整大小的功能添加到引导模式吗?
我有一个纯 javascript 解决方案的链接:DRAG AND RESIZE BOOTSTRAP MODAL 本质上调用了模态 div 上的可调整大小和可拖动函数
<script src="https://code.jquery.com/ui/1.11.3/jquery-ui.min.js"></script>
<script type="text/javascript">
$('.modal-content').resizable({
//alsoResize: ".modal-dialog",
minHeight: 300,
minWidth: 300
});
$('.modal-dialog').draggable();
</script>
我已尝试将此脚本添加到我的 _Host.cshtml 页面,但没有效果。任何关于如何做到这一点的建议将不胜感激...
大卫
更新了答案
答案是在 OnAfterRenderAsync 覆盖中显式调用 javascript 函数,以将 JQuery UI 函数应用于模态 div。
例如
protected override async Task OnAfterRenderAsync(bool firstRender)
{
await jsRuntime.InvokeVoidAsync("setModalDraggableAndResizable");
await base.OnAfterRenderAsync(firstRender);
}
其中 setModalDraggableAndResizable
是 _Hosts.cshtml 中的 javascript 函数:
<script src="https://code.jquery.com/ui/1.11.3/jquery-ui.min.js"></script>
<script type="text/javascript">
function setModalDraggableAndResizable() {
$('.modal-content').resizable({
//alsoResize: ".modal-dialog",
minHeight: 300,
minWidth: 300
});
$('.modal-dialog').draggable();
}
</script>
并且模态现在可以拖动和调整大小...
答案 5 :(得分:0)
使用 Kyle 解决方案,当我点击背景时,我的对话框不会关闭。
我看到是z-index的问题:模态div的z-index为1050,背景div的z-index为1040,这样我就无法点击我的背景了。
我已经移动了对话框 div 内的背景并添加到模态对话框 div z-index > 1040
(ES: 1055)
我还在背景 div 中添加了 data-dismiss="modal" @onclick="() => Close()"
,现在它与“关闭”按钮一样有效。
<div class="modal @ModalClass" tabindex="-1" role="dialog" style="display:@ModalDisplay">
<div class="modal-dialog" role="document" style="z-index:1055">
...
</div>
@if (ShowBackdrop)
{
<div class="modal-backdrop fade show" data-dismiss="modal" @onclick="() => Close()"></div>
}
</div>