batzen / XAMLTools

MIT License
36 stars 4 forks source link

Support ResourcesDictionary With a Code-Behind #78

Open ghost1372 opened 1 year ago

ghost1372 commented 1 year ago

hi, i have a Library and in this library i have a ResourcesDictionary file called ItemTemplate.xaml in this file i have a DataTemplate, for using x:Bind method i have to set an empty Code-Behind for this ResourcesDictionary. when i compile my library with merging this ResourcesDictionary into Generic.xaml file and running an example project, i get some exceptions. is this supported? if not can you please add it?

https://github.com/WinUICommunity/LandingsPage/blob/main/LandingsPage/Themes/ItemTemplates.xaml

https://github.com/WinUICommunity/LandingsPage/blob/main/LandingsPage/Themes/ItemTemplates.xaml.cs

batzen commented 1 year ago

The code behind file does not seem to be generated, can't you just add a Generic.xaml.cs that has the required content?

ghost1372 commented 1 year ago

Generic file is generated by your tool, how i can add a code-behind for this?

What do you think about this Idea, if we consider a property for example IsCodeBehind

<XAMLCombineItems Include="Themes\ItemTemplate.xaml">
      <TargetFile>Themes\Generic.xaml</TargetFile>
      <Visible>True</Visible>
      <IsCodeBehind>True</IsCodeBehind>
    </XAMLCombineItems>

Then, instead of the contents of this dictionary being placed inside the generic, We only have one reference to this dictionary

in Generic.xaml-->

<theme:ItemItemplate/>

batzen commented 1 year ago

The Generic.xaml is generated by my tool, but it doesn't know or care about any code files. If you just create a Generic.xaml.cs at the same directory the compiler should just pick it up, as my tool runs before the compiler does.

ghost1372 commented 1 year ago

So how about x:class attribute? Does this attribute added to generic.xaml by tool?

batzen commented 1 year ago

All attributes/namespaces are taken over to the generated file. There might only be one issue with your x:class. If you have multiple source files with x:class, but a different value for each that won't work.

Have you tried just adding a Generic.xaml.cs for your case?

ghost1372 commented 1 year ago

I will test as soon as I get home But I guess it won't work because the x:class (mynamespace.folder.itemtemplate) in itemtemplate.xaml is connected to itemtemplate.cs codebehind If i create a generic.cs file how exactly will the x:class be connected to the generic.cs file? I am confused a little

batzen commented 1 year ago

You could try copying your existing file and just rename it to Generic.xaml.cs. That might work. I have to admit that i haven't done any WinUI development so far. So it's just guessing from my WPF experience.

ghost1372 commented 1 year ago

Ok tnx i should test, Do you have any comment about my idea? Adding a property in xamlcombineitem csproj? I think this can be work