gpt4 book ai didi

c# - 无法转换 viewModel 和 ObservableCollection

转载 作者:行者123 更新时间:2023-11-30 23:28:11 25 4
gpt4 key购买 nike

在我接下来的第二行中,我发现了一个转换错误参数:

UserStackVM _listeStack = JsonWorker.ReadData();
ListeStacks = new ObservableCollection<UserStackVM>(_listeStack); // here

我的错误是:

cannot convert from 'MyStack.ViewModels.UserStackVM' to 'System.Collections.Generic.List'

UserStackVM 是一个 ViewModel:

#region Properties
private string name;
...
private string[] path;
...
#endregion

JsonWorker 是一个使用 Json.NET 的静态类 (http://www.newtonsoft.com/json):

#region Properties
private static string _json;
private static UserStackVM _userStack;
#endregion

#region Methods

/// <summary>
/// Open the json config file. Create it if he doesn't exist.
/// </summary>
private static void OpenFile()
{
using (var stream = new FileStream(@"config.json", FileMode.OpenOrCreate))
{
using (var reader = new StreamReader(stream))
{
_json = reader.ReadToEnd();
}
}
}

/// <summary>
/// Read the json config file and return all data.
/// </summary>
/// <returns></returns>
public static UserStackVM ReadData()
{
OpenFile();
_userStack = JsonConvert.DeserializeObject<UserStackVM>(_json);
return _userStack;
}
#endregion

提前,感谢您的帮助。

最佳答案

'MyStack.ViewModels.UserStackVM' to 'System.Collections.Generic.List'

那个ObservableCollection(T) Constructor期望一个 List<T> (实例列表);您只提供一个实例。改成

UserStackVM _listeStack = JsonWorker.ReadData();
ListeStacks = new ObservableCollection<UserStackVM>();

ListeStacks.Add( _listeStack );

ListeStacks = new ObservableCollection<UserStackVM>
( new List<UserStackVM> () { listeStack } );

关于c# - 无法转换 viewModel 和 ObservableCollection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36136324/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com