Silverlight 3 and Silverlight 4 conditional xaml

江枫思渺然 提交于 2020-01-15 06:26:10

问题


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.


回答1:


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};



回答2:


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>



回答3:


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.



来源:https://stackoverflow.com/questions/6645368/silverlight-3-and-silverlight-4-conditional-xaml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!