开发者

Conditional XAML

For easy of development I'm using a ViewBox to wrap all content inside a Window. This is because my development machine has a smaller screen than the deployment machine so using a ViewBox allows for better realisation of p开发者_JAVA百科roportion. Obviously there is no reason for it to be there on Release versions of the code. Is there an easy method to conditionally include/exclude that 'wrapping' ViewBox in XAML?

E.g.

<Window>
  <Viewbox>
    <UserControl /*Content*/>
  </Viewbox>
</Window>


create two control templates in a resources dictionary somewhere accesible.

they should look like this

<ControlTemplate x:key="debug_view">
    <ViewBox>
        <ContentPresenter Content={Binding} />
    </ViewBox>
</ControlTemplate>
<ControlTemplate x:key="release_view">
    <ContentPresenter Content={Binding} />
</ControlTemplate>

then you could use this in your main view

<Window>
    <ContentControl Template="{StaticResource debug_view}">
        <UserControl /*Content*/ ...>
    </ContentControl>
</Window>

then to switch back and forth just change the lookup key in the StaticResource Binding from 'debug_view' to 'release_view'

if you wanted to make it more dynamic you could additionally do this:

<Window>
    <ContentControl Loaded="MainContentLoaded">
        <UserControl /*Content*/ ...>
    </ContentControl>
</Window>

then in your codebehind

void MainContentLoaded(object sender, RoutedEventArgs e)
{
    ContentControl cc = (ContentControl) sender;
#if DEBUG
    sender.Template = (ControlTemplate) Resources["debug_view"];
#else
    sender.Template = (ControlTemplate) Resources["release_view"];
#endif
}

this way depending on whether or not the DEBUG symbol is defined different templates will be selected.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