Wpf merged resource dictionary no being recognized from app.xaml

前端 未结 3 1781
暗喜
暗喜 2020-12-23 21:49

I have a WPF .net 4.5 application where I am having trouble merging resource dictionaries.

I have the exact same problem as This SO question and This Question but th

3条回答
  •  南方客
    南方客 (楼主)
    2020-12-23 22:25

    I had to introduce themes into our application and faced these exact problems.

    The short answer is this: Resource dictionaries can "See" other Resource dictionaries if they come before them in App.xaml. If you try to use MergedDictiories in a file that is not App.xaml, the resource dictionaries will not "See" each other.

    For default resources in Generic.xaml: You can use resources defined in App.xaml or a merged dictionary out of App.xaml as DynamicResource only. You can use resources defined in Generic.xaml as StaticResource, but only if your style is defined in Generic.xaml itself and not in a merged dictionary inside of Generic.xaml

    For the full answer I have a detailed post in my blog about this issue

    My suggested solution: Create whatever XAML hierarchy you want, and place your files in a folder with .txaml extensions. I created a small simple program (provided below in GitHub) that will run as a pre-build event and merge your .txaml files into one long .XAML file.

    This allows to structure resources folders and files however you want, without WPF’s limitations. StaticResource and the designer will work always. This is the only solution where you can have CustomControl styles in multiple files, not just one long Generic.xaml.

    This will also solve whatever performance issues multiple XAML files create.

    Xaml merging program in GitHub

提交回复
热议问题