Using LoadXaml() to load non-noesis objects
Hi everybody,
from WPF I'm used to using XamlReader.Load() to load files that contain non-UI elements, like for example configuration files. The xaml format enables one to write pretty complex configuration files.
I tried this with LoadXaml() and it crashes Unity. Sometimes i get an error message saying that the "XAML file is empty" (which is not the case) and then it produces that message endlessly.
So it's obviously not possible to load XAMLs that do not contain UI elements, right?
Günther
from WPF I'm used to using XamlReader.Load() to load files that contain non-UI elements, like for example configuration files. The xaml format enables one to write pretty complex configuration files.
I tried this with LoadXaml() and it crashes Unity. Sometimes i get an error message saying that the "XAML file is empty" (which is not the case) and then it produces that message endlessly.
So it's obviously not possible to load XAMLs that do not contain UI elements, right?
Günther
-
sfernandez
Site Admin
- Posts: 3005
- Joined:
Re: Using LoadXaml() to load non-noesis objects
You should be able to load any kind of data defined in your XAML files as long as it uses classes defined in the Unity project.
Do you have those xaml assets referenced in your Unity scene somehow? Because if the assets are not part of the scene they are not included in the game and cannot be loaded. As we mentioned in our Unity tutorial you can manually add Xaml.Dependencies to another XAML if you want, or you can reference those configuration files (xaml assets) in any component of your scene.
Please let me know if that was the cause of your problem?
Do you have those xaml assets referenced in your Unity scene somehow? Because if the assets are not part of the scene they are not included in the game and cannot be loaded. As we mentioned in our Unity tutorial you can manually add Xaml.Dependencies to another XAML if you want, or you can reference those configuration files (xaml assets) in any component of your scene.
Please let me know if that was the cause of your problem?
Re: Using LoadXaml() to load non-noesis objects
Hi Fernandez,
yes, that was probably one cause of the problem. The other problem was that a file with only a root tag and two attributes didn't load and made Unity crash, although it loaded fine under WPF.
We cannot preload the file because it is a settings file that should be editable by the user before app start.
I switched to a simple XML file - that should do it for now.
But thanks for your response!
Günther
yes, that was probably one cause of the problem. The other problem was that a file with only a root tag and two attributes didn't load and made Unity crash, although it loaded fine under WPF.
We cannot preload the file because it is a settings file that should be editable by the user before app start.
I switched to a simple XML file - that should do it for now.
But thanks for your response!
Günther
-
sfernandez
Site Admin
- Posts: 3005
- Joined:
Re: Using LoadXaml() to load non-noesis objects
Could you please attach an example of this, we shouldn't crash Unity editor never and we want to fix it.The other problem was that a file with only a root tag and two attributes didn't load and made Unity crash, although it loaded fine under WPF.
Understood. But in that case you will probably be loading the file using a simple File.Open, right? Then another option could be to use XamlReader.Parse() to parse the contents of the file.We cannot preload the file because it is a settings file that should be editable by the user before app start.
Re: Using LoadXaml() to load non-noesis objects
I'll see if I can build a small example of the crashing file. I don't have it anymore because I deleted it... ;-)
We are using the XmlSerializer at the moment:
I wasn't aware that ther is a XamlReader.Parse() method - maybe I'll try that.
We are using the XmlSerializer at the moment:
Code: Select all
XmlSerializer serializer = new XmlSerializer(typeof(T));
T newObject;
using (var reader = File.OpenText(path))
{
newObject = (T)serializer.Deserialize(reader);
}
Re: Using LoadXaml() to load non-noesis objects
Hi again,
I wanted to deliver the crashing XAML:
The class is actually empty:
This crashes Unity when I try to parse it with Noesis.GUI.ParseXaml().
It even crashes Unity when Unity touches the file, for example when a project is loaded and the file is parsed.
btw: I know that the namespaces in the XAML are wrong; this was just for testing purposes. But anyway, even if I use Karma.Config as default namespace the XamlReader still complains about not having a default namespace" and returns null. Any idea what I'm doing wrong?
Best regards,
G.
I wanted to deliver the crashing XAML:
Code: Select all
<KarmaPackage
xmlns="http:=//schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:y="clr-namespace:Karma.Config" />
Code: Select all
namespace Karma.Config
{
[Serializable]
public class KarmaPackage
{
}
}
It even crashes Unity when Unity touches the file, for example when a project is loaded and the file is parsed.
btw: I know that the namespaces in the XAML are wrong; this was just for testing purposes. But anyway, even if I use Karma.Config as default namespace the XamlReader still complains about not having a default namespace" and returns null. Any idea what I'm doing wrong?
Best regards,
G.
-
sfernandez
Site Admin
- Posts: 3005
- Joined:
Re: Using LoadXaml() to load non-noesis objects
Thanks for the repro xaml.
It turns out that the problem was with single node xamls that doesn't define the default namespace. In your case you have a typo in the first xmlns address and wasn't recognized, it should be:
We fixed the crash for the next release.
It turns out that the problem was with single node xamls that doesn't define the default namespace. In your case you have a typo in the first xmlns address and wasn't recognized, it should be:
Code: Select all
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Re: Using LoadXaml() to load non-noesis objects
But what if I need an own default namespace? As I said, I want to read non-noesis objects which have their own namespace.
Thanks for your reply!
G.
Thanks for your reply!
G.
-
sfernandez
Site Admin
- Posts: 3005
- Joined:
Re: Using LoadXaml() to load non-noesis objects
Since 3.1.2 we support assigning non-null prefix to the default namespace, so you can use it for your own objects, so this should work:
Although we should probably remove the requirement of declaring default namespace all along, as WPF doesn't need that, could you please report that in our bugtracker?
Code: Select all
<KarmaPackage
xmlns:def="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns="clr-namespace:Karma.Config"/>
Re: Using LoadXaml() to load non-noesis objects
I wouldn't say that WPF doesn't use the default namespace (xmlns=) - it does use it.
In our example we have a configuration file containing dumb DTOs (POCOs), which are then wrapped in model objects with the same name but in a different namespace. If I wouldn't be able to set the default namespace, then it would be coincidence which namespace the parser takes.
So I wouldn't say it's a bug...
btw: it is working now. Thanks for the support!
In our example we have a configuration file containing dumb DTOs (POCOs), which are then wrapped in model objects with the same name but in a different namespace. If I wouldn't be able to set the default namespace, then it would be coincidence which namespace the parser takes.
So I wouldn't say it's a bug...
btw: it is working now. Thanks for the support!
Who is online
Users browsing this forum: Ahrefs [Bot] and 8 guests