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

Expose created Maui view on the Maui Host (backport #1803) #1807

Merged
merged 3 commits into from
Aug 25, 2023
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
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; }
}