Conditional XAML

To simplify development, I use ViewBox to transfer all content inside the window. This is due to the fact that my development machine has a smaller screen than the deployment machine, so using ViewBox allows me to better realize the proportions. Obviously, there is no reason for his presence in the release versions of the code version. Is there an easy way to conditionally include / exclude that the 'wrapping' ViewBox in XAML?

eg.

<Window>
  <Viewbox>
    <UserControl /*Content*/>
  </Viewbox>
</Window>
+3
source share
1 answer

create two control patterns in a resource dictionary somewhere accessible.

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 can use this in your main view

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

StaticResource Binding '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, .

+3

Source: https://habr.com/ru/post/1747099/


All Articles