无法转换 viewModel 和 ObservableCollection
cannot convert viewModel and ObservableCollection
在我的后续第二行中,我发现了一个转换错误参数:
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 是静态的 class 使用 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 } );
在我的后续第二行中,我发现了一个转换错误参数:
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 是静态的 class 使用 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 } );