2
Vote

Error in .NET-package install using NuGet and PTVS 2.0

description

I am trying to create a MahApps styled WPF application using IronPython 2.7.3 in Visual Studio Ultimate 2012 with PTVS 2.0. However I cannot install MahApps.Metro via Nuget using the GUI. Choosing 'Install' in the Manage NuGet Packages console trows the following error by Visual Studio:

'MahApps.Metro' could not be installed because it is not compatible with any project in the solution. The package targets the following frameworks:

.NetFramwork, Verion=v4.0

.NetFramwork, Verion=v4.5


Trying out different .NET packages trough the NuGet Manager all trough the same errors.

I have created a new IronPython WPF application using both the .NET 4.0 and 4.5 Framework.

Any suggestions into how I could install mahapps.metro assemblies in an IronPython WPF application either using NuGet or an alternative way would be greatly appreciated.

(FYI: I made a C# WPF application using the same frameworks. Here I can install the MahApss assemblies as I should and they work perfectly fine there.)

comments

Zooba wrote May 23, 2013 at 3:29 PM

Unfortunately, when the core VS teams add a feature like this they don't work with us on it, so we usually have to catch up later.

I haven't tried, but this may be as easy as setting a property in the .pyproj file (for example, we probably don't set the target .NET framework version, even though you can select it in the New Project dialog).

You could open the .pyproj and .csproj files in a text editor and compare them - they're just XML files. We won't be exposing the property in the UI anywhere, so this is the only option. Editing project files is pretty safe though.

If you find a way to make it work let us know so we can tell other people and make any changes to our templates. (If you don't find a way then we'll have to triage this feature against all the rest - it probably won't make 2.0 final.)

JandalDoe wrote May 25, 2013 at 11:59 AM

Thank you for the reply Zooba I have found an alternative solution to this problem.

First I have tried your suggestion, yet unfortunately with no result. The project file generated from a IronPython WPF application *.pyproj structure does miss a reference to the .NET Framework wheras a the project file from a C# WPF application does not:
<?xml version...
<Project ToolsV...
    <Import Pro...
    <PropertyGroup>
        <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
    </PropertyGroup>
However adding this reference to the *.pyproj file did nothing for the NuGet installation error.

Manually adding packages without using NuGet for IronPython (WPF) applications (work-around)

So next I looked for an alternative way of installing the package without having to use NuGet at all:
  1. Install the NuGet package in a C# WPF project as per the instructions. For Mahapps: http://mahapps.com/MahApps.Metro/
  2. Next create a new IronPython WPF Application
  3. If you explore both projects in the explorer the C# project will have a folder in the solution folder of the project called 'packages' this folder contains the just installed NuGet packages and their .dll's. Copy this folder to the solution folder of the python project.
  4. Now you are able to reference the .dll's from the package (NuGet would normally do this automatically for you)
    In case of referencing MahApps set references to: MahApps.dll and System.Windows.Interactivity.dll in
    YourPoject\packages\MahApps.Metro.0.10.1.1\lib\net40
  5. Don't forget to set the search path to the same path in your project. (for clr.AddReference() to be able to find the assemblies)
  6. Now you can import the assemblies from the package in your IronPython script:
import wpf
import System.Windows
from System.Windows import Application

import clr
clr.AddReference("MahApps.Metro")
import MahApps.Metro.Controls
from MahApps.Metro.Controls import MetroWindow
  1. You can now follow the installation strategy on http://mahapps.com/MahApps.Metro/ The result will look something like this:
In the IronPython YourWindow.py script
import wpf
import System.Windows
from System.Windows import Application

import clr
clr.AddReference("MahApps.Metro")
import MahApps.Metro.Controls
from MahApps.Metro.Controls import MetroWindow

clr.AddReference("System.Windows.Interactivity")
import System.Windows.Interactivity

class MainWindow(MetroWindow):
    def __init__(self):
        wpf.LoadComponent(self, 'IPython_WPF.xaml')
    

if __name__ == '__main__':
    Application().Run(MainWindow())
In the YourWindow.xaml script
<Controls:MetroWindow 
       xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
       Title="IPython_WPF" Height="300" Width="300"> 
       <Grid></Grid>
</Controls:MetroWindow> 
It's quite the elaborate work-around but it works! I guess it would be interesting if someone would able to find out why exactly the NuGet installation doesn't detect the proper .NET framework for IronPython scripts. I am pretty sure you are not able to change anything in your templates to automate this process (I have tried to install all .NET 4.0 assemblies in the IronPython WPF application that are automatically present in the C# WPF application on start up but also then NuGet trows the same error).

I guess for the time being this work-around should suffice.

Zooba wrote May 25, 2013 at 3:54 PM

Thanks for the great write up!

If you're really keen to figure out how NuGet is validating the project you can actually look at their source code: https://nuget.codeplex.com/ This is all we'll be doing (eventually... which is why you may want to do it sooner :) )