Skip to content

Support Avalonia previewer #69

@gentledepp

Description

@gentledepp

The avalonie previewer in Visual Studio and Rider fails to load when the view derives from AvaloniaInside.Shell.Page.

The reason is, that in OnLoaded, the Chain is still null

	protected override void OnLoaded(RoutedEventArgs e)
	{
		base.OnLoaded(e);
		ApplyNavigationBar();
		AttachedNavigationBar?.UpdateView(this);

		this[!ApplyTopSafePaddingProperty] = this[!ShellView.EnableSafeAreaForTopProperty];
		this[!ApplyBottomSafePaddingProperty] = this[!ShellView.EnableSafeAreaForBottomProperty];
		this[!ApplyLeftSafePaddingProperty] = this[!ShellView.EnableSafeAreaForLeftProperty];
		this[!ApplyRightSafePaddingProperty] = this[!ShellView.ApplyRightSafePaddingProperty];

		IsModal = Chain.Type == NavigateType.Modal;
	}

to fix this, I propose adding the following check:

protected override void OnLoaded(RoutedEventArgs e)
	{
		base.OnLoaded(e);
		ApplyNavigationBar();
		AttachedNavigationBar?.UpdateView(this);

		this[!ApplyTopSafePaddingProperty] = this[!ShellView.EnableSafeAreaForTopProperty];
		this[!ApplyBottomSafePaddingProperty] = this[!ShellView.EnableSafeAreaForBottomProperty];
		this[!ApplyLeftSafePaddingProperty] = this[!ShellView.EnableSafeAreaForLeftProperty];
		this[!ApplyRightSafePaddingProperty] = this[!ShellView.ApplyRightSafePaddingProperty];

+		// allow previewer to display page-based views
+		if (Design.IsDesignMode && Chain is null)
+			return;
			
		IsModal = Chain.Type == NavigateType.Modal;
	}

in case the Chain should never be null at that point, I'd even propose

protected override void OnLoaded(RoutedEventArgs e)
	{
		base.OnLoaded(e);
		ApplyNavigationBar();
		AttachedNavigationBar?.UpdateView(this);

		this[!ApplyTopSafePaddingProperty] = this[!ShellView.EnableSafeAreaForTopProperty];
		this[!ApplyBottomSafePaddingProperty] = this[!ShellView.EnableSafeAreaForBottomProperty];
		this[!ApplyLeftSafePaddingProperty] = this[!ShellView.EnableSafeAreaForLeftProperty];
		this[!ApplyRightSafePaddingProperty] = this[!ShellView.ApplyRightSafePaddingProperty];

		// allow previewer to display page-based views
		if (Design.IsDesignMode && Chain is null)
			return;
			
+		if (Chain is null)
+			throw new InvalidOperationException("Chain must not be null");

		IsModal = Chain.Type == NavigateType.Modal;
	}

That way, if there is a configuration error, the developer gets a much more descriptive error than just a NRE.
What do you think?

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions