Jump to: navigation, search

Difference between revisions of "XAML Tips"


(Disable FilloutCriteria)
Line 179: Line 179:
 
   <Interactivity:Interaction.Behaviors>
 
   <Interactivity:Interaction.Behaviors>
 
         <Core:EventTriggerBehavior EventName="Tapped">
 
         <Core:EventTriggerBehavior EventName="Tapped">
                            <Core:InvokeCommandAction Command="{Binding ElementName=ChildAreaGrid, Path=DataContext.AddTemplatableDataCommand}" >
+
                <Core:InvokeCommandAction Command="{Binding ElementName=ChildAreaGrid, Path=DataContext.AddTemplatableDataCommand}" >
                                <Core:InvokeCommandAction.CommandParameter>
+
                    <Core:InvokeCommandAction.CommandParameter>
                                    <uc:KeyValueList>
+
                        <uc:KeyValueList>
                                        <uc:KeyValueParameter Key="Uid" Value="{Binding Tag,ElementName=selectionGrid}"/>
+
                            <uc:KeyValueParameter Key="Uid" Value="{Binding Tag,ElementName=selectionGrid}"/>
                                        <uc:KeyValueParameter Key="EnableFillOutCriteria" Value="false"/>
+
                            <uc:KeyValueParameter Key="EnableFillOutCriteria" Value="false"/>
                                        <uc:KeyValueParameter Key="SkipDialog" Value="false"/>
+
                            <uc:KeyValueParameter Key="SkipDialog" Value="false"/>
                                    </uc:KeyValueList>
+
                        </uc:KeyValueList>
                                </Core:InvokeCommandAction.CommandParameter>
+
                    </Core:InvokeCommandAction.CommandParameter>
                            </Core:InvokeCommandAction>
+
              </Core:InvokeCommandAction>
 
         </Core:EventTriggerBehavior>
 
         </Core:EventTriggerBehavior>
 
   </Interactivity:Interaction.Behaviors>
 
   </Interactivity:Interaction.Behaviors>
Line 193: Line 193:
 
</source>
 
</source>
  
 +
[[Category:Client|XAML Tips]]
 +
[[Category:WinX|XAML Tips]]
 +
[[Category:XAML|XAML Tips]]
 +
[[Category:Xamarin|XAML Tips]]
  
 
=== Hotspotting ===
 
=== Hotspotting ===

Revision as of 08:49, 23 July 2021

Functionality related

Attachable behaviors

It's quite often that you need to attach behaviors to certain XAML elements. For example, on a Grid, you want to attach a behavior which executes a command upon a Tapped event, or you want to execute a command when a certain property on a UBIK object changes.

Notice that in the following examples, "Interactivity" and "Core" are both namespaces and you have to make sure that they are defined at the root of your XAMLs:

<DataTemplate
   ...
   xmlns:Core="using:Microsoft.Xaml.Interactions.Core"
   xmlns:Interactivity="using:Microsoft.Xaml.Interactivity">
    ...
</DataTemplate>

Event Triggered

With an EventTriggerBehavior, you can react on changes/events of UI Elements:

<Grid>
    <Interactivity:Interaction.Behaviors>
        <Core:EventTriggerBehavior EventName="Tapped">
            <Core:InvokeCommandAction Command="{Binding NavigateToChildrenCommand}" />
        </Core:EventTriggerBehavior>
    </Interactivity:Interaction.Behaviors>
</Grid>

Data Triggered

If you want to react on changes of the underlying data (ViewModel), you can use DataTriggerBehavior instead. The following example, when used in the UBIKSplashArea template, automatically navigates to the root objects once the login process is finished and the user was successfully authenticated:

<Grid>
   <Interactivity:Interaction.Behaviors>
       <Core:DataTriggerBehavior Binding="{Binding IsLoggedIn}" Value="True">
           <Core:InvokeCommandAction Command="{Binding NavigateToRootPageCommand}" />
       </Core:DataTriggerBehavior>
    </Interactivity:Interaction.Behaviors>
</Grid>


Fit mode

If you are using the standard app without XAML customizings, the fit mode feature should work out of the box. However, when using a FlipView for displaying documents in your customized XAMLs, you must make sure to include the following binding to the FlipView's ItemTemplate so that the fit modes are properly applied.

