User avatar
horeaper
Posts: 34
Joined: 22 Sep 2014, 12:50

Re: Unity3D autogenerate .g.cs

24 Jun 2019, 09:18

The feature "Skip if x:Class does not exist" doesn't work properly.
For example, parse NocturnalStyle.xaml (theme provided in noesis native SDK) will cause a .g.xaml get generated, which causes compile error.
It's a ResourceDictionary so it doesn't have "FindName" method. Also the generated code will have an empty namespace statement which is illegal.
Further investment found out the problem to be in "string GetCsText()" method. Which I made another small modification:
string GetCsText()
{
	var namespaceString = GetNamespaceString();
	var className = GetClassNameString();
	var hasCodeBehind = className != null && namespaceString != null;
	if (!hasCodeBehind) {
		return null;
	}
...
It works again now. But this is not the final solution, since in WPF, ResourceDictionary can have x:Class, just without the FindName() method. The method does exist but it's an empty implementation (hoory to open source!). So I think that means you can hook events, but not x:Name.
I will try to fix it after digging a little deeper.
 
User avatar
horeaper
Posts: 34
Joined: 22 Sep 2014, 12:50

Re: Unity3D autogenerate .g.cs

24 Jun 2019, 09:46

Ok this is my result: https://gist.github.com/horeaper/274914 ... 30590319ce

What's changed:
1. Bug fixes (obviously)
2. Changed the coding style (both source and generated) a little to fit my likings 😊
3. Changed the access modifer of the generated fields to "internal", which is what WPF does by default.
4. The original code skips generation if there was no x:Name and events, but for newly created XAML, if it's created by Visual Studio, the code will have a "InitializeComponent()" call by default, and that will cause a compile error in Unity if you didn't go through the "Comment this line -> Wait for generation done -> Uncomment that line" process. This is no longer the case.

This is by far NOT the perfect solution, for example "x:Modifier" is not handled. But it works!
What's next: Dump the Regex solution and use proper XML analysis, it's more robust, also if user changed the xmlns to something other than "x" it will still work. But that's gonna take some doing, so, not today! 😁
 
User avatar
CyberFox
Topic Author
Posts: 6
Joined: 06 May 2019, 10:41

Re: Unity3D autogenerate .g.cs

25 Jun 2019, 08:41

Thanks for the extra work. I have opened a github repo https://github.com/CyberFoxHax/NoesisGU ... sGenerator
should be better than stitching the code together in the forums. I'll add you as a collaborator.

I don't want the version history bloated with bracket changes, so i wont be accepting any changes to code style. I have merged most of your changes. If i missed anything please tell me.

Just one note:
In 73 you wrote baseClassName != "ResourceDictionary" Though i can't see a reason to do it, i do believe it is possible to use a codebehind in ResourceDictionary.

(Btw i have no clue what x:Modifier even is)
 
User avatar
horeaper
Posts: 34
Joined: 22 Sep 2014, 12:50

Re: Unity3D autogenerate .g.cs

25 Jun 2019, 11:05

Just one note:
In 73 you wrote baseClassName != "ResourceDictionary" Though i can't see a reason to do it, i do believe it is possible to use a codebehind in ResourceDictionary.

(Btw i have no clue what x:Modifier even is)
What I means are x:ClassModifier and x:FieldModifier.

ResourceDictionary allows code behind but not with x:Name, so the generated code should ignore them.
Aside from that I believe you got everything.

I have forked your code so I can stick to my style. 😝 If I found any more bugs I'll create a PR on your repo.
 
User avatar
CyberFox
Topic Author
Posts: 6
Joined: 06 May 2019, 10:41

Re: Unity3D autogenerate .g.cs

25 Jun 2019, 12:31

much appreciated!

Who is online

Users browsing this forum: No registered users and 4 guests