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

Loading…
Cancel
Save