<DataTemplate
   ...
   xmlns:hs="using:UBIK.WinX.HotSpotting.Document">
    ...
    <Grid.Resources>
        ...
        <DataTemplate x:Key="FlipDocItemTemplate">
            ...
            <hs:Document
               ...
               FitMode="{Binding DocumentViewModel.FitMode}" />
        </DataTemplate>
    </Grid.Resources>
</DataTemplate>

Content creation

To directly create an object on a child of the current object, you can define a Button as follows. The method "Item.IsTypeCreationAllowed" used in the expression gets the uid of the type that should be created below a child, if a child does not allow the creation of that type underneath it, the child will be hidden in the selection dialog. To actually create the object, the "CreateChildItemCommand" needs to be passed a KeyValueList with two parameters: The Parent-key is the UID or the ContentViewModel of the child underneath the object should be created, the Type-key is the type of object which should be created--this should match the uid passed to the "Item.IsTypeCreationAllowed" method.

    xmlns:uc="using:UBIK.WinX.Controls"
    xmlns:cv="using:UBIK.WinX.UI.CollectionView"
<x:String x:Key="PlantMap">Item.IsTypeCreationAllowed(&quot;21fc990a-d064-4bee-8d48-3293351f827a&quot;)</x:String>
<cv:ListCollectionView x:Key="PlantMapView" Expression="{StaticResource PlantMap}" ItemsSource="{Binding Children.Items}" />

<AppBarButton>
<AppBarButton.Flyout>
  <Flyout Placement="Full">
        <ListView ItemsSource="{Binding Source={StaticResource PlantMapView}}">
          <ListView.ItemTemplate>
                <DataTemplate>
                <Button Content="{Binding Header}" Command="{Binding CreateChildItemCommand}" x:Name="CreateButton" Tag="{Binding}">
                        <Button.CommandParameter>
                          <uc:KeyValueList>
                                <uc:KeyValueParameter Key="Parent" Value="6D733909-1742-4110-8619-237849BFE453"/>
                                <uc:KeyValueParameter Key="Type" Value="21fc990a-d064-4bee-8d48-3293351f827a"/>
                          </uc:KeyValueList>
                        </Button.CommandParameter>
                  </Button>
                </DataTemplate>
          </ListView.ItemTemplate>
        </ListView>
  </Flyout>
</AppBarButton.Flyout>
</AppBarButton>
<x:String x:Key="PlantMap">Item.IsTypeCreationAllowed(&quot;21fc990a-d064-4bee-8d48-3293351f827a&quot;)</x:String>
<cv:ListCollectionView x:Key="PlantMapView" Expression="{StaticResource PlantMap}" ItemsSource="{Binding Children.Items}" />

<AppBarButton>
<AppBarButton.Flyout>
  <Flyout Placement="Full">
        <ListView ItemsSource="{Binding Source={StaticResource PlantMapView}}">
          <ListView.ItemTemplate>
                <DataTemplate>
                <Button Content="{Binding Header}" Command="{Binding CreateChildItemCommand}" x:Name="CreateButton" Tag="{Binding}">
                        <Button.CommandParameter>
                          <uc:KeyValueList>
                                <uc:KeyValueParameter Key="Parent" Value="{Binding Tag, ElementName=CreateButton}"/>
                                <uc:KeyValueParameter Key="Type" Value="21fc990a-d064-4bee-8d48-3293351f827a"/>
                          </uc:KeyValueList>
                        </Button.CommandParameter>
                  </Button>
                </DataTemplate>
          </ListView.ItemTemplate>
        </ListView>
  </Flyout>
</AppBarButton.Flyout>
</AppBarButton>


Additionally, the following optional parameters can be added as well.

  • CreateOnly (optional, defaults to false): When set to true, the client will not automatically navigate to the created content, rather automatically save and commit it. If set to true, this overrides the following parameters;
  • AutoNavigate (optional, defaults to true): When set to false, the client will not automatically navigate to the created content;
  • AutoCommit (optional, defaults to false): When set to true, the change(s) will be saved to the local cache and the database, and then committed to the server.

Creating multiple documents

