12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- using Avalonia.Controls;
- using Avalonia.Data.Converters;
- using System;
- using System.Collections.Concurrent;
- using System.Collections.Generic;
- using System.Globalization;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ShakerApp.Convert
- {
- public class Type2ViewConverter : IValueConverter
- {
- ConcurrentDictionary<RuntimeTypeHandle, Control> Views = new ConcurrentDictionary<RuntimeTypeHandle,Control>();
- public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
- {
- if (value is Type type)
- {
- if (parameter != null) return (Control)Activator.CreateInstance(type)!;
- else
- {
- if (Views.TryGetValue(type.TypeHandle, out var view)) return view;
- else
- {
- view = (Control)Activator.CreateInstance(type)!;
- Views[type.TypeHandle] = view;
- return view;
- }
- }
- }
- if(value is IList<Type> types)
- {
- if (parameter != null) return types.Where(x => x != null).Select(x => Activator.CreateInstance(x));
- else
- {
- return types.Where(x => x != null)
- .Select(x =>
- {
- if (Views.TryGetValue(x.TypeHandle, out var view)) return view;
- else
- {
- view = (Control)Activator.CreateInstance(x)!;
- Views[x.TypeHandle] = view;
- return view;
- }
- });
- }
- }
- return null;
- }
- public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
- {
- throw new NotImplementedException();
- }
- }
- }
|