简体   繁体   English

条件XAML

[英]Conditional XAML

For easy of development I'm using a ViewBox to wrap all content inside a Window. 为了便于开发,我使用ViewBox将所有内容包装在Window中。 This is because my development machine has a smaller screen than the deployment machine so using a ViewBox allows for better realisation of proportion. 这是因为我的开发机器比部署机器具有更小的屏幕,因此使用ViewBox可以更好地实现比例。 Obviously there is no reason for it to be there on Release versions of the code. 显然,它没有理由在Release版本的代码上出现。 Is there an easy method to conditionally include/exclude that 'wrapping' ViewBox in XAML? 是否有一种简单的方法来有条件地包含/排除在XAML中“包装”ViewBox?

Eg 例如

<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' 然后来回切换只需将StaticResource Binding中的查找键从'debug_view'更改为'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. 这种方式取决于是否定义了DEBUG符号,将选择不同的模板。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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