Skip to content

Commit

Permalink
feat: expose created Maui view on the Maui Host
Browse files Browse the repository at this point in the history
  • Loading branch information
dansiegel committed Aug 24, 2023
1 parent 4e12a32 commit ced5760
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 14 deletions.
11 changes: 11 additions & 0 deletions src/Uno.Extensions.Maui.UI/MauiContentCreatedEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Uno.Extensions.Maui;

public class MauiContentCreatedEventArgs : EventArgs
{
public MauiContentCreatedEventArgs(VisualElement content)
{
Content = content;
}

public VisualElement Content { get; }
}
38 changes: 24 additions & 14 deletions src/Uno.Extensions.Maui.UI/MauiHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,21 @@ dependencyObject is not MauiHost mauiHost ||

// Allow the use of Dependency Injection for the View
var instance = ActivatorUtilities.CreateInstance(mauiContext.Services, type);
if(instance is VisualElement page)
if(instance is VisualElement visualElement)
{
mauiHost.EmbeddedView = page;
page.Parent = app;
page.BindingContext = mauiHost.DataContext;
mauiHost.Content = visualElement;
visualElement.Parent = app;
visualElement.BindingContext = mauiHost.DataContext;
}
else
{
throw new MauiEmbeddingException(string.Format(Properties.Resources.TypeMustInheritFromPageOrView, instance.GetType().FullName));
}

var native = page.ToPlatform(mauiContext);
mauiHost.Content = native;
var native = visualElement.ToPlatform(mauiContext);
mauiHost.NativeView = native;

mauiHost.MauiContentCreated.Invoke(mauiHost, new MauiContentCreatedEventArgs(visualElement));
}
catch (Exception ex)
{
Expand All @@ -80,7 +82,11 @@ dependencyObject is not MauiHost mauiHost ||
private static ILogger GetLogger() =>
IPlatformApplication.Current?.Services.GetRequiredService<ILogger<MauiHost>>() ?? throw new MauiEmbeddingInitializationException();

private VisualElement? EmbeddedView;
private object NativeView
{
get => base.Content;
set => base.Content = value;
}

/// <summary>
/// Initializes a new instance of the MauiContent class.
Expand All @@ -97,6 +103,8 @@ public MauiHost()
ActualThemeChanged += OnActualThemeChanged;
}

public event EventHandler<MauiContentCreatedEventArgs> MauiContentCreated = delegate { };

private void OnActualThemeChanged(FrameworkElement sender, object args)
{
if (IPlatformApplication.Current is null || IPlatformApplication.Current.Application is not MauiApplication app)
Expand All @@ -121,13 +129,13 @@ private void OnActualThemeChanged(FrameworkElement sender, object args)

private void OnMauiContentLoaded(object sender, RoutedEventArgs e)
{
var page = GetPage(EmbeddedView);
var page = GetPage(Content);
page?.SendAppearing();
}

private void OnMauiContentUnloaded(object sender, RoutedEventArgs e)
{
var page = GetPage(EmbeddedView);
var page = GetPage(Content);
page?.SendDisappearing();
}

Expand All @@ -140,6 +148,8 @@ private void OnMauiContentUnloaded(object sender, RoutedEventArgs e)
};
#endif

public new VisualElement? Content { get; private set; }

/// <summary>
/// Gets or sets the <see cref="Type"/> of the Maui Content Source
/// </summary>
Expand Down Expand Up @@ -174,19 +184,19 @@ private void OnLoading(FrameworkElement sender, object args)
treeElement = VisualTreeHelper.GetParent(treeElement);
}

if (!_initializedResources && EmbeddedView is not null)
if (!_initializedResources && Content is not null)
{
EmbeddedView.Resources.MergedDictionaries.Add(resources.ToMauiResources());
Content.Resources.MergedDictionaries.Add(resources.ToMauiResources());
_initializedResources = true;
}
}

void OnDataContextChanged(FrameworkElement sender, DataContextChangedEventArgs args)
{
if (EmbeddedView is not null &&
EmbeddedView.BindingContext != DataContext)
if (Content is not null &&
Content.BindingContext != DataContext)
{
EmbeddedView.BindingContext = DataContext;
Content.BindingContext = DataContext;
}
}
#endif
Expand Down

0 comments on commit ced5760

Please sign in to comment.