To upload multiple documents at once, the CreateChildItemsCommand can be used. The list of supported command parameters are similar to those of the CreateChildItemCommand (single item). Except that anything other than AutoNavigate=false and AutoCommit=true do not make sense in multi-creation scenario. Therefore, those parameters are fixed and any received from XAML will be ignored.

<Button ...
   xmlns:uc="using:UBIK.WinX.Controls"
   Command="{Binding CreateChildItemsCommand}"
   Content="Create multiple documents">
    <Button.CommandParameter>
        <uc:KeyValueList>
            <uc:KeyValueParameter Key="Type" Value="6170a068-2314-4444-ad62-0da99769a048" />
        </uc:KeyValueList>
    </Button.CommandParameter>
</Button>



Disable FilloutCriteria

To enable/disable the automatic filtering of a query based on the ParentObject, there is the possibility to specify EnableFillOutCriteria--if it is not set, it defaults to false. Additionaly "SkipDialog" can be set to true, to not display a dialog.

<Grid x:Name="selectionGrid" Tag="{Binding MetaUID}">
  <Interactivity:Interaction.Behaviors>
         <Core:EventTriggerBehavior EventName="Tapped">
                <Core:InvokeCommandAction Command="{Binding ElementName=ChildAreaGrid, Path=DataContext.AddTemplatableDataCommand}" >
                    <Core:InvokeCommandAction.CommandParameter>
                        <uc:KeyValueList>
                            <uc:KeyValueParameter Key="Uid" Value="{Binding Tag,ElementName=selectionGrid}"/>
                            <uc:KeyValueParameter Key="EnableFillOutCriteria" Value="false"/>
                            <uc:KeyValueParameter Key="SkipDialog" Value="false"/>
                        </uc:KeyValueList>
                    </Core:InvokeCommandAction.CommandParameter>
               </Core:InvokeCommandAction>
         </Core:EventTriggerBehavior>
  </Interactivity:Interaction.Behaviors>
</Grid>

Hotspotting

IC Attention.pngTo use a binding in the KeyValueParameter, it has to be applied like in the example provided here: KeyValueList

The hotspotting command is used for hotspotting as well as for annotating, to configure the button for hotspotting, the commandparameter "Mode" should be set to "HotSpotting", for annotating the "Mode" should be "Annotate". The parameter commit is optional, if set to true, the changes get automatically persisted when leaving the editing mode.

<AppBarToggleButton
                IsChecked="{Binding EditingAnnotation, Mode=TwoWay}"
                IsEnabled="{Binding IsAnnotatable}"
                Command="{Binding HotSpottingCommand}">
                <AppBarToggleButton.CommandParameter>
                        <uc:KeyValueList>
                                <uc:KeyValueParameter Key="Mode" Value="Annotate"/>
                                <uc:KeyValueParameter Key="Commit" Value="true"/>
                        </uc:KeyValueList>
                </AppBarToggleButton.CommandParameter>
</AppBarToggleButton>



Remember scroll positions of list views

Version 3.7 & later

Starting from this version,

  • The precision of scroll position remembering is improved(by pixel offsets instead of by items);
  • It also works for other scrollable lists (instead of just for content object lists).


To enable this feature, you should make sure the following.

  • The SelectionBoundListView is used instead of the standard ListView. Its UBIK® namespace is UBIK.WinX.Controls;
  • The SelectionBoundListView's RememberScrollPosition property is not set to "false"; (It's "true" by default.)
  • The SelectionBoundListView's x:Name property value is unique.



Version 3.6

The UBIK-Client does include a function to remember the position in a list (ListView) when navigating away from it. This function is only available when the list (ListView) has a unique name as a property (x:Name). When browsing back to the previously visited list UBIK scrolls back to the last position. The function does not save scroll positions over different sessions. Implementing the function to remember the scroll position in a ListView one has to consider that the list elements (Children) could depend on a other UI-element. If the list elements do depend on a other UI-elemente, this element has to be created above the ListView in the XAML.

<DataTemplate xmlns:behaviors="using:UBIK.WinX.Behaviors" xmlns:uc="using:UBIK.WinX.Controls">
  ...
     <uc:SelectionBoundListView x:Name="ChildListView">
        <Interactivity:Interaction.Behaviors>
           <behaviors:FirstVisibleItemPersistenceBehavior FirstVisibleItems="{Binding ScrollItems}" />
        </Interactivity:Interaction.Behaviors>
     </uc:SelectionBoundListView>
