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

3

1

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?

Furfur answered 20/9, 2009 at 23:48 Comment(0)
C
2

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.

Cameron answered 20/9, 2009 at 23:55 Comment(1)
that works, thank you. before : the new code behind file <Compile Include="file.open.cs" /> just copied the old dependency info from the other one, now i have two code behinds there. <Compile Include="file.open.cs" > <DependentUpon>file.xaml</DependentUpon> </Compile> cool as, wish i didnt have to go to xml to do it but...Furfur
C
3

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...

Cleat answered 21/9, 2009 at 0:9 Comment(0)
C
2

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.

Cameron answered 20/9, 2009 at 23:55 Comment(1)
that works, thank you. before : the new code behind file <Compile Include="file.open.cs" /> just copied the old dependency info from the other one, now i have two code behinds there. <Compile Include="file.open.cs" > <DependentUpon>file.xaml</DependentUpon> </Compile> cool as, wish i didnt have to go to xml to do it but...Furfur
V
1

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

Vlada answered 20/9, 2009 at 23:49 Comment(2)
Please expand URL shortened link.Biscuit
This was when we found a bunch of URL shortened links and were in the middle of a cleanup.Biscuit

© 2022 - 2024 — McMap. All rights reserved.