Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net core Blazor-WASM视图模型_Asp.net Core_Viewmodel_Blazor Webassembly - Fatal编程技术网

Asp.net core Blazor-WASM视图模型

Asp.net core Blazor-WASM视图模型,asp.net-core,viewmodel,blazor-webassembly,Asp.net Core,Viewmodel,Blazor Webassembly,在过去的一年里,我做了很多Razor页面,几周前,我开始为我的Blazor服务器应用程序将所有页面转换为ViewModel 现在我想是时候制作一个新的Blazor WebAssembly应用程序了 但是,基于WeatherForecast示例,我很难用ViewModel构建POC 但无论我做什么,我都有错误。到目前为止,我还没有找到一个好的基本例子 未处理的异常呈现组件:尝试激活“fm2.Client.ViewModels.fetchDataModel”时,无法解析类型“fm2.Client.M

在过去的一年里,我做了很多Razor页面,几周前,我开始为我的Blazor服务器应用程序将所有页面转换为ViewModel

现在我想是时候制作一个新的Blazor WebAssembly应用程序了

但是,基于WeatherForecast示例,我很难用ViewModel构建POC

但无论我做什么,我都有错误。到目前为止,我还没有找到一个好的基本例子

未处理的异常呈现组件:尝试激活“fm2.Client.ViewModels.fetchDataModel”时,无法解析类型“fm2.Client.Models.IFetchDataModel”的服务。 System.InvalidOperationException:尝试激活“fm2.Client.Models.IFetchDataModel”时,无法解析类型“fm2.Client.Models.IFetchDataModel”的服务

例如:

namespace fm2.Client.ViewModels
{
公共接口IFetchDataViewModel
{
WeatherForecast[]天气预报{get;set;}
任务检索ForecastAsync();
任务OnInitializedAsync();
}
公共类FetchDataViewModel:IFetchDataViewModel
{
私人天气预报【】天气预报;
私有IFetchDataModel\u fetchDataModel;
公共天气预报
{
获取天气预报;
set=>_weatherForecasts=值;
}
公共FetchDataViewModel(IFetchDataModel fetchDataModel)
{
WriteLine(“正在执行的FetchDataViewModel构造函数”);
_fetchDataModel=fetchDataModel;
}
公共异步任务RetrieveForecastsAsync()
{
_weatherForecasts=await_fetchDataModel.RetrieveForecastAsAsync();
WriteLine(“检索到FetchDataViewModel预测”);
}
公共异步任务OnInitializedAsync()
{
_weatherForecasts=await_fetchDataModel.RetrieveForecastAsAsync();
}
}
}
名称空间fm2.Client
{
公共课程
{
公共静态异步任务主(字符串[]args)
{
var builder=WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add

在这里,我对Blazor WebAssembly应用程序进行了相同的尝试: (构造函数未初始化)


此POC与顶部的第一个链接不同。在这里,我尝试了与Blazor服务器应用程序相同的操作。

我从Github提取了最新代码。看起来调用了错误的api

当我从这个改变:

WeatherForecast[] _weatherForecast = await _http.GetFromJsonAsync<WeatherForecast[]>("api/SampleData/WeatherForecasts");
WeatherForecast[]\u WeatherForecast=wait\u http.GetFromJsonAsync(“api/SampleData/WeatherForecast”);
为此:

WeatherForecast[] _weatherForecast = await _http.GetFromJsonAsync<WeatherForecast[]>("WeatherForecast");
WeatherForecast[]\u WeatherForecast=wait\u http.GetFromJsonAsync(“WeatherForecast”);
在WeatherViewModel.cs中


我可以获得要显示的天气数据。

请添加。看起来您缺少在IoC容器中注册
IFetchDataModel
。正如所怀疑的,您尚未注册
IFetchDataModel
的实现,您需要添加(
builder.Services.addScope()
)几个小时的反复尝试之后,它仍然不起作用。不知道还可以尝试什么。“无法解决”是一个DI错误,发布启动类。
WeatherForecast[] _weatherForecast = await _http.GetFromJsonAsync<WeatherForecast[]>("WeatherForecast");