How to compile a .NET assembly against Silverlight? - c#

I am trying to compile AvalonEdit for Silverlight, but I am not sure how to do it as I never worked with Silverlight, only WPF.
I tried to add this control to my Silverlight application but Visual Studio complained that the assembly wasn't compiled against Silverlight.
I thought it was gonna be a matter of picking Silverlight under the target platform in the project settings.
Any ideas?

Silverlight and WPF assemblies are not compatible. So any assembly you reference from your Silverlight assembly must also have been compiled as a Silverlight assembly. I'm assuming your reference to "project settings" is your assembly, not the third party assembly.
But, compiling WPF code as Silverlight may be challenging. Silverlight is a functional subset of WPF so the code may use features not available in Silverlight.

You need to create new project which targets silverlight (File - New Project - Silverlight) and then add all the source code from the AvalonEdit source code into that library.
Edit:
Also note that the control might use classes that are not available in Silverlight. From the codeplex discussion related to this control (from year 2009):
Silverlight does not have the System.Windows.Media.TextFormatting namespace, so you'd have to rewrite a large part of the ICSharpCode.AvalonEdit.Rendering namespace if you want to port AvalonEdit to Silverlight.

In silverlight projects you can't reference non-silverlight dll's or projects. So in this project if there is an essential non-silverlight dll for building this project. You cant do this build.
If there are just some usages wfp supports but silverlight doesnt support find out this differs.
Then exclude these codes or use pragma
#if !WPF
//Todo
#endif

Related

In Xamarin, Is it possible link to a .NET 4.0 assembly?

I don't have Xamarin yet, and i want to build some application both for android and windows, each will use same shared library.
Is there a possibility to link .NET 4.0 assembly to Xamarin ios/android project, without recompile?
This assembly reference to System only, uses SQLite-NET by praeclarum, which requires native Windows library sqlite3.dll.
As i heard, on Mono/Xamarin? Mono.Data.Sqlite is required instead sqlite3.dll, but i am not sure, can i keep my library "cross-platform" with it? Or i will have to change something?
Unfortunately, i cannot check this currently.
Thank you..
.net assemblies can't be linked to Xamarin Android/iOS but you can link PCLs or you can create a Xamarin Android/iOS project, link files from your .net assemblies there and resolve possible problems with conditional directives (#if...).

Cannot add "using System.Windows.Media.Imaging;" in WPF desktop application

I need to use the BitmapImage class which is part of the System.Windows.Media.Imaging namespace.
From Visual Studio 2012 Express, I started with a WPF desktop application project template, and I can see the relevant dll, PresentationCore.dll is present under the references.
This link on MSDN indicates this reference should be sufficient. This thread indicates the same, and highlights the fact that the necessary dll is referenced by default when using a typical wpf project template in VS2012.
And yet, in my C# code behind, the compiler complains on my using System.Windows.Media.Imaging; statement that Media cannot be found.
This problem seems to not be uncommon, and yet I found no solution to it.
UPDATE:
When looking at the properties for the reference to PresentationCore.dll, I see that version is 4.0.0.0 and runtime version is 4.0.30319. I am making a cheap relation to the fact that BitmapImage exists only for .net 4.5 supposedly... Could this be a hint to the problem ?
There are 3 required references need for a WPF project, PresentationCore, PresentationFramework and WindowsBase. See WPF Architecture and WPF Architecture - and overview ... . The second one has a nice description of the contents of the libraries.

Do i have to recompile a .dll in order to use it on a Mono Android Project?

