feat: init avalonia demo.
This commit is contained in:
1 parent
adb6355884
commit
63bf5c6aa7
95 files changed
+20896
-2634
No files matched your search
@@ -0,0 +1,15 @@
|
||||
using System.Collections.Generic;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
|
||||
namespace HandyControlDemo.ViewModel;
|
||||
|
||||
public class DemoViewModelBase<T> : ObservableObject
|
||||
{
|
||||
private IList<T> _dataList = [];
|
||||
|
||||
public IList<T> DataList
|
||||
{
|
||||
get => _dataList;
|
||||
set => SetProperty(ref _dataList, value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
using System;
|
||||
using System.Collections.ObjectModel;
|
||||
using Avalonia.Threading;
|
||||
using CommunityToolkit.Mvvm.Messaging;
|
||||
using HandyControlDemo.Data;
|
||||
using HandyControlDemo.Properties.Langs;
|
||||
using HandyControlDemo.Service;
|
||||
using HandyControlDemo.Tools;
|
||||
|
||||
namespace HandyControlDemo.ViewModel;
|
||||
|
||||
public class MainViewModel : DemoViewModelBase<DemoDataModel>
|
||||
{
|
||||
private readonly DataService _dataService;
|
||||
|
||||
private object? _contentTitle;
|
||||
private object? _subContent;
|
||||
|
||||
public MainViewModel(DataService dataService)
|
||||
{
|
||||
_dataService = dataService;
|
||||
|
||||
UpdateMainContent();
|
||||
UpdateLeftContent();
|
||||
}
|
||||
|
||||
public DemoItemModel? DemoItemCurrent { get; private set; }
|
||||
|
||||
public DemoInfoModel? DemoInfoCurrent { get; set; }
|
||||
|
||||
public object? SubContent
|
||||
{
|
||||
get => _subContent;
|
||||
set => SetProperty(ref _subContent, value);
|
||||
}
|
||||
|
||||
public object? ContentTitle
|
||||
{
|
||||
get => _contentTitle;
|
||||
set => SetProperty(ref _contentTitle, value);
|
||||
}
|
||||
|
||||
public ObservableCollection<DemoInfoModel> DemoInfoCollection { get; set; } = [];
|
||||
|
||||
private void UpdateMainContent()
|
||||
{
|
||||
WeakReferenceMessenger.Default.Register<DemoItemModel, string>(this, MessageToken.SwitchDemo, (_, message) =>
|
||||
{
|
||||
SwitchDemo(message);
|
||||
});
|
||||
}
|
||||
|
||||
private void UpdateLeftContent()
|
||||
{
|
||||
//clear status
|
||||
WeakReferenceMessenger.Default.Register<object, string>(this, MessageToken.ClearLeftSelected, (_, _) =>
|
||||
{
|
||||
DemoItemCurrent = null;
|
||||
foreach (var item in DemoInfoCollection)
|
||||
{
|
||||
item.SelectedIndex = -1;
|
||||
}
|
||||
});
|
||||
|
||||
WeakReferenceMessenger.Default.Register<object, string>(this, MessageToken.LangUpdated, (_, _) =>
|
||||
{
|
||||
if (DemoItemCurrent == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
ContentTitle = Lang.ResourceManager.GetString(DemoItemCurrent.Name);
|
||||
});
|
||||
|
||||
//load items
|
||||
DemoInfoCollection = [];
|
||||
foreach (var item in _dataService.GetDemoInfo())
|
||||
{
|
||||
Dispatcher.UIThread.InvokeAsync(() =>
|
||||
{
|
||||
DemoInfoCollection.Add(item);
|
||||
}, DispatcherPriority.ApplicationIdle);
|
||||
}
|
||||
}
|
||||
|
||||
private void SwitchDemo(DemoItemModel item)
|
||||
{
|
||||
if (SubContent is IDisposable disposable)
|
||||
{
|
||||
disposable.Dispose();
|
||||
}
|
||||
|
||||
DemoItemCurrent = item;
|
||||
ContentTitle = Lang.ResourceManager.GetString(item.Name);
|
||||
object? demoControl = AssemblyHelper.ResolveByKey(item.TargetCtlName) ?? AssemblyHelper.CreateInternalInstance($"UserControl.{item.TargetCtlName}");
|
||||
SubContent = demoControl;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
using System;
|
||||
using HandyControlDemo.Service;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace HandyControlDemo.ViewModel;
|
||||
|
||||
public class ViewModelLocator
|
||||
{
|
||||
private static readonly Lazy<ViewModelLocator> InstanceInternal = new(() => new ViewModelLocator(), isThreadSafe: true);
|
||||
|
||||
public static ViewModelLocator Instance => InstanceInternal.Value;
|
||||
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
|
||||
private ViewModelLocator()
|
||||
{
|
||||
var services = new ServiceCollection();
|
||||
|
||||
services.AddSingleton<DataService>();
|
||||
services.AddTransient<MainViewModel>();
|
||||
|
||||
_serviceProvider = services.BuildServiceProvider();
|
||||
}
|
||||
|
||||
public MainViewModel Main => _serviceProvider.GetService<MainViewModel>()!;
|
||||
}
|
||||
Reference in new issue
Block a user