Mvvm与Locator绑定到JumpList LongList
本文关键字:JumpList LongList 绑定 Locator Mvvm | 更新日期: 2023-09-27 17:58:44
好的,让我们看看。。。我有这个ViewModel类,它有一个从存储中检索数据的生成器,另一个向Internet请求数据的方法,它有个保存程序notifypropertychange方法和locator方法。但问题是我如何将其绑定到跳转列表框,因为当我使用定位器绑定时,我无法将其作为一个组来处理,以获取标题并排序。
public class FriendLocator
{
private Lazy<ViewModelFriends> mainVMFriends;
public FriendLocator()
{
mainVMFriends = new Lazy<ViewModelFriends>(() => new ViewModelFriends());
}
public ViewModelFriends MainVMFriends
{
get
{
return mainVMFriends.Value;
}
}
}
public class VMFriendsBase : INotifyPropertyChanged
{
public VMFriendsBase()
{ }
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
public class ViewModelFriends : VMFriendsBase
{
IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
private ObservableCollection<User_friend> Friends;
public ObservableCollection<User_friend> friends
{
get
{
return Friends;
}
set
{
Friends = value;
RaisePropertyChanged("friends");
}
}
public ViewModelFriends()
{
ObservableCollection<User_friend> a = new ObservableCollection<User_friend>();
if (settings.Contains("userFriends"))
{
string listado = settings["userFriends"] as string;
var listajson = JsonConvert.DeserializeObject<objetoslistas.getmyfriends>(listado);
if (listajson.profile != null)
{
foreach (objetoslistas.User user in listajson.profile)
{
User_friend usuario = new User_friend(user);
a.Add(usuario);
}
}
}
friends = a;
AskFriends();
}
public async void AskFriends()
{
ObservableCollection<User_friend> a = new ObservableCollection<User_friend>();
Uri url = new Uri(link);
objetoslistas.GetByUserID paquete = new objetoslistas.GetByUserID();
string respuesta = await metodosJson.jsonPOST(url, paquete);
var respuestajson = JsonConvert.DeserializeObject<objetoslistas.getmyfriends>(respuesta.ToString());
if (respuestajson.error == "")
{
saveFriends(respuesta);
if (respuestajson.profile != null)
{
foreach (objetoslistas.User user in respuestajson.profile)
{
User_friend usuario = new User_friend(user);
a.Add(usuario);
}
}
}
friends = a;
}
public void saveFriends(string jsonfriends)
{
if (!settings.Contains("userFriends"))
settings.Add("userFriends", jsonfriends);
else
settings["userFriends"] = jsonfriends;
settings.Save();
}
}
我用来处理列表框数据的方法是
public static List<Group<T>> GetItemGroups<T>(IEnumerable<T> itemList, Func<T, string> getKeyFunc)
{
IEnumerable<Group<T>> groupList = from item in itemList
group item by getKeyFunc(item) into g
orderby g.Key
select new Group<T>(g.Key, g);
return groupList.ToList();
}
public class Group<T> : List<T>
{
public Group(string name, IEnumerable<T> items)
: base(items)
{
this.Title = name;
}
public string Title
{
get;
set;
}
}
感谢所有
好吧,我终于自己解决了,我做了一个这样的对话器:
public class ObservableToGroupedConverter : IValueConverter
{
public object Convert(object value, Type targetType,
object parameter, CultureInfo culture)
{
var group = (value as ViewModelFriends).friends;
return metodosAuxiliares.GetItemGroups(group.OrderBy(o => o.distraw).ToList(), c => c.online);
}
public object ConvertBack(object value, Type targetType,
object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
我将conversor添加到资源中,绑定为:
<view:ViewFriends x:Name="vistaAmigos"
DataContext="{Binding MainVMFriends,
Source={StaticResource friendLocator},
Converter={StaticResource friendConvert}}"/>