I have recently acquired the student licence of Mono Android from Xamarin.
After making a simple project, i decided to start using some DLL's i am familiar with such as Json.Net and H.A.P on my Android Application (mono project), and that's when the problems started.
After trying to pull those references from NuGet, they failed since the "Android Application" project is not known leading to no .dll installed (NuGet tries to match the project type with the correct .dll to be downloaded be it WindowsPhone, .NET4, .NET 3.5 and so on).
So i moved on to mannually referencing the libraries, since i have them downloaded on my PC. Referencing Works, but as soon as i try to access the HtmlAgilityPack it raises an exception.
Error CS0012:
The type 'System.Xml.XPath.IXPathNavigable' is defined in an assembly that is not referenced.
You must add a reference to assembly 'System.Xml, Version=x.x.x.x, Culture=neutral
The exception is raised on the first execution of the code because of J.I.T compilation of .NET, i know this.
Question:
How can i use my "so loved" .dlls on my Mono Android Project?
Do i need the source code of those libraries in order to use them on my projects?
Thanks in Advance
In order to recompile an assembly you will need the source.
To see what assemblies are provided by Xamarin.Android and Xamarin.iOS you can visit these two pages:
http://docs.xamarin.com/guides/android/advanced_topics/assemblies
http://docs.xamarin.com/guides/ios/advanced_topics/assemblies
One way to recompile it would be to make a Android Class Library or iPhone Class Library and add a link to all the files in the original source and then compile it. Or when Xamarin have their PCL support done, do something similar just in a PCL so that you have one DLL targeting multiple platforms.
You may also find this post useful, which briefly describes how to compile HtmlAgilityPack against Xamarin.Android: http://forums.xamarin.com/discussion/comment/2139/#Comment_2139 You might need to alter some things as this is from December 2012, since then things might have changed.

Visual Studio 2012 How to debug "Unable to add reference to project x" error?

In Visual Studio 2012 in a blank new Class Library I am trying to add reference to an existing project in the solution, also a Class Library, MonoGame.Framework.Windows8 (https://github.com/mono/MonoGame), but get the error:
Unable to add reference to Project MonoGame.Framework.Windows8
I have built the existing library, MonoGame, successfully and made it a dependency of the new one. What could be the problem?
(It's extremely annoying the error message does not give a reason!)
Trying to add the built dll has a reference gave a more explicit error message which has solved the problem for me, it said:
“A reference to ‘x’ could not be added. The project targets ‘.NetFramework’ while the file reference targets ‘.NetCore’. This is not a supported scenario”
The MonoGame Class Library is a Class Library (Windows Store apps) type! (The bit in parenthesis is important! Presumably the difference is the Windows Store apps type use .Net Core which is not the same as .Net Framework).
So to get it working your project must be a Class Library (Windows Store apps) which is available under the Windows Store option in Add New Project.
I suspect the problem is to do with the 'Target Framework' in project options.
I don't know how MonoGame works for Windows 8 but when you're developing for Android the target framework options are the different versions of Android [e.g. Android 2.2 (Froyo)]. When you create a regular class library you have choices between the different versions of the .NET framework or Mono [e.g. Mono / .NET 4.0].
Unfortunately, these frameworks are not compatible with each other. You can't add a project reference for Android to a .NET framework class library and visa-versa.
The solution is to use a Portable Class Library. They are specifically designed to deal with this issue. The downside is that you will only have access to the subset of assemblies provided in the lowest denominator you choose to target.
You might also want to consider code sharing between projects using linked files. It can make maintenance a little trickier but gives you a little more control over code that should compile in both target frameworks but doesn't fit into the portable class library. Here's a related question:
Project reference vs. file links in Mono multi-target projects
The key to all of this once you understand what's going on here is how you structure your solution. With a little care you can have nearly all your code shared across different platforms.
I might be because of both projects are using different Framework versions and change the Framework versions in both Projects of properties section then your problem might solve.
Same question I guess here:
Visual Studio 11 metro apps unable to add reference?
The answer was:
What are you adding a reference to? Metro style apps can only add references to other Metro style class libraries or portable class libraries

F# Library in Silverlight Application?

I have an F# library in the same solution as a Silverlight (C#) project that I'd like to use the library in. When I try to add a reference to the library project, it says I can only add Silverlight references. How can I make this F# library compatible with my Silverlight project?
I'm using Visual Studio 2010.
You need to start with an 'F# Silverlight Library' rather than 'F# library' project template.
The F# compiler automatically generates assembly for some non-standard runtime (such as Silverlight or Compact Framework) if you reference the appropriate (non-standard) version of the mscorlib.dll and FSharp.Core.dll assemblies in your F# project. The Silverlight version of the assemblies can be found here (on my machine):
C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\Silverlight\v3.0\mscorlib.dll
C:\Programs\Development\FSharp-1.9.9.9\Silverlight\2.0\bin\FSharp.Core.dll
This is a more user-friendly replacement for the --noframework and --cli-root parameters mentioned in the question referenced by Pretzel.

Categories