</DataTemplate>



MultiBinding

Very often we want to display some UI elements (e.g. a Grid) depending on whether multiple criteria are met. It's much easier to achieve this by using a MultiBindingBehavior like the following.

<DataTemplate
   xmlns:Interactivity="using:Microsoft.Xaml.Interactivity"
   xmlns:behaviors="using:UBIK.WinX.Behaviors">
    ...
    <Grid>
        <Interactivity:Interaction.Behaviors>
            <behaviors:MultiBindingBehavior Converter="{StaticResource VisLogicAndConverter}" PropertyName="Visibility">
                <behaviors:MultiBindingItem Value="{Binding MassEditViewModel, Converter={StaticResource NullToVisConverter}}" />
                <behaviors:MultiBindingItem Value="{Binding Documents.Items.Count, Converter={StaticResource ItemCountToVisConverter}}" />
            </behaviors:MultiBindingBehavior>
        </Interactivity:Interaction.Behaviors>
    </Grid>
</DataTemplate>

The behavior makes sure the container Grid is set to Visibile only if the mass editing mode is not turned on (MassEditViewModel is null) and the context object has child document(s) (Documents.Items.Count is greater than 0. You can combine any number of binding results (MultiBindingItem) using the VisLogicAndConverter (the name should be self explanatory).

InvokeOnItemsCommand

Available on all ListViewModels, this command allows executing a specified command on a collection of list items. It can be used in combination with features such as mass editing and expression based collection filtering. Examples for both combinations are provided below.

IC Attention.pngThe command specified through the "Command" parameter is executed on list items and, therefore, must be available in the list item contexts (view models). If in doubt, the developer mode can be used to inspect if a command is available in a certain context.
IC Hint square.pngParameter "Command" and "SelectedItemsOnly" are specific to the InvokeOnItemsCommand. What other parameters to define or whether to define them at all depends on the type of command to be executed on the items.

Invoke on selected items

IC Attention.pngTo use a binding in the KeyValueParameter, it has to be applied like in the example provided here: KeyValueList

This example demonstrates how you can use the mass editing feature to select certain objects from the child list and then execute the SetPropertyValueCommand for those selected.

  • The example code assumes that the child objects have an editable property called "VALUE" and tries to set 50 as their value;
  • You should insert the following code snippet into the default UBIKChildArea template;
  • If the parameter "SelectedItemsOnly" is missed or set to "False", the command will be executed on all child items;
  • To enable selection, click on the "Mass Edit" button below the property list.


  <AppBarButton
     xmlns:example="using:UBIK.WinX.Controls"
     Command="{Binding Children.InvokeOnItemsCommand}"
     Icon="AllApps"
     Label="Set to 50%"
     Style="{ThemeResource UBIKActionAppBarButton}">
      <AppBarButton.CommandParameter>
          <example:KeyValueList>
              <example:KeyValueParameter Key="Command" Value="SetPropertyValueCommand" />
              <example:KeyValueParameter Key="SelectedItemsOnly" Value="True" />
              <example:KeyValueParameter Key="PropertyName" Value="VALUE" />
              <example:KeyValueParameter Key="PropertyValue" Value="50" />
          </example:KeyValueList>
      </AppBarButton.CommandParameter>
  </AppBarButton>



Invoke on filtered results

IC Attention.pngTo use a binding in the KeyValueParameter, it has to be applied like in the example provided here: KeyValueList

First, you need to setup a filtered list (ListCollectionView) in the Resources section of a UI element (e.g. Grid).

  • This list is only available/visible within that UI element (the Grid in this case);
  • The ItemsSource uses Children.Items. Use the developer mode if necessary to find out if this is available where you intend to define the list;
  • The example expression filters for any items that don't contain the text "EXAMPLE" in their Title texts. You can filter differently by altering the expression.


    <Grid xmlns:CV="using:UBIK.WinX.UI.CollectionView">
        <Grid.Resources>
            <CV:ListCollectionView
               x:Key="Filtered"
               Expression="!Item.Title.Contains(&quot;EXAMPLE&quot;)"
               ItemsSource="{Binding Children.Items}" />
        </Grid.Resources>
       ...
    </Grid>


With the filtered list configured, you can then insert the following code snippet to execute the SetPropertyValueCommand for the filtered result items.

  • The example code assumes that the child objects have an editable property called "VALUE" and tries to set 50 as their value;
  • The "Filtered" refers to the ListCollectionView configured above.


  <AppBarButton
     xmlns:example="using:UBIK.WinX.Controls"
     Command="{Binding Source={StaticResource Filtered}, Path=ListViewModel.InvokeOnItemsCommand}"
     Icon="AllApps"
     Label="Set to 50"
     Style="{ThemeResource UBIKActionAppBarButton}">
      <AppBarButton.CommandParameter>
          <example:KeyValueList>
              <example:KeyValueParameter Key="Command" Value="SetPropertyValueCommand" />
              <example:KeyValueParameter Key="PropertyName" Value="VALUE" />
              <example:KeyValueParameter Key="PropertyValue" Value="50" />
          </example:KeyValueList>
      </AppBarButton.CommandParameter>
  </AppBarButton>



Support for old styled commands

Some old commands might not support KeyValueLists as parameters. In that case, just define the parameter value under the "CommandParameter" key, e.g.

<example:KeyValueParameter Key="CommandParameter" Value="some string value for example" />
.

This single value is then passed as the command parameter instead of the entire KeyValueList.



SetPropertyValueCommand

IC Attention.pngTo use a binding in the KeyValueParameter, it has to be applied like in the example provided here: KeyValueList

This command existed before but was implemented differently. In the newer version(s), it is improved to provide customizers more control over the things that happen during/after the property value changes. The available commnad parameters are:

  • PropertyName: the name of the property to set a new value to;
  • PropertyValue: the value to be set to the above mentioned property;
  • OnlyForUnvalidated (optional, defaults to false): When set to true, the value will only be set if the property is not yet validated;
  • AutoSave (optional, defaults to false): When set to true, the change(s) will be saved to the local cache and database;
  • AutoCommit (optional, defaults to false): When set to true, the change(s) will be committed to the server.
IC Hint square.pngThere's no way to commit changes without saving them locally first. Therefore, the "AutoSave" parameter will be ignored when "AutoCommit" is set to true.

Here's an example of the command usage. It tries to set the property called "VALUE" to a double value 50 regardless of its current state and then automatically save and commit the change.

  <AppBarButton
     xmlns:example="using:UBIK.WinX.Controls"
     Command="{Binding SetPropertyValueCommand}"
     Icon="Edit"
     Label="Set to 50%"
     Style="{ThemeResource UBIKActionAppBarButton}">
      <AppBarButton.CommandParameter>
          <example:KeyValueList>
              <example:KeyValueParameter Key="PropertyName" Value="VALUE" />
              <example:KeyValueParameter Key="PropertyValue">
                  <example:KeyValueParameter.Value>
                      <x:Double>50</x:Double>
                  </example:KeyValueParameter.Value>
              </example:KeyValueParameter>
              <example:KeyValueParameter Key="OnlyForUnvalidated" Value="false" />
              <example:KeyValueParameter Key="AutoSave" Value="true" />
              <example:KeyValueParameter Key="AutoCommit" Value="true" />
          </example:KeyValueList>
      </AppBarButton.CommandParameter>
  </AppBarButton>
IC Hint square.pngIt is advised to provide typed values like <x:Double>50</x:Double>. But for simple types, you can try writing them in the text format like <example:KeyValueParameter Key="PropertyValue" Value="50" /> and UBIK® will try to find the right type.


DisplayViewCommand

This command can be used to display cutom views.


Performance related

FlipView

When using the FlipView control in your XAML code, it's better to enable UI virtualization. The difference in performance gets more obvious as the number of items in the FlipView increases. Here's how to enable it.

<FlipView
   ...
   VirtualizingStackPanel.VirtualizationMode="Standard">
    <Flipview.ItemsPanel>
        <ItemsPanelTemplate>
            <VirtualizingStackPanel Orientation="Horizontal" />
        </ItemsPanelTemplate>
    </Flipview.ItemsPanel>
</FlipView>


VirtualizingStackPanel.VirtualizationMode offers two possibilities: Standard & Recycling. In case you are interested, here are their differences.