refactor: use view locator instead of creating views manually in viewmodels (#1213)

Signed-off-by: leo <longshuang@msn.cn>
This commit is contained in:
leo 2025-04-19 11:14:19 +08:00
parent 413669741d
commit 5fd074a9b6
No known key found for this signature in database
60 changed files with 52 additions and 140 deletions

View file

@ -67,7 +67,7 @@
<StackPanel Orientation="Vertical" Background="{DynamicResource Brush.Popup}">
<!-- Popup Widget -->
<ContentPresenter Margin="8,16,8,8"
Content="{Binding View}"
DataContextChanged="OnPopupDataContextChanged"
IsHitTestVisible="{Binding InProgress, Converter={x:Static BoolConverters.Not}}"/>
<!-- Options -->

View file

@ -1,4 +1,6 @@
using System;
using Avalonia.Controls;
using Avalonia.Controls.Presenters;
using Avalonia.Input;
using Avalonia.Interactivity;
@ -48,5 +50,35 @@ namespace SourceGit.Views
e.Handled = true;
}
private void OnPopupDataContextChanged(object sender, EventArgs e)
{
if (sender is ContentPresenter presenter)
{
if (presenter.DataContext == null || presenter.DataContext is not ViewModels.Popup)
{
presenter.Content = null;
return;
}
var dataTypeName = presenter.DataContext.GetType().FullName;
if (string.IsNullOrEmpty(dataTypeName))
{
presenter.Content = null;
return;
}
var viewTypeName = dataTypeName.Replace("ViewModels", "Views");
var viewType = Type.GetType(viewTypeName);
if (viewType == null)
{
presenter.Content = null;
return;
}
var view = Activator.CreateInstance(viewType);
presenter.Content = view;
}
}
}
}