How to not flicker in screen transitions in Livet

Asked 2 years ago, Updated 2 years ago, 114 views

The screen transition is done with the following coding, but it will be done after window.Hide(), so
In order to hide the current window, it becomes empty and the transition destination window opens.
This causes flickering.
To avoid flickering window.Hide() after the screen transition (after Messenger.Raise()),
Because the control has not been returned, the window from which the transition source window will also remain.
Please tell me how to transition the screen without any windows left.

// Get the current View instance
var window=Application.Current.Windows.OfType<Window>().SingleOrDefault(w)=>w.IsActive);
try
{
       // Hide Current View
       window.Hide();
       // Send a message to View - launch child screen in modal mode
      Messenger.Raise(newTransitionMessage(new) 
                 ViewModel 2(), ShowCommand 2");   
}
finally
{
      // Redisplay View
      window.ShowDialog();
}

c# wpf

2022-09-30 16:17

1 Answers

Do you mean that you want to transition to a single window?

In that case, you can use ContentControl to change the View according to the data type set in DataContext.

Below is a sample of ContentControl's View changes in conjunction with Window.DataContext.

MainWindow.Xaml Content Part

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <StackPanel Grid.Row="0" Orientation="Horizontal">
        <Button Content="ViewModel1" Click="Button1_Click"/>
        <Button Content="ViewModel2" Click="Button2_Click" Margin="10,0,0,0" /gt;
    </ StackPanel>

    <!--- Synchronize Window.DataContext with ContentControl.DataContext-->
    <ContentControl Grid.Row="1" Content="{Binding}">
        <ContentControl.Resources>
            <!--View definition when the contents are ViewModel1-->
            <DataTemplateDataType="{x:Typevm:ViewModel1}">
                <Label Content="ViewModel 1"Background="PaleGreen"/>
            </DataTemplate>
            <!--View definition when the contents are ViewModel 2 -->
            <DataTemplateDataType="{x:Typevm:ViewModel2}">
                <Label Content="ViewModel2"Background="PaleVioletRed"/>
            </DataTemplate>
        </ContentControl.Resources>
    </ContentControl>
</Grid>

MainWindow.Xaml.cs

public partial class MainWindow:Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void Button1_Click(object sender, RoutedEventArgse)
    {
        DataContext=newViewModel1();
    }

    private void Button2_Click(object sender, RoutedEventArgse)
    {
        DataContext=newViewModel2();
    }
}


2022-09-30 16:17

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.