Skip to content

Commit

Permalink
Merge pull request #1803 from unoplatform/dev/ds/expose-maui-content
Browse files Browse the repository at this point in the history
Expose created Maui view on the Maui Host
  • Loading branch information
nickrandolph authored Aug 24, 2023
2 parents 4e12a32 + 0546134 commit 0259182
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

<embed:MauiHost Grid.Row="1"
x:Name="mauiContent"
VisualElementChanged="OnVisualElementChanged"
Source="external:SyncfusionDemoPage" />

</Grid>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@ private void MainPage_DataContextChanged(FrameworkElement sender, DataContextCha
{

}

private void OnVisualElementChanged(object sender, VisualElementChangedEventArgs args)
{

}
}
40 changes: 25 additions & 15 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.VisualElement = 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);
var native = visualElement.ToPlatform(mauiContext);
mauiHost.Content = native;

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

private VisualElement? EmbeddedView;

/// <summary>
/// Initializes a new instance of the MauiContent class.
/// </summary>
Expand Down Expand Up @@ -121,13 +121,13 @@ private void OnActualThemeChanged(FrameworkElement sender, object args)

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

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

Expand All @@ -141,7 +141,17 @@ private void OnMauiContentUnloaded(object sender, RoutedEventArgs e)
#endif

/// <summary>
/// Gets or sets the <see cref="Type"/> of the Maui Content Source
/// Fires when the VisualElement has changed.
/// </summary>
public event EventHandler<VisualElementChangedEventArgs> VisualElementChanged = delegate { };

/// <summary>
/// Gets the Maui <see cref="VisualElement"/> created by the Source.
/// </summary>
public VisualElement? VisualElement { get; private set; }

/// <summary>
/// Gets or sets the <see cref="Type"/> of the Maui Content Source.
/// </summary>
public Type? Source
{
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 && VisualElement is not null)
{
EmbeddedView.Resources.MergedDictionaries.Add(resources.ToMauiResources());
VisualElement.Resources.MergedDictionaries.Add(resources.ToMauiResources());
_initializedResources = true;
}
}

void OnDataContextChanged(FrameworkElement sender, DataContextChangedEventArgs args)
{
if (EmbeddedView is not null &&
EmbeddedView.BindingContext != DataContext)
if (VisualElement is not null &&
VisualElement.BindingContext != DataContext)
{
EmbeddedView.BindingContext = DataContext;
VisualElement.BindingContext = DataContext;
}
}
#endif
Expand Down
11 changes: 11 additions & 0 deletions src/Uno.Extensions.Maui.UI/VisualElementChangedEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Uno.Extensions.Maui;

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

public VisualElement Content { get; }
}

0 comments on commit 0259182

Please sign in to comment.