Views Children Not found

32 Views Asked by At

We have an application which has a startup dialog which is a bit clunky and doesnt look the best. Within this dialog we house a Tab Control which holds a WebView2 which we use for Authentication (using Auth0).

We want to strip back all of this dialog and only have a window which displays the WebView2 object. So I have pulled everything out and am left with the following:

<Window x:Class="App.Windows.Views.AuthenticationDialog"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:wv2="clr-namespace:Microsoft.Web.WebView2.Wpf;assembly=Microsoft.Web.WebView2.Wpf"
        mc:Ignorable="d" Style="{DynamicResource ThemedDialog_Window}" WindowStartupLocation="CenterScreen" WindowStyle="None"
        Title="Authentication" Name="Authentication" Height="600" Width="540">

  <Window.Resources>
    <ResourceDictionary Source="pack://application:,,,/UI.Windows;component/Style.xaml" />
  </Window.Resources>

  <i:Interaction.Triggers>

    <i:KeyTrigger Key="Escape">
      <i:InvokeCommandAction Command="{Binding QuitCommand}" />
    </i:KeyTrigger>

  </i:Interaction.Triggers>

  <Grid>

    <wv2:WebView2 DefaultBackgroundColor="Transparent"
                  CoreWebView2InitializationCompleted="SigninWebView_InitialisationCompleted"
                  Name="signinWebView" />

  </Grid>

</Window>

The issue I am facing is that in order to wire up the WebView2 to our authentication/tokenisation mechanism we use an object tree service to find the child objects of the Parent (window). This mechanism worked fine in our old setup, but in the updated window, the FindChildRecursively method is telling me that the window has 0 children, so the WebView2 control is never located.

Why would the children of the Parent not be found when I call FindChild using this call:

var webView = _objectTreeService.FindChild<AuthenticationDialog, WebView2>("signinWebView");
    public TChild FindChild<TParent, TChild>(string childName)
       where TParent : DependencyObject
       where TChild : DependencyObject
    {
        var parent = _serviceProvider.GetRequiredService<TParent>() ?? throw new InvalidOperationException("Parent cannot found!");

        var foundChild = FindChildRecursively<TChild>(parent, childName);

        return foundChild ?? throw new InvalidOperationException($"Specified child not found! Child name: {childName}");
    }

    private static T? FindChildRecursively<T>(DependencyObject parent, string childName)
       where T : DependencyObject
    {
        T? foundChild = null;

        var childrenCount = VisualTreeHelper.GetChildrenCount(parent);

        for (var i = 0; i < childrenCount; i++)
        {
            var child = VisualTreeHelper.GetChild(parent, i);

            // If the child is not of the request child type child

            if (child is not T)
            {
                // recursively drill down the tree
                if (child != null)
                    foundChild = FindChildRecursively<T>(child, childName);

                // If the child is found, break so we do not overwrite the found child. 
                if (foundChild != null)
                    break;
            }
            else if (!string.IsNullOrEmpty(childName))
            {
                // If the child's name is set for search
                if (child is FrameworkElement frameworkElement && frameworkElement.Name == childName)
                {
                    // if the child's name is of the request name
                    foundChild = (T)child;
                    break;
                }
            }
            else
            {
                // child element found.
                foundChild = (T)child;
                break;
            }
        }

        return foundChild;
    }
0

There are 0 best solutions below