How to copy paste WPF window (clone) and not have errors

I am using .Net 4.5.2 with WPF and C # 5.0. I created a window in a WPF project. I would like to copy this window to solution explorer, make a second window, and rename it to a new name.

When I do this, the new (copied) InitializeComponent () window always gives me an error. How do I clear a copy of a window (and its code, etc.) in the solution explorer?

This question was partially answered: WPF copy paste window gives error , however the answer did not solve my problem.

My approach (which doesn't work):

  • Create a window and write it WindowTest
  • In the solution explorer select WindowTest and copy then paste it into the same project
  • Rename the new copied window to WindowTestCopy
  • In WindowTestCopy change the x: class property in xaml to be WindowTestCopy instead of WindowTest
  • Open the code in WindowTestCopy and change any WindowTest references to WindowTestCopy
  • Compile

Expected: No error, copy (clone) operation succeeded

Relevant: compilation error "Unable to access non-stationary method" InitializeComponent "in static context".

I only have one error. Obviously InitializeComponent () becomes an ambiguous reference, but I'm not clear on how to make manual changes to the code to fix this. I want VS or Resharper to automatically help me with this.

UPDATE

WindowTest contains two user controls that I didn't mention earlier. After the copy / paste occurs, for some reason, incorrect xaml elements appeared in WindowTestCopy:

Xmlns: UserControls .... (ellided) Xmlns: UserControls .... (ellided)

After removing this data, Resharper determined that the userControl objects were missing the xmlns link tags and asked me if I wanted to import them automatically. I chose yes. After Resharper added the missing xmlns reference tags I was able to compile (all errors went away).

I have no explanation as to why this happened. In my steps to reproduce, I am not editing the xaml and so it should be identical to the original xaml. This is curious behavior, but at least there is a workaround as stated.

+3


source to share


3 answers


Someone else posted and then removed the correct idea, but briefly on why.

The problem you are facing has to do with duplicating a class within the same project ... although in the solution explorer you can easily copy / paste a WPF form or any other class, user management class, etc. When copying, it suffixes the filenames with "- Copy", and if you compile right away, it will crash. Cause? In the class itself, the class name and window name are the same. So, in a project, you have two instances of a class, and it chokes on that, hoping that you will allow duplicates. I ran into this exact same case early in WPF development.

So now that I've explained why this is happening, here's what you need to do to fix the problem. I created a window in my sample project "FirstWindowSample", so two files FirstWindowSample.xaml and FirstWindowSample.xaml.cs are created. Cs version is window code (or custom control class, principle is EXACTLY the same.

If you look in the code for "FirstWindowSample.xaml" (visual design version), the code looks something like this:

<Window x:Class="YourProject.FirstWindowSample"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="FirstWindowSample" Height="300" Width="300">
    <Grid>

    </Grid>
</Window>

      

Note that on the first line

   x:Class="YourProject.FirstWindowSample"

      



You need to change ".FirstWindowSample" to whatever you need for the new form, for example

   x:Class="YourProject.SecondWindowSample"

      

Then navigate to the CODE-BEHIND file "FirstWindowSample.xaml.cs".

namespace YourProject
{
    /// <summary>
    /// Interaction logic for FirstWindowSample.xaml
    /// </summary>
    public partial class FirstWindowSample : Window
    {
        public FirstWindowSample()
        {
            InitializeComponent();
        }
    }
}

      

This is a hard link for the class name and its corresponding constructor. Just change them to match the new window name that you expect (e.g. SecondWindowSample sample)

namespace YourProject
{
    /// <summary>
    /// Interaction logic for SecondWindowSample.xaml
    /// </summary>
    public partial class SecondWindowSample : Window
    {
        public SecondWindowSample()
        {
            InitializeComponent();
        }
    }
}

      

At the end, your project will now have a completely different class name that is not duplicated and does not cause compilation errors. FOR THE FIRST TIME that you face this, yeah ... pain to find out. Once you get it, the next 2-3 times is "oh yeah", freshener ... After that, you do it without thinking about it.

+2


source


When it happened to me, it was like this:



  • Copy xaml to solution explorer.
  • Rename xaml in solution explorer.
  • Rename the x: class to xaml.
  • Rename the class name to xaml.cs.
  • Rename the constructor to xaml.cs.
  • Why is it still not working? <== (you're here probably)
  • Understand that there is a secret sorcery witchcraft underneath.
  • Copy the contents of xaml and xaml.cs into a couple of notebooks.
  • Remove xaml from solution explorer.
  • Create a new xaml in solution explorer with the desired name.
  • Write down both xaml and xaml.cs with the exact contents of the two notebooks.
  • The errors disappeared.
+2


source


The problem was solved by removing all xlmns elements in xaml and allowing resharper to automatically add them back. I am not satisfied with this decision because it is illogical; however it resolved the compilation error.

0


source







All Articles