Browse Source

Exclude persistent views in designer mode

pull/165/head
Ionite 1 year ago
parent
commit
dfeb13e0f4
No known key found for this signature in database
  1. 39
      StabilityMatrix.Avalonia/ViewLocator.cs

39
StabilityMatrix.Avalonia/ViewLocator.cs

@ -21,7 +21,8 @@ public class ViewLocator : IDataTemplate, INavigationPageFactory
/// <inheritdoc /> /// <inheritdoc />
public Control Build(object? data) public Control Build(object? data)
{ {
if (data is null) throw new ArgumentNullException(nameof(data)); if (data is null)
throw new ArgumentNullException(nameof(data));
var type = data.GetType(); var type = data.GetType();
@ -31,10 +32,7 @@ public class ViewLocator : IDataTemplate, INavigationPageFactory
return GetView(viewType, data, viewAttr.IsPersistent); return GetView(viewType, data, viewAttr.IsPersistent);
} }
return new TextBlock return new TextBlock { Text = "View Model Not Found: " + data.GetType().FullName };
{
Text = "View Model Not Found: " + data.GetType().FullName
};
} }
private Control GetView(Type viewType) private Control GetView(Type viewType)
@ -44,21 +42,25 @@ public class ViewLocator : IDataTemplate, INavigationPageFactory
return view; return view;
} }
return new TextBlock return new TextBlock { Text = "View Not Found: " + viewType.FullName };
{
Text = "View Not Found: " + viewType.FullName
};
} }
private Control GetView(Type viewType, object context, bool persistent) private Control GetView(Type viewType, object context, bool persistent)
{ {
// Disregard persistent settings in design mode
if (Design.IsDesignMode)
{
persistent = false;
}
if (persistent) if (persistent)
{ {
// Check assignable from IPersistentViewProvider // Check assignable from IPersistentViewProvider
if (context is not IPersistentViewProvider persistentViewProvider) if (context is not IPersistentViewProvider persistentViewProvider)
{ {
throw new InvalidOperationException( throw new InvalidOperationException(
$"View {viewType.Name} is marked as persistent but does not implement IPersistentViewProvider"); $"View {viewType.Name} is marked as persistent but does not implement IPersistentViewProvider"
);
} }
// Try get from context // Try get from context
@ -87,10 +89,7 @@ public class ViewLocator : IDataTemplate, INavigationPageFactory
} }
} }
return new TextBlock return new TextBlock { Text = "View Not Found: " + viewType.FullName };
{
Text = "View Not Found: " + viewType.FullName
};
} }
/// <inheritdoc /> /// <inheritdoc />
@ -102,8 +101,10 @@ public class ViewLocator : IDataTemplate, INavigationPageFactory
/// <inheritdoc /> /// <inheritdoc />
public Control? GetPage(Type srcType) public Control? GetPage(Type srcType)
{ {
if (Attribute.GetCustomAttribute(srcType, typeof(ViewAttribute)) is not ViewAttribute if (
viewAttr) Attribute.GetCustomAttribute(srcType, typeof(ViewAttribute))
is not ViewAttribute viewAttr
)
{ {
throw new InvalidOperationException("View not found for " + srcType.FullName); throw new InvalidOperationException("View not found for " + srcType.FullName);
} }
@ -118,8 +119,10 @@ public class ViewLocator : IDataTemplate, INavigationPageFactory
/// <inheritdoc /> /// <inheritdoc />
public Control GetPageFromObject(object target) public Control GetPageFromObject(object target)
{ {
if (Attribute.GetCustomAttribute(target.GetType(), typeof(ViewAttribute)) is not if (
ViewAttribute viewAttr) Attribute.GetCustomAttribute(target.GetType(), typeof(ViewAttribute))
is not ViewAttribute viewAttr
)
{ {
throw new InvalidOperationException("View not found for " + target.GetType().FullName); throw new InvalidOperationException("View not found for " + target.GetType().FullName);
} }

Loading…
Cancel
Save