Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions samples/Sample.Avalonia/Regions/ListBoxRegion.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using AsyncNavigation;
using AsyncNavigation.Avalonia;
using AsyncNavigation.Avalonia;
using Avalonia.Controls;
using System;

namespace Sample.Avalonia.Regions;

public class ListBoxRegion : ItemsRegion
public class ListBoxRegion : SelectingItemsRegion
{
private readonly ListBox _listBox;
public ListBoxRegion(string name,
Expand All @@ -16,8 +15,4 @@ public ListBoxRegion(string name,
_listBox = listBox;
_listBox.AutoScrollToSelectedItem = true;
}
public override void ProcessActivate(NavigationContext navigationContext)
{
base.ProcessActivate(navigationContext);
}
}
26 changes: 13 additions & 13 deletions samples/Sample.Avalonia/Views/BView.axaml
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
<UserControl
d:DesignHeight="450"
d:DesignWidth="800"
mc:Ignorable="d"
x:Class="Sample.Avalonia.Views.BView"
x:DataType="vm:BViewModel"
xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:an="https://github.com/NeverMorewd/AsyncNavigation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="using:Sample.Common"
d:DesignHeight="450"
d:DesignWidth="800"
mc:Ignorable="d"
x:DataType="vm:BViewModel">
xmlns:vm="using:Sample.Common"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<DockPanel LastChildFill="True">
<Border
HorizontalAlignment="Stretch"
Background="IndianRed"
BorderBrush="LemonChiffon"
BorderThickness="1"
DockPanel.Dock="Top">
DockPanel.Dock="Top"
HorizontalAlignment="Stretch">
<StackPanel>
<TextBlock Text="{Binding Name}" />
<TextBlock>
Expand Down Expand Up @@ -54,11 +54,11 @@
</StackPanel>
<ScrollViewer>
<ItemsControl an:RegionManager.RegionName="ItemsRegion">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</ScrollViewer>
</DockPanel>
Expand Down
33 changes: 14 additions & 19 deletions src/AsyncNavigation.Avalonia/ContentRegion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,6 @@ public ContentRegion(string name,
IServiceProvider serviceProvider,
bool? useCache) : base(name, contentControl, serviceProvider)
{
ArgumentNullException.ThrowIfNull(contentControl);
ArgumentNullException.ThrowIfNull(serviceProvider);

RegionControlAccessor.ExecuteOn(control =>
{
control.Tag = this;
control.ContentTemplate = new FuncDataTemplate<NavigationContext>((context, np) =>
{
return context?.IndicatorHost.Value?.Host as Control;
});

control.Bind(
ContentControl.ContentProperty,
new Binding(nameof(RegionContext.Selected)) { Source = _context, Mode = BindingMode.TwoWay });

});



EnableViewCache = useCache ?? true;
IsSinglePageRegion = true;
}
Expand All @@ -40,6 +21,20 @@ public override NavigationPipelineMode NavigationPipelineMode
get => NavigationPipelineMode.RenderFirst;
}

protected override void InitializeOnRegionCreated(ContentControl control)
{
base.InitializeOnRegionCreated(control);
control.Tag = this;
control.ContentTemplate = new FuncDataTemplate<NavigationContext>((context, np) =>
{
return context?.IndicatorHost.Value?.Host as Control;
});

control.Bind(
ContentControl.ContentProperty,
new Binding(nameof(RegionContext.Selected)) { Source = _context, Mode = BindingMode.TwoWay });
}

public override void Dispose()
{
base.Dispose();
Expand Down
61 changes: 5 additions & 56 deletions src/AsyncNavigation.Avalonia/ItemsRegion.cs
Original file line number Diff line number Diff line change
@@ -1,68 +1,17 @@
using AsyncNavigation.Core;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Controls.Templates;
using Avalonia.Data;
using Avalonia.Controls;

namespace AsyncNavigation.Avalonia;

public class ItemsRegion : RegionBase<ItemsRegion, ItemsControl>
public class ItemsRegion : ItemsRegionBase<ItemsRegion, ItemsControl>
{
public ItemsRegion(string name,
ItemsControl itemsControl,
IServiceProvider serviceProvider,
bool? useCache) : base(name, itemsControl, serviceProvider)
bool? useCache) : base(name, itemsControl, serviceProvider, useCache)
{
ArgumentNullException.ThrowIfNull(itemsControl);
ArgumentNullException.ThrowIfNull(serviceProvider);

RegionControlAccessor.ExecuteOn(control =>
{
// binding the lifetime of region to the control
control.Tag = this;
control.ItemTemplate = new FuncDataTemplate<NavigationContext>((context, np) =>
{
return context?.IndicatorHost.Value?.Host as Control;
});

control.Bind(
ItemsControl.ItemsSourceProperty,
new Binding(nameof(RegionContext.Items)) { Source = _context });

control.Bind(
SelectingItemsControl.SelectedItemProperty,
new Binding(nameof(RegionContext.Selected)) { Source = _context, Mode = BindingMode.TwoWay });
});
EnableViewCache = useCache ?? false;
IsSinglePageRegion = false;
}
public override NavigationPipelineMode NavigationPipelineMode
{
get => NavigationPipelineMode.RenderFirst;
}
public override void Dispose()
{
base.Dispose();
_context.Clear();

}
public override void ProcessActivate(NavigationContext navigationContext)
{
if (!_context.Items.Contains(navigationContext))
_context.Items.Add(navigationContext);
}

_context.Selected = navigationContext;

RegionControlAccessor.ExecuteOn(control =>
{
control.ScrollIntoView(navigationContext);
});
}

