-
Notifications
You must be signed in to change notification settings - Fork 677
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
How to load XAML dynamically via a Uri #4457
Comments
@billhenn XamlReader.Load can load markup and build an object for you. Would that work for you ? |
@ranjeshj I did see that API and it probably would work if I could get the XAML. The problem is that when the XAML (and its possible code-behind file) are included in the project as the default Build Action of "Page", I'm not sure how to access the XAML. I may be wrong, but it seems like the XAML might have to be included with a Build Action of "Content" in order to even access the file via "ms-appx://", and that's only because it's then shipping the file as a loose file, which is not what I want to do. I want to be able to access the XAML from the normal "Page" compilation. However Application.LoadComponent is getting to it, and be able to then do a The simplest thing would be if there was an |
@billhenn if the XAML pages are being included in the compilation as Why is using a You can use general reflection APIs to crawl your app's included assemblies for the pages in whatever namespaces you've put them in too, so if it's just a matter of not knowing type names at runtime due to conditional build steps coming from other sources, that should be fine as well. (Though a better solution now would be to have an incremental source generator do that instead at compile time and create a registry of all known XAML page types that the app can then just use to grab a particular page). |
Related to #6299 as well. |
This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 5 days. |
We have a need where we want to dynamically load XAML that has been added to our application with build action of "Page" and render it as the content of our window. We have hundreds of these XAML files and a list of the paths to each of them. Our app UI lets us navigate around them.
In WPF where we have a similar setup, we could call
Application.LoadComponent
, pass it the path, and it would return the resulting object. The object was aFrameworkElement
and we'd simply set it asContentControl.Content
and it would display in the window.However in WinUI, the
Application.LoadComponent
API is different. There doesn't seem to be an overload that returns a created and initialized instance of whatever was at the path. We could really use that.Instead, the WinUI version seems to want you to have already created an instance of the object to initialize, and then it will further initialize the object based on the XAML at the specified path. In our case, this is bad because we don't always know the type that the XAML is associated with. Sometimes it might be a basic
UserControl
. Other times it might be a specialControl
-based class with custom code-behind.Is there any other way to get behavior like what is in the
Application.LoadComponent
in WPF, where you point it to a path and it returns the object that was created AND initialized? If not, can that be added to WinUI?The text was updated successfully, but these errors were encountered: