XAML designer exception when casting Window.Current.Content as Frame

Aria Rad 51 Reputation points
2022-06-20T12:57:59.77+00:00

Hello,

I have an old (2015-ish) UWP project which I recently decided to resume work on in Visual Studio 2022 (Version 17.2.4). The solution originally contained just a Windows Phone project to which I added a UWP project for use on different device types. The migration process to VS2022 went without trouble but inside Visual Studio, the XAML designer throws me the following exception:

System.InvalidCastException: Unable to cast object of type 'XSurfUwp.ContentWrapper' to type 'Windows.UI.Xaml.Controls.Frame'.    
   at WindowCurrentContentAsFrame.GlobalNavPage.GlobalNavPage_Loaded(Object sender, RoutedEventArgs e)  

Note: this is purely a designer issue as I can run the app fine.
To the best of my abilities I have been able to track this issue down to a piece of code where I am casting the current window's Content as a Frame inside a custom class, like so:

((Frame)Window.Current.Content) ...  

...and working with this to add buttons to a command bar, depending on which page the user is on.
Strangely, I found very little information online about XSurfUwp.ContentWrapper. And what was there did not help me understand why this designer exception occurs.

I've been able to strip the code down to make a basic reproduction here. MainPage.xaml's designer view should throw the exception when you open it. You may need to Rebuild the solution first.
Even though this is an exception that only occurs in the XAML designer, it would still be nice if I could fix this issue so I can see my XAML pages while working on the project.

Any assistance would be greatly appreciated.

Universal Windows Platform (UWP)
Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
4,482 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Aria Rad 51 Reputation points
    2022-06-22T16:32:57.813+00:00

    I received feedback on this issue. As I understand, it has to do with making your app designer-aware by checking for DesignMode.

    I solved this issue by executing the code that casts Window.Current.Content as a Frame only if DesignMode is not enabled.
    The resulting code in GlobalNavPage_Loaded looks like this:

            string currentFrameSourceName = "";  
    
            if (!Windows.ApplicationModel.DesignMode.DesignModeEnabled || !Windows.ApplicationModel.DesignMode.DesignMode2Enabled)  
            {  
                currentFrameSourceName = ((Frame)Window.Current.Content).SourcePageType.ToString();  
            }  
    
            if (currentFrameSourceName == "WindowCurrentContentAsFrame.MainPage")  
            {  
                commandbar.SecondaryCommands.Add(HelpButton);  
                commandbar.SecondaryCommands.Add(AboutButton);  
            }  
            else  
            {  
                commandbar.SecondaryCommands.Add(AboutButton);  
            }  
    

    Now the XAML designer displays the XAML page correctly instead of throwing an InvalidCastException.

    0 comments No comments