简体   繁体   中英

How do I reconnect a XAML view file with it's code behind file?

I find very frequently that while I'm 'prototyping', and I change the base type of the code behind class, or something like that, that the two files become completely unaware of each other. Example:

XAML:

<UserControl x:Class="G4S.XTime.Modules.Employees.Details.Views.EmployeeGridView" 
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             ...

Code-behind:

namespace G4S.XTime.Modules.Employees.Details.Views
{
  public sealed partial class EmployeeGridView: UserControl
  {
    public EmployeeGridView()
    {
      //InitializeComponent();
    }
  }
}

The call InitializeComponent produces a compile time error, saying it doesn't exist.

This disconnect phenomenon happens often enough to be costing me time, and I often just copy the code out of both files, delete the view, add a new view with the same name, paste the same code back, and everything works.

What am I missing that connects the two files? In the project file, I see the code-behind depends on the XAML, so I think if I comment out InitializeComponent , then compile with only the XAML, I will have the other part of my code-behind partial class. But this does not work. It doesn't seem to compile the XAML at all unless there is a code behind.

What can I do to reconnect these two files, in most cases?

Edit your project file and make sure you have something similar to this:

<Compile Include="EmployeeGridView.xaml.cs">
  <DependentUpon>EmployeeGridView.xaml</DependentUpon>
  <SubType>Code</SubType>
</Compile>

For me the problem was caused by not having the correct way of including the files in the csproj file.

incorrect:

<CodeAnalysisDictionary Include="Windows\ConnectionSecuritySettings.xaml">

correct:

<Page Include="Windows\ConnectionSecuritySettings.xaml">

This happened when I moved the items to a new project

I had the same problem ( InitializeComponent could not be found) after a cut-paste of my XAML. The answer suggested here solved my problem. The suggestions is, in the Properties window of the XAML, change the Build Action to Page . Apparently the copy-paste can change the Build Action to Resource .

Hope this helps!

[Edit] I just wanted to add that this was also after updating the namespace for both the code-behind and in the xaml:

    x:Class="NewNamespace.CodeBehindClass"      

Make sure the declaration at the top of the xaml matches the code behind file with the full path including namespace.

eg. If namespace name is "MyControls" and Code behind Class is "MyNewControl" then xaml declaration should be something like ..

    <UserControl x:Class="MyControls.MyNewControl"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"     
          xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

          mc:Ignorable="d"

          Height="41" Width="77"
          >

and code behind would be ..

namespace MyControls
{
    /// <summary>
    /// Interaction logic for MyNewControl.xaml
    /// </summary>
    public partial class MyNewControl: UserControl
    {
        #region Constructors
        public MyNewControl()
        {
            InitializeComponent();
        }
        #endregion
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM