0

I am working on a c# WPF solution with a main project and 3 library projects. In the main project there is a window with this refresh methode to update changes I make to content.

public static class ExtensionMethods
    {
        private static readonly Action EmptyDelegate = delegate { };
        public static void Refresh(this UIElement uiElement)
        {
            uiElement.Dispatcher.Invoke(DispatcherPriority.Render, EmptyDelegate);
        }
    }

In the App.xaml.cs file I can use this refresh methode like this :

startupScreen.Worker.Content = "bla bla bla...";
startupScreen.Worker.Refresh();

That changes the content of the label Worker and refreshes the UI.

But now I also need the do this form one of the library projects. I can update the text by passing startupScreen.Worker as a parameter to a methode, but how can I acces the Refresh methode ?

Or are there other/better ways to update that label and force refresh the UI ?

RonnyMees
  • 1
  • 1
  • "*Or are there other/better ways to update that label and force refresh the UI?*" - yes. It's called data binding. Your approach is definitely wrong. – Clemens Apr 04 '21 at 07:23
  • Could you please elaborate your real goal here? Do you need just a refresh data in UI after the data was returned from library code? Or you need exactly to directly work with UIElement object from the library code? – Serg Apr 04 '21 at 07:28
  • In the library a heavy workload is runned where I need to update the UI with the status. So basicly every cyclus the routine runs I want to update a label in the UI to indicate where the routine is at. – RonnyMees Apr 04 '21 at 08:11
  • @Clemens: could you help me on the way on how I can use databinding in a project with data from a other project ( the library ) ? – RonnyMees Apr 04 '21 at 08:13
  • Without any apparent effort from your side? I don't think so. However, there should be an instance of a class in your library that implements the INotifyPropertyChanged interface. It should expose some kind of status property with change notification. The UI would bind to that object and its property. Start here: [Data binding overview](https://learn.microsoft.com/en-us/dotnet/desktop/wpf/data/data-binding-overview?view=netdesktop-5.0). – Clemens Apr 04 '21 at 08:24
  • If you need a some kind of progress indicator, you can check this for example of using bindings for such a task https://stackoverflow.com/questions/3520359/how-to-implement-a-progress-bar-using-the-mvvm-pattern. Please note that you will need to use some async/background task or worker to allow your UI to refresh during yoru heavy workload running. – Serg Apr 04 '21 at 08:29
  • Thank you very much Clemens, I will look into that, learn to implement it in a small project and then try it on this project. – RonnyMees Apr 04 '21 at 08:39

0 Answers0