Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Avalonia UI features #967

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ public void GoHome()
}
}

public void GoBack()
{
var menuVM = _container.GetInstance<MenuViewModel>();
if (_navigationService != null)
{
_navigationService.GoBackAsync();
}
}

public async void NavReady(NavigationFrame frame)
{
_navigationService = frame as INavigationService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,11 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="Features.CrossPlatform.Views.NavigationSourceView">
Welcome to Avalonia!
<Grid>
<StackPanel Margin="20">
<TextBox x:Name="Text" />
<CheckBox x:Name="IsNavigationEnabled" Content="Is Enabled" Margin="0,10" />
<Button x:Name="Navigate" Content="Navigate" />
</StackPanel>
</Grid>
</UserControl>
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,10 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="Features.CrossPlatform.Views.NavigationTargetView">
Welcome to Avalonia!
<Grid>
<StackPanel Margin="20">
<TextBlock x:Name="Text" />
<TextBlock x:Name="IsNavigationEnabled" />
</StackPanel>
</Grid>
</UserControl>
18 changes: 17 additions & 1 deletion samples/features/Features.Avalonia/Views/ShellView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,29 @@
Height="450"
x:Class="Features.CrossPlatform.Views.ShellView"
Title="ShellView">
<Window.Resources>
<StreamGeometry x:Key="arrow_left_regular">M12.7347,4.20949 C13.0332,3.92233 13.508,3.93153 13.7952,4.23005 C14.0823,4.52857 14.0731,5.00335 13.7746,5.29051 L5.50039,13.25 L24.2532,13.25 C24.6674,13.25 25.0032,13.5858 25.0032,13.9999982 C25.0032,14.4142 24.6674,14.75 24.2532,14.75 L5.50137,14.75 L13.7746,22.7085 C14.0731,22.9957 14.0823,23.4705 13.7952,23.769 C13.508,24.0675 13.0332,24.0767 12.7347,23.7896 L3.30673,14.7202 C2.89776,14.3268 2.89776,13.6723 3.30673,13.2788 L12.7347,4.20949 Z</StreamGeometry>
<StreamGeometry x:Key="home_regular">M21.6062 5.85517C23.0048 4.71494 24.9952 4.71494 26.3938 5.85517L39.5688 16.5966C40.4736 17.3342 41 18.4492 41 19.628V39.1134C41 41.2599 39.2875 43 37.175 43H32.075C29.9625 43 28.25 41.2599 28.25 39.1134V29.7492C28.25 29.0337 27.6792 28.4536 26.975 28.4536H21.025C20.3208 28.4536 19.75 29.0337 19.75 29.7492V39.1134C19.75 41.2599 18.0375 43 15.925 43H10.825C8.71251 43 7 41.2599 7 39.1134V19.628C7 18.4493 7.52645 17.3342 8.43124 16.5966L21.6062 5.85517ZM24.7979 7.87612C24.3317 7.49604 23.6683 7.49604 23.2021 7.87612L10.0271 18.6175C9.72548 18.8634 9.55 19.2351 9.55 19.628V39.1134C9.55 39.8289 10.1208 40.4089 10.825 40.4089H15.925C16.6292 40.4089 17.2 39.8289 17.2 39.1134V29.7492C17.2 27.6027 18.9125 25.8626 21.025 25.8626H26.975C29.0875 25.8626 30.8 27.6027 30.8 29.7492V39.1134C30.8 39.8289 31.3708 40.4089 32.075 40.4089H37.175C37.8792 40.4089 38.45 39.8289 38.45 39.1134V19.628C38.45 19.2351 38.2745 18.8634 37.9729 18.6175L24.7979 7.87612Z</StreamGeometry> </Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="50"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Orientation="Horizontal">
<Button cal:Message.Attach="GoHome">Home</Button>
<StackPanel.Background>
<LinearGradientBrush StartPoint="50%,0%" EndPoint="50%,100%">
<GradientStop Color="#A8E6CF" Offset="0.0"/>
<GradientStop Color="#3D84A8" Offset="1.0"/>
</LinearGradientBrush>
</StackPanel.Background>
<Button cal:Message.Attach="GoBack" >
<PathIcon Height="25" Width="25" Foreground="Blue"
Data="{StaticResource arrow_left_regular}"/>
</Button>
<Button cal:Message.Attach="GoHome" >
<PathIcon Height="25" Width="25" Foreground="Blue"
Data="{StaticResource home_regular}"/>
</Button>
</StackPanel>
<DockPanel Grid.Row="1">
<Border>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;

namespace Caliburn.Micro
{
Expand All @@ -15,18 +17,24 @@
private static readonly ILog Log = LogManager.GetLog(typeof(NavigationFrame));

private string defaultContent { get; } = "Default Content";

private readonly List<object> navigationStack = new List<object>();
private int navigationStackIndex = 0;
/// <summary>
/// Initializes a new instance of the <see cref="NavigationFrame"/> class.
/// </summary>
public NavigationFrame()
{
Content = defaultContent;
this.AttachedToVisualTree += NavigationFrame_AttachedToVisualTree;
LayoutUpdated += NavigationFrame_LayoutUpdated;
this.Loaded += NavigationFrame_Loaded;
ContentProperty.Changed.AddClassHandler<NavigationFrame>((sender, e) => NavigationFrame_ContentChanged(sender, e));
}

private void NavigationFrame_Loaded(object sender, RoutedEventArgs e)
{
Log.Info("Navigation Frame loaded");
OnNavigationServiceReady(new EventArgs());
}

private void NavigationFrame_ContentChanged(NavigationFrame sender, AvaloniaPropertyChangedEventArgs e)
{
Log.Info("Content changed");
Expand Down Expand Up @@ -66,15 +74,6 @@
}
}

/// <summary>
/// Handles the event when the frame is attached to the visual tree.
/// </summary>
private void NavigationFrame_AttachedToVisualTree(object sender, VisualTreeAttachmentEventArgs e)
{
Log.Info("Attached to visual tree");
OnNavigationServiceReady(new EventArgs());
}

/// <summary>
/// Occurs when the navigation service is ready.
/// </summary>
Expand All @@ -98,7 +97,7 @@
/// Navigates to the specified view model.
/// </summary>
/// <param name="viewModel">The view model to navigate to.</param>
private void NavigateToViewModel(object viewModel)
private void NavigateToViewModel(object viewModel, bool addToStack = true)

Check warning on line 100 in src/Caliburn.Micro.Platform/Platforms/netcore-avalonia/NavigationFrame.cs

View workflow job for this annotation

GitHub Actions / build

Parameter 'addToStack' has no matching param tag in the XML comment for 'NavigationFrame.NavigateToViewModel(object, bool)' (but other parameters do)

Check warning on line 100 in src/Caliburn.Micro.Platform/Platforms/netcore-avalonia/NavigationFrame.cs

View workflow job for this annotation

GitHub Actions / build

Parameter 'addToStack' has no matching param tag in the XML comment for 'NavigationFrame.NavigateToViewModel(object, bool)' (but other parameters do)
{
if (viewModel == null)
{
Expand All @@ -117,17 +116,25 @@

ViewModelBinder.Bind(viewModel, viewInstance, null);
Log.Info($"View Model {viewModel}");
Log.Info($"View {viewInstance}");
Tag = "Navigating";
viewInstance.DataContext = viewModel;
Content = viewInstance;

if (addToStack)
{
navigationStack.Add(viewModel);
navigationStackIndex = navigationStack.Count;
}
}

/// <inheritdoc/>
public Task GoBackAsync(bool animated = true)
{
throw new NotImplementedException();
Log.Info("Going back");
if (navigationStackIndex > 0)
navigationStackIndex--;

NavigateToViewModel(navigationStack[navigationStackIndex - 1], false);
return Task.CompletedTask;
}

/// <inheritdoc/>
Expand All @@ -145,18 +152,27 @@
/// <inheritdoc/>
public Task NavigateToViewModelAsync(Type viewModelType, object parameter = null, bool animated = true)
{
Log.Info($"View model type {viewModelType}");
var vm = Caliburn.Micro.IoC.GetInstance(viewModelType, null);
Log.Info($"VM is null {vm == null}");
TryInjectParameters(vm, parameter);
NavigateToViewModel(vm);

return Task.CompletedTask;
}

/// <summary>
/// Gets or sets the ViewModel.
/// </summary>
public static string ViewModel
{
get; set;
}

/// <inheritdoc/>
public Task NavigateToViewModelAsync<T>(object parameter = null, bool animated = true)
{
throw new NotImplementedException();
Log.Info($"Navigate to View model type {typeof(T)}");
Log.Info($"Navigate to View model parameter not null {parameter != null}");
return NavigateToViewModelAsync(typeof(T), parameter, animated);
}

/// <inheritdoc/>
Expand All @@ -170,6 +186,7 @@
/// <inheritdoc/>
public Task GoBackToRootAsync(bool animated = true)
{
Log.Info("Going back to root");
throw new NotImplementedException();
}

Expand All @@ -181,5 +198,41 @@
{
return $"{nameof(NavigationFrame)}: Content={Content}, IsNavigationServiceReady={NavigationServiceReady != null}";
}

/// <summary>
/// Attempts to inject query string parameters from the view into the view model.
/// </summary>
/// <param name="viewModel"> The view model.</param>
/// <param name="parameter"> The parameter.</param>
protected virtual void TryInjectParameters(object viewModel, object parameter)
{
var viewModelType = viewModel.GetType();

var dictionaryParameter = parameter as IDictionary<string, object>;

if (dictionaryParameter != null)
{
foreach (var pair in dictionaryParameter)
{
var property = viewModelType.GetPropertyCaseInsensitive(pair.Key);

if (property == null)
{
continue;
}

property.SetValue(viewModel, MessageBinder.CoerceValue(property.PropertyType, pair.Value, null), null);
}
}
else
{
var property = viewModelType.GetPropertyCaseInsensitive("Parameter");

if (property == null)
return;

property.SetValue(viewModel, MessageBinder.CoerceValue(property.PropertyType, parameter, null), null);
}
}
}
}
Loading