question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

To reduce load time or to increase layout performance one may need to conditionally detach layout controls from visual tree. For example, when layout part loads images from the Net, but a user has disabled image loading in the application settings, XAML layout does not need to load the view part responsible for image showing.

In Angular, there is a NgIf directive that solves this issue. So please, take a look: https://angular.io/api/common/NgIf

So, maybe Avalonia framework should have such control in its standard library? Something named AvalonIf that we can use in our application like this:

<AvalonIf Value="{Binding ShouldLoad, Mode=OneWay}">
    <Image Source="{Binding Image, Mode=OneTime}"/>
</AvalonIf>

Thanks!

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:7
  • Comments:21 (20 by maintainers)

github_iconTop GitHub Comments

5reactions
maxkatz6commented, Mar 12, 2018

UWP has build in x:DeferLoadStrategy attribute for lazy loading and x:Load attribute (Creators Update+) for loading and unloading with binding, which can replace UwpIf/AvaloniaIf

<Grid>
    <Image x:Name="MyImage" 
           x:DeferLoadStrategy="Lazy" 
           Source="{Binding Image, Mode=OneTime}"
           Visibility="Collapsed"/>
    <VisualStateManager.VisualStateGroups>
      <VisualStateGroup>
        <VisualState>
          <VisualState.StateTriggers>
            <StateTrigger IsActive="{Binding ShouldLoad, Mode=OneWay}"/>
          </VisualState.StateTriggers>
          <VisualState.Setters>
            <Setter Target="MyImage.Visibility"
                    Value="Visible"/>
          </VisualState.Setters>
        </VisualState>
    </VisualStateManager.VisualStateGroups>
</Grid>

Or

    <Image x:Load="{Binding ShouldLoad, Mode=OneWay}" 
           Source="{Binding Image, Mode=OneTime}"/>
3reactions
maxkatz6commented, Nov 22, 2022

@robloo, yes, it does.

<Panel>
   <OnPlatform>
       <OnPlatform.Default>
            <DataGrid />
       </OnPlatform.Default>
       <OnPlatform.Android>
            <ListBox />
       </OnPlatform.Android>
   </OnPlatform>
</Panel>

It will be translated into IL code like: Panel.Children.Add(IsOnAndroid() ? new ListBox() : new DataGrid()) which means only one “branch” will be executed and intialized.

This, or users can use OnFormFactor which has Mobile or Default options, which is closer to what you asked.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Conditional XAML - UWP applications
It selectively parses elements or attributes to determine whether they will be available at runtime. Conditional statements are evaluated at ...
Read more >
c# - Conditional XAML (WPF)
I am trying to create a User Control that, depending on the mode the user sets in the Dependency Property, changes the User...
Read more >
Conditionals in XAML - Patrik Svensson
Ever wanted to display things conditionally in XAML based on a pre-processor directive like DEBUG ? Start by adding the following to your ......
Read more >
Conditional XAML in UWP - YouTube
This tutorial explain about how to use the Conditional XAML concept in UWP.
Read more >
Conditional XAML and x:Bind
Conditional XAML provides a way to use the ApiInformation.IsApiContractPresent method in XAML markup. This lets you set properties and ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found