Click here to Skip to main content
15,889,592 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello!

I seem to be facing a small dilemma and that would be that my application wont compile the C# code i need it to compile

If I dont add the reference it says

C#
[The type or namespace name 'Window' could not be found (are you missing a using directive or an assembly reference?)


and when I add
C#
PresentationFramework.dll


it says
C#
Metadata file 'PresentationFramework.dll' could not be found


I've done some reasearch and it seems like
C#
The type or namespace name 'Window' needs PresentationFramework.dll


Why is my application throwing me this error when I try to compile inside my application (not from VS)


My source code

C#
using Microsoft.CSharp;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Linq;
using System.Windows;


namespace SimpleBuilder
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// store code
    /// select what features
    /// print out textfile with all the code from the features
    /// compile that textfileContent to a exe
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        
        private void fakeMessageOne()
        {
            if(fakeMessageCheckbox.IsChecked == true)
            {
                fakeMessage1 fkmsg = new fakeMessage1();
                fkmsg.fakeMessage();
            }
        }

        private void button_Click(object sender, RoutedEventArgs e)
        {
            
            

            CSharpCodeProvider csc = new CSharpCodeProvider(new Dictionary<string, string>() { { "CompilerVersion", frameworkTextbox.Text } });
            CompilerParameters parameters = new CompilerParameters(new[] { "mscorlib.dll", "System.Core.dll" }, outputTextbox.Text, true);
            parameters.GenerateExecutable = true;
            parameters.ReferencedAssemblies.Add("Microsoft.CSharp.dll");
            parameters.ReferencedAssemblies.Add("System.dll");
            //parameters.ReferencedAssemblies.Add("PresentationFramework.dll"); commented this out since it wasnt working.

            CompilerResults result = csc.CompileAssemblyFromSource(parameters, sourceTextbox.Text);
            if (result.Errors.HasErrors)
            {
                result.Errors.Cast<CompilerError>().ToList().ForEach(error => errorTextbox.Text += error.ErrorText + "\r\n");
            }
            else
            {
                errorTextbox.Text = "--- Build Succeeded! ---";
            }

        }
    }
}


The code im trying to compile

C#
using Microsoft.CSharp;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Linq;
using System.Windows;


namespace SimpleBuilder
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// store code
    /// select what features
    /// print out textfile with all the code from the features
    /// compile that textfileContent to a exe
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void fakeMessageOne()
        {
            Messagebox.Show("Hello World");
        }
    }
}


What I have tried:

I've done some rerearch about the dll but cant find any others
Posted
Updated 28-Sep-16 7:17am
Comments
Richard Deeming 28-Sep-16 12:46pm    
Are you adding the reference in Visual Studio, or trying to compile via the csc.exe command-line?
Goober S. Johnsson 28-Sep-16 12:48pm    
I already have it in VS, Im trying to add it through

parameters.ReferencedAssemblies.Add("PresentationFramework.dll");

1 solution

The problem is that the PresentationFramework.dll assembly isn't in a directory that's in the compiler's search path. Instead, it's in a sub-directory called "WPF" under the main framework directory:
C#
parameters.ReferencedAssemblies.Add("WPF\\PresentationFramework.dll");

You'll also need some other references:
C#
parameters.ReferencedAssemblies.Add("System.Xaml.dll");
parameters.ReferencedAssemblies.Add("WPF\\WindowsBase.dll");
parameters.ReferencedAssemblies.Add("WPF\\PresentationCore.dll");

You'll also need to correct the case of the MessageBox class in your code to compile.

With those changes in place, you'll then find a single compiler error:
The name 'InitializeComponent' does not exist in the current context

That's because your compilation unit doesn't include any of the artefacts that Visual Studio generates for you when you create a new WPF window, including the BAML (compiled XAML) and the other part of the partial class.

If you look at an existing WPF project in Windows Explorer, you'll see a folder called obj. Under that, in a folder with the same name as the configuration (typically Debug or Release), you'll see that each WPF window has three compiler-generated files:
  • WindowName.baml - the compiled version of the XAML markup;
  • WindowName.g.cs - defines the fields, the InitializeComponent method to load the BAML from the embedded resources, and code to connect the fields to the loaded BAML;
  • WindowName.g.i.cs - seems to be a duplicate of .g.cs, possibly used for Intellisense;

Since you're generating the code manually, you'll need to duplicate these artefacts somehow, or move the code from the .g.cs file into your main class.
 
Share this answer
 
Comments
Goober S. Johnsson 28-Sep-16 14:06pm    
I saw a tutorial on YouTube and it worked perfectly for him, but then again he used a WinForm application.
Richard Deeming 28-Sep-16 14:12pm    
Yes, WinForms is a lot simpler than WPF. :)
Goober S. Johnsson 28-Sep-16 14:44pm    
Can I mix both? Lets say I have a wpf application, I can still add a winForm to it right?
Richard Deeming 28-Sep-16 14:48pm    
Yes.
Goober S. Johnsson 28-Sep-16 15:02pm    
So what I could do is create what I posted above in a WinForm application instead and use it from my WPF application?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900