3

为了便于开发,我使用 ViewBox 将所有内容包装在 Window 中。这是因为我的开发机器的屏幕比部署机器的屏幕小,所以使用 ViewBox 可以更好地实现比例。显然,它没有理由出现在代码的发布版本中。是否有一种简单的方法可以有条件地在 XAML 中包含/排除“包装”ViewBox?

例如

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

1 回答 1

3

在可访问的资源字典中创建两个控件模板。

他们应该看起来像这样

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

然后你可以在你的主视图中使用它

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

然后来回切换只需将StaticResource绑定中的查找键从“debug_view”更改为“release_view”

如果你想让它更有活力,你还可以这样做:

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

然后在你的代码隐藏中

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
}

这种方式取决于是否定义了 DEBUG 符号,将选择不同的模板。

于 2010-05-25T23:18:17.587 回答