locked
Best way to share code between Silverlight and WPF RRS feed

  • Question

  •  Hey!

     I'm aware of the fact that similar things been discussed here before, but all of those discussions are pretty old.

    I have a Silverlight and a WPF application, both are very similar and share much of the code base.

    Shared the common code in a Silverlight class library, worked fine for a while but then I suddenly began to receive really strange errors in the WPF app (like: System.Collections.Generic.LinkedList`1<T0> wasn't found and VS told me to add reference to "System, Version=2.0.5.0", no matter what I removed and readded, nothing helped). Back then I was in a hurry so I solved the problem with the first feasible solution found, removed the reference to the SL class library from the WPF app, and added all the classes in the class library as links.

    While this method can work, it really is a pain after a while to add new links every time I create a new class in the library, plus when a class accidently opens in the WPF project (all 3 projects are hold in the same solution), it really messes up intellisense, and who knows what other mess it will cause.

    Is there yet any better method to be able to use a common (silverlight) class library between silverlight app and WPF app? Why are the strange errors? WPF has everything silverlight does, doesn't it?

     

    Thank you

    Friday, October 31, 2008 12:03 PM

Answers

  •  The reason for the errors is that silverlight and wpf use different versions of the .net framework.  While the code is will work with both frameworks it has to be compiled with the right version of the framework to use.

    Friday, October 31, 2008 12:09 PM

All replies

  •  The reason for the errors is that silverlight and wpf use different versions of the .net framework.  While the code is will work with both frameworks it has to be compiled with the right version of the framework to use.

    Friday, October 31, 2008 12:09 PM
  • Is there yet any better method to be able to use a common (silverlight) class library between silverlight app and WPF app?

     

     

    Herthoren,

    you need to create two projects: one for Silverlight and one for WPF. Each project has references to different assemblies (Silverlight or WPF), but can share source files.

    Patrick

    Friday, October 31, 2008 3:36 PM
  • Top tip for sharing source files; when Adding Existing Item, flick down the Add button and add as a link. Then you can make use of the new conditional source code if block:

    #if !SILVERLIGHT

    // Non Silverlight code goes here, life is good again...!!

    #endif

    Thursday, November 13, 2008 1:24 PM
  •  Well, my library needs silverlight code only, that's why it would be very useful and conventional to be able to just reference it like a dll in the WPF project.

    I'm having references to the code files in the WPF app now, and the IDE has several errors using this approach, I constantly have to watch out actually which project I'm having the class files open from (both project are in the same solution). Not very serious issues, but reduces efficiency for sure.

    Thursday, November 13, 2008 3:53 PM
  • Top tip for sharing source files; when Adding Existing Item, flick down the Add button and add as a link.

     

    This is definitely the TOP tip for sharing source files. I used to have two copies of all shared files and felt a lot pain in working with vss. Just tested adding link while working with vss and found working well. Visual Studio automatically check out/in two "files" for you.

    Thanks for the tip! 

    Thursday, November 13, 2008 5:55 PM
  • Also Generics come in very handy. If you have a common base class but are deriving from it in Silverlight and Full-fat applications and want to return a different type depending upon the client CLR, make the base class method generic. 

    // In Base.cs
    //
    public abstract T BuildCarForSilverlightOrFullfat<T>() where T : new();
    ...
    
    // In Derived.cs
    //
    #if !SILVERLIGHT
    
    public override T BuildCarForSilverlightOrFullfat<T>()
    {
    T generic = new T();
    FullCar fCar = generic as FullCar;
    fCar.RegistrationPlate = "H1 BCL"
    return generic;
    }
    
    #else
    
    public override T BuildCarForSilverlightOrFullfat<T>()
    {
    T generic = new T();
    SilverCar sCar = generic as SilverCar;
    sCar.RegistrationPlate = "SL2"
    return generic;
    }
    
    #endif
    
    ..
    
    // In consuming class:
    //
    ...
    Derived d = new Derived();
    SilverCar car = d.BuildCarForSilverlightOrFullfat<SilverCar>();
    ...
    // or
    //
    FullCar car = d.BuildCarForSilverlightOrFullfat<FullCar>();
    
    
     
    Friday, November 14, 2008 12:56 PM
  •  

    i have silverlight class Library which it uses WCF service,and it has two Classes inherite form TextBox and ComboBox, which use that WCF Service.

    i can not AddLink because the method i used in silverlight for calling wcf is differnt from which is possible in Wpf.

    i also have another WPF Applicatio that i use this code :

    var assembly = Assembly.LoadFile(@"D:\SL.dll");
       var node = new AssemblyNode();
       node.Assembly = assembly;
       node.Path = @"D:\SL.dll";
       //node.Path = path;
       foreach (var t in assembly.GetExportedTypes())
       {
        //if (IsControl(t) && Metadata.IsPopularControl(t)) {
        if (IsControl(t))
        {
         node.Controls.Add(new ControlNode() { Type = t });
        }
       }
    
    ....
    
    


     

    at assembly.GetExportedTypes() i got error  :

    {"Could not load file or assembly 'System.Windows, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e' or one of its dependencies. The system cannot find the file specified.":"System.Windows, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e"}

     

    Controls could not add in my ToolBox

    is it possible to run out this error?

    thanks in advanced

    Monday, February 14, 2011 4:27 AM