public override void ProcessDeactivate(NavigationContext? navigationContext)
{
var target = navigationContext ?? _context.Selected;
if (target == null)
return;
_ = _context.Items.Remove(target);
}
}
4 changes: 0 additions & 4 deletions src/AsyncNavigation.Avalonia/ItemsRegionAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ namespace AsyncNavigation.Avalonia;

public class ItemsRegionAdapter : RegionAdapterBase<ItemsControl>
{
public override bool IsAdapted(ItemsControl control)
{
return base.IsAdapted(control);
}
public override IRegion CreateRegion(string name, ItemsControl control, IServiceProvider serviceProvider, bool? useCache)
{
return new ItemsRegion(name, control, serviceProvider, useCache);
Expand Down
72 changes: 72 additions & 0 deletions src/AsyncNavigation.Avalonia/ItemsRegionBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using AsyncNavigation.Core;
using Avalonia.Controls;
using Avalonia.Controls.Templates;
using Avalonia.Data;
using Avalonia.Threading;

namespace AsyncNavigation.Avalonia;

public abstract class ItemsRegionBase<TRegion, TItemsControl>
: RegionBase<TRegion, TItemsControl>
where TRegion : ItemsRegionBase<TRegion, TItemsControl>
where TItemsControl : ItemsControl
{
protected ItemsRegionBase(
string name,
TItemsControl control,
IServiceProvider serviceProvider,
bool? useCache)
: base(name, control, serviceProvider)
{
IsSinglePageRegion = false;
EnableViewCache = useCache ?? false;
}

public override NavigationPipelineMode NavigationPipelineMode
=> NavigationPipelineMode.RenderFirst;

protected override void InitializeOnRegionCreated(TItemsControl control)
{
base.InitializeOnRegionCreated(control);

control.Tag = this;
control.ItemTemplate = new FuncDataTemplate<NavigationContext>((context, _) =>
{
return context?.IndicatorHost.Value?.Host as Control;
});

control.Bind(
ItemsControl.ItemsSourceProperty,
new Binding(nameof(RegionContext.Items)) { Source = _context });
}


public override void ProcessActivate(NavigationContext navigationContext)
{
if (!_context.Items.Contains(navigationContext))
_context.Items.Add(navigationContext);

_context.Selected = navigationContext;
// https://github.com/AvaloniaUI/Avalonia/issues/17347
Dispatcher.UIThread.RunJobs(DispatcherPriority.Loaded);
RegionControlAccessor.ExecuteOn(control =>
{
control.ScrollIntoView(navigationContext);
});
}

public override void ProcessDeactivate(NavigationContext? navigationContext)
{
var target = navigationContext ?? _context.Selected;
if (target == null)
return;

_ = _context.Items.Remove(target);
}

public override void Dispose()
{
base.Dispose();
_context.Clear();
}
}
27 changes: 27 additions & 0 deletions src/AsyncNavigation.Avalonia/SelectingItemsRegion.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using AsyncNavigation.Core;
using Avalonia.Controls.Primitives;
using Avalonia.Data;

namespace AsyncNavigation.Avalonia;

/// <summary>
/// Make this class abstract to avoid direct usage.
/// Because https://github.com/AvaloniaUI/Avalonia/issues/11139
/// </summary>
public abstract class SelectingItemsRegion : ItemsRegionBase<SelectingItemsRegion, SelectingItemsControl>
{
protected SelectingItemsRegion(string name,
SelectingItemsControl selectingItemsControl,
IServiceProvider serviceProvider,
bool? useCache) : base(name, selectingItemsControl, serviceProvider, useCache)
{

}
protected override void InitializeOnRegionCreated(SelectingItemsControl control)
{
base.InitializeOnRegionCreated(control);
control.Bind(
SelectingItemsControl.SelectedItemProperty,
new Binding(nameof(RegionContext.Selected)) { Source = _context, Mode = BindingMode.TwoWay });
}
}
38 changes: 18 additions & 20 deletions src/AsyncNavigation.Avalonia/TabRegion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,31 @@ public TabRegion(string name,
TabControl tabControl,
IServiceProvider serviceProvider,
bool? useCache) : base(name, tabControl, serviceProvider)
{
ArgumentNullException.ThrowIfNull(tabControl);
ArgumentNullException.ThrowIfNull(serviceProvider);

RegionControlAccessor.ExecuteOn(control =>
{
control.Tag = this;
control.Bind(ItemsControl.ItemsSourceProperty,
new Binding(nameof(RegionContext.Items)) { Source = _context });

control.Bind(SelectingItemsControl.SelectedItemProperty,
new Binding(nameof(RegionContext.Selected)) { Source = _context, Mode = BindingMode.TwoWay });

control.ContentTemplate = new FuncDataTemplate<NavigationContext>((context, _) =>
{
return context?.IndicatorHost.Value?.Host as Control;
});
});


{
EnableViewCache = useCache ?? false;
IsSinglePageRegion = false;
}
public override NavigationPipelineMode NavigationPipelineMode
{
get => NavigationPipelineMode.ResolveFirst;
}

protected override void InitializeOnRegionCreated(TabControl control)
{
base.InitializeOnRegionCreated(control);
control.Tag = this;
control.Bind(ItemsControl.ItemsSourceProperty,
new Binding(nameof(RegionContext.Items)) { Source = _context });

control.Bind(SelectingItemsControl.SelectedItemProperty,
new Binding(nameof(RegionContext.Selected)) { Source = _context, Mode = BindingMode.TwoWay });

control.ContentTemplate = new FuncDataTemplate<NavigationContext>((context, _) =>
{
return context?.IndicatorHost.Value?.Host as Control;
});
}

public override void Dispose()
{
base.Dispose();
Expand Down
Loading
Loading