How do i add a second code behind file to a xaml file?

两盒软妹~` 提交于 2019-12-24 06:40:19

问题


In visual studio i want to add a second code behind file to a xaml window (my main form). i know i can have another (or as many) files that form partial parts of a class, and if they are in the same project they will be included, but can i make more than one file sit in the expander (in the solution explorer) when i expand the xaml file to see its code behind?


回答1:


Your project file (.csproj for example) is actually XML. Open the .csproj file from the open file dialog, and you will see how it is structured. You can hand edit the project file from there.




回答2:


I blogged about this a few months ago, you will find the explanation in this blog post

Basically, you just need to add a <DependentUpon> element to your extra code-behind file :

<Compile Include="Window1.Foo.cs">
    <DependentUpon>Window1.xaml</DependentUpon>
</Compile>

As a side note : why would you want a second code-behind file ? I think one is bad enough ;). If you use a pattern like MVVM, you will almost never need to write any code-behind...




回答3:


one such reason is code organization. in a lot of MVVM implementations which ..

  • don't separate V & VM code by projects)
  • have a 1-to-1 between View/Xaml & ViewModel

.. it might be advantageous to have your solution explorer look like:

MyView.xaml
-- MyView.xaml.cs
-- MyViewModel.cs

check out this VisualStudio add-in:

NestIn - Visual Studio Gallery



来源:https://stackoverflow.com/questions/1452353/how-do-i-add-a-second-code-behind-file-to-a-xaml-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!