在Blazor中从_host.cshtml使用预渲染时,如何停止渲染两次(对于非数据检索组件)?

在Blazor中从_host.cshtml使用预渲染时,如何停止渲染两次(对于非数据检索组件)?,blazor,server-side-rendering,blazor-webassembly,preloading,Blazor,Server Side Rendering,Blazor Webassembly,Preloading,我在razor页面的代码段中初始化了一些变量,当我开始在应用程序中使用_Host.cshtml从服务器进行预渲染时,它们初始化了两次。在UI中加载客户端部件的第二次渲染期间,如何避免这些初始化 public string isVisible="hidden"; protected override void OnAfterRender(bool firstRender) { base.OnAfterRender(firstRender); if (firstR

我在razor页面的代码段中初始化了一些变量,当我开始在应用程序中使用_Host.cshtml从服务器进行预渲染时,它们初始化了两次。在UI中加载客户端部件的第二次渲染期间,如何避免这些初始化

public string isVisible="hidden";
protected override void OnAfterRender(bool firstRender)
{
    base.OnAfterRender(firstRender);
    if (firstRender)
    {
        isVisible = "visible";
    }
}
此变量将返回到第二次渲染中的值“hidden”。请帮助了解如何防止这种情况。

从以下网站:

在Blazor服务器应用程序中,当RenderMode为ServerPrerendered时,组件最初作为页面的一部分静态呈现。一旦浏览器建立回服务器的信号器连接,组件将再次呈现并交互。如果存在用于初始化组件的OnInitialized{Async}生命周期方法,则该方法将执行两次:

当组件以静态方式预渲染时。 在服务器连接建立之后。 当最终呈现组件时,这可能会导致UI中显示的数据发生明显变化。为了避免Blazor服务器应用程序中出现这种双重呈现行为,请传入标识符以在预呈现期间缓存状态,并在预呈现之后检索状态

以下代码演示了基于模板的Blazor服务器应用程序中更新的WeatherForecastService,该应用程序避免了双重渲染。在下面的示例中,等待的延迟(await Task.Delay(…)模拟从GetForecastSync方法返回数据之前的短暂延迟

WeatherForecastService.cs

使用系统;
使用System.Linq;
使用System.Threading.Tasks;
使用Microsoft.Extensions.Caching.Memory;
使用BlazorSample.Shared;
公营天气预报服务
{
私有静态只读字符串[]摘要=新[]
{
“冷冻”、“支撑”、“寒冷”、“凉爽”、“温和”,
“温暖”、“温暖”、“炎热”、“闷热”、“灼热”
};
公共天气预报服务(IMemoryCache memoryCache)
{
MemoryCache=MemoryCache;
}
公共IMemoryCache MemoryCache{get;}
公共任务GetForecastSync(日期时间开始日期)
{
返回MemoryCache.GetOrCreateAsync(startDate,async e=>
{
e、 设置选项(新MemoryCacheEntryOptions
{
绝对过期相对现在=
时间跨度。从秒(30)
});
var rng=新随机数();
等待任务延迟(时间跨度从秒(10));
返回可枚举的范围(1,5)。选择(索引=>NewWeatherForecast
{
日期=开始日期。添加天数(索引),
温度c=下一个温度(-20,55),
摘要=摘要[rng.Next(摘要长度)]
}).ToArray();
});
}
}

@身体
上面的代码位于MainLayout.razor文件的Html段中,下面的代码位于代码段中

@inherits LayoutComponentBase
@inject BrowserService Service
@inject CarouselService cs

public int ScreenWidth { get; set; }
private List<string> CDNImages;
 protected override void OnInitialized()
{
    base.OnInitialized();
    try
    {
        isVisible = "visible";
        ScreenWidth = Service.GetDimension().Width;
        Console.WriteLine("initializing");
    }
    catch (Exception e)
    {
        Console.WriteLine("Error occurred " + e.ToString());
    }
}
protected override void OnParametersSet()
{
    base.OnParametersSet();
    CDNImages = cs.GetImages();
    Console.WriteLine("images are set in onParameterSet");
    //   isVisible = "visible";
}

protected override void OnAfterRender(bool firstRender)
{
    base.OnAfterRender(firstRender);
    if (firstRender)
    {
        //  isVisible = "visible";
    }
}
@继承LayoutComponentBase
@注入浏览器服务
@注入转盘服务cs
公共int屏幕宽度{get;set;}
私人名单;
受保护的覆盖无效OnInitialized()
{
base.OnInitialized();
尝试
{
isVisible=“visible”;
ScreenWidth=Service.GetDimension().Width;
控制台写入线(“初始化”);
}
捕获(例外e)
{
WriteLine(“发生错误”+e.ToString());
}
}
受保护的覆盖无效OnParametersSet()
{
base.OnParametersSet();
CDNImages=cs.GetImages();
WriteLine(“图像在onParameterSet中设置”);
//isVisible=“visible”;
}
受保护的覆盖无效OnAfterRender(布尔firstRender)
{
base.OnAfterRender(firstRender);
if(firstRender)
{
//isVisible=“visible”;
}
}
CarouselService.cs中的方法如下,它向CarouselBlock组件提供字符串列表

 public List<string> GetImages() 
    {
        for (int i = 1; i <= 3; i++)
        {
            paths.Add(FolderPath + Province + "/test" + i + ".png");
        }
        return paths;
    }
public List GetImages()
{

对于(int i=1;我能不能请你帮我将其用于非任务返回服务?@Keerthi,请发布你正在使用的服务的示例方法。嗨@Jason,我在下面发布了我的代码作为答案(让代码看起来正确缩进)。请看一下:)
 public List<string> GetImages() 
    {
        for (int i = 1; i <= 3; i++)
        {
            paths.Add(FolderPath + Province + "/test" + i + ".png");
        }
        return paths;
    }