Creating a WPF user interface on the desktop?

I need to create a pretty sophisticated WPF interface (hughe mesh) in code (no xaml invlued). Is there a way to stop the main UI thread from blocking during the creation of the Grid? Are there some parts of building a user interface that can be passed on to third-party workers? What parts of the user interface creation really should be in the user interface thread?

  • Designer Call Controls?
  • Setting control properties?
  • Setting data binding?
  • Building a logical tree (adding children)?

What other options to improve performance when creating a user interface? Suspends the dispatcher or calls FrameworkElement.BeginInit a good idea?

+3
source share
3 answers

If you can break down your user interface design into steps, you can complete each of these steps as a separate message. This will allow other messages to be processed between them. Sort of:

private delegate void BuildHandler();

private void BuildGridPart1()
{
    //build first part of grid

    Dispatcher.BeginInvoke(new BuildHandler(BuildGridPart2), DispatcherPriority.Background);
}

private void BuildGridPart2()
{
    //build second part of grid

    Dispatcher.BeginInvoke(new BuildHandler(BuildGridPart3), DispatcherPriority.Background);
}

//etc

, (, , , ) , . Bingo, .

+4

, , .

, , , Dispatcher. , - , UIElements.

, UI DispatcherObject, Dispatcher , .

Dispatcher.VerifyAccess , . .

@Kent. , foreach foreach , , , Dispatcher.BeginInvoke, "" . , , . , , .

private void Window_Loaded(object sender, RoutedEventArgs e)
{
  CreateRow(1);
}

private void CreateRow(int i)
{
  // 
  // Construct row i of the grid
  //
  Dispatcher.BeginInvoke(DispatcherPriority.Background, new EventHandler(delegate { CreateRow(i + 1); }));
}
+1

, ? , ( I.E. ), .

, ? , .

, , , . , ... , , , , .

GOOD, , , API . ( , ). .

0
source

All Articles