简体   繁体   中英

Silverlight 3 and Silverlight 4 conditional xaml

I am working on a project right now that has to separate project files. One complies in Silverlight 3 and the other in Silverlight 4. The project creates a user control that is used in a couple other projects. Unfortunately one is in SL3 and cant be upgraded right now which is why a SL3 version of the control is needed.

So far it has worked out fine but recently I tried to add a ViewBox to the control. This causes a problem because in SL3 it is located in the toolbox and in SL4 it is in the core.

Is there any way to have it pull from the toolkit in the SL3 project and the core in the SL4 project? I looked into preprocessor definitions in xaml and found some info but I don't like how it kills the ability to load the code in the designer.

What I ended up doing is creating a border around the inner viewbox elements. Then I got rid of the viewbox in xaml. When I load the control I set the child of the containing border to null, create a viewbox in code, and set its child to the inner border. Then I set the child of the containing border to the viewbox.

<Border Name="viewBoxContainer">
    <Border x:Name="innerBorder">
        <TextBlock x:Name="innerText" Text="Test" />
    </Border>
</Border>

Code Behind

this.viewBoxContainer.Child = null;
this.viewBoxContainer.Child = new Viewbox { Child = this.innerBorder};

No, there is no support for conditional complication within XAML. How about creating your own subclass within each project:

within the SL3 project:

using //namespace for SL3 ViewBox

namepsace MyProjectNamespace
{    
    public class MyViewBox : ViewBox
    {
    }    
}

within the SL4 project:

using //namespace for SL4 ViewBox

namepsace MyProjectNamespace
{    
    public class MyViewBox : ViewBox
    {
    }    
}

within your common XAML file:

<UserControl ...
      xmlns:local="clr-namespace:MyProjectNamespace">
  <local:MyViewBox>
    .. content goes here ..
  </local:MyViewBox>
</UserControl>

We had the same problem, and our solution was to make our own viewbox that was used across the board.

Bascially, we just took the source code for Viewbox provided in the SL3 toolkit, copied it and put it in our own namespace, then renamed it to something like MyViewbox.

I'm not sure this is the best solution, but it worked out well for us.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM