mirror of
https://github.com/sourcegit-scm/sourcegit
synced 2025-05-24 21:54:59 +00:00
refactor: use view locator instead of creating views manually in viewmodels (#1213)
Signed-off-by: leo <longshuang@msn.cn>
This commit is contained in:
parent
413669741d
commit
5fd074a9b6
60 changed files with 52 additions and 140 deletions
|
@ -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 -->
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue