条件 XAML

发布于 2024-09-03 03:31:13 字数 281 浏览 1 评论 0原文

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

例如

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

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 proportion. 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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

瑾兮 2024-09-10 03:31:13

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

它们应该看起来像这样

<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 符号,将选择不同的模板。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文