Faced such a problem: I accept callback from the site and process it

public partial class MainWindow : Window { private readonly CallbackObjectForJs _callBackObjectForJs; private readonly CallbackObjectSaveSettings _callbackObjectSaveSettings; private static XmlHelper _xmlhelper; public MainWindow() { InitializeComponent(); _xmlhelper = new XmlHelper(); var hostInterface = new HostInterface(this); Browser.RegisterAsyncJsObject("hostInterface", hostInterface); Browser.ConsoleMessage += (s, e) => { Debug.WriteLine($"BROWSERCONSOLE: {e.Source}:{e.Line} - {e.Message}"); }; _callBackObjectForJs = new CallbackObjectForJs(); _callbackObjectSaveSettings = new CallbackObjectSaveSettings(); Browser.RegisterAsyncJsObject("callbackObj", _callBackObjectForJs); Browser.RegisterAsyncJsObject("callbackObj", _callbackObjectSaveSettings); } public class CallbackObjectForJs { public void showMessage(string msg) { MessageBox.Show(msg); } } public class CallbackObjectSaveSettings { public void showMessage(string msg) { MessageBox.Show(msg); } public void getAuthUser(string login, string password) { _xmlhelper.UpdateSettingValue("user_login", login); _xmlhelper.UpdateSettingValue("user_password", UserDataProtected.Shifrovka(password, "wsprod")); userBarGrid.Visibility = true; // ОШИБКА Требуется указать ссылку на объект } } } 

After that, I want to run the loadUserData () method, but getAuthUser swears at a non-static method (indicated by public static void loadUserData, the internal code begins to swear). Tell me, please, how to solve it? Thank!

  • How to solve? Make it static and correct errors or somehow get a link to the necessary class with this method and call it. In general, it is not clear why you need it, what you have to do with it, it may or may not be necessary for you at all - but you can’t understand without the code - tym32167
  • @ tym32167 when I receive a callback, I write the received data to a file, after which I need to make some elements visible MainWindow - Elizabeth
  • You only confused me more with this :) Based on the data that you gave, I cannot advise you anything. Please prepare a minimal, self-sufficient and reproducible example to make it clear where you have a problem. - tym32167
  • @ tym32167 in general .. :) I have a Grid in the window that has the Hidden property set, all I need is to switch the Grid property to Visible after the getAuthUser method, because it contains elements that should only be displayed after processing the getAuthUser method - Elizabeth
  • Details better, of course, put in the question itself. If all you need is to make the grid visible, then myGrid.Visible = true or something like that will suit you. What does it have to do with non-static methods I do not understand :) - tym32167

1 answer 1

If you don’t go into details on how best to write code and why to do it at all, but just concentrate on solving the problem you are asking about, then you can solve it by sending a copy of your window to your callbacks

 public class MainWindow { ...... _callbackObjectSaveSettings = new CallbackObjectSaveSettings(this); ...... } public class CallbackObjectSaveSettings { private MainWindow _mainWindow; public CallbackObjectSaveSettings(MainWindow mainWindow) { _mainWindow = mainWindow; } public void getAuthUser(string login, string password) { ...... ...... _mainWindow.Dispatcher.Invoke(()=> _mainWindow.userBarGrid.Visibility = true); } } 
  • _mainWindow.userBarGrid.Visibility = true; - Error - cannot convert source to bool - _mainWindow.userBarGrid.Visibility = Visibility.Visible; - like this, without errors, only the element is still hidden ( - Elizabeth
  • And you put a breakpoint there - is this code called at all? - tym32167
  • Called, but no sense, then a window appears and that's all - Elizabeth
  • Once the code is called, but the grid is still not visible, then show your XAML code, maybe it will clarify the problem. - tym32167
  • <Grid x: Name = "userBarGrid" Visibility = "Hidden"> there are a bunch of elements </ Grid> - Elizabeth