本文介绍了Blazor'Global'参数-在MainLayout.razor,NavMenu.razor,Counter.razor之间传递-来回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出一个默认" Blazor应用程序-使用Visual Studio 2019 Blazor服务器模板创建的.如何创建可从所有剃刀页面和组件访问的 GLOBAL 对象.

Given a 'default' Blazor app - created using the Visual Studio 2019 Blazor server template. How do you create a GLOBAL object that is accessible from all the razor pages and components.

如何在MainLayout,NavMenu,索引页面上使当前计数"值可见并动态更新?

How would you make the 'Current Count ' value - visible and 'dynamically' updated on the MainLayout, NavMenu, Index pages?

当前计数:@currentCount

Current count: @currentCount

  • 使用[Parameters]?
  • 使用[CascadingParameter]吗?
  • 创建静态类?
  • Blazor中推荐的方法是什么?
  • Use [Parameters]?
  • Use [CascadingParameter]?
  • Create a Static Class ?
  • What is the recommended approach in Blazor?

推荐答案

一种方法是使用注入到允许事件订阅的组件中的单例服务.我实际上是几天前与其他人讨论的.

One way to do this is to is to use a singleton service that is injected into the components that allows event subscriptions. I discussed this with someone else a few days ago actually.

查看这篇文章以获取有关动态更新页面的讨论从单例触发的事件中获取,这些事件在连接之间进行更新.

Check out this post for a discussion on dynamically updating pages from events fired out from a singleton, that updates across connections.

这是一个有效的演示,当您运行此项目,它会指示您打开另一个浏览器,复制/粘贴URL,并具有一个运行中的计数器(用于更新页面)和一个用于在页面之间传递消息的文本框.在框中输入内容,单击按钮,计数器将在所有连接的客户端的所有页面上更新,并且消息也将传递到所有页面并显示.跨浏览器和跨连接工作.

Here is a working demo that I adapted from another project I'm working on, when you run this project it instructs you to open another browser, copy / paste the URL, and has a running counter that updates and a text box to use to pass messages between pages. Enter something in the box, click the button, the counter updates on all pages from all connected clients and the message passes to all pages as well and displays. Works cross-browser and across connections.

可以使用类似的概念来构建诸如长期运行任务的后台通知,用户登录通知等内容.对于我的需求而言,它非常灵活.

A similar concept could be used to build up things like background notifications of long running tasks, user logon notifications, etc. Pretty flexible for my needs.

这篇关于Blazor'Global'参数-在MainLayout.razor,NavMenu.razor,Counter.razor之间传递-来回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 17:05