Please can you help me,
I have stumbled into a problem that I don't understand or know how to fix.
I have 2 tabs in my WPF MainWindow.xaml file.
I am trying to load a web site into each WebControl. However, for the second WebControl - WebViewB - the website has been stopped in IIS, so rightfully, the page won't load.
What I would like to do is to create an event handler that keeps trying to load from the URL until it succeeds.
I almost have this working, however, my event handler fires twice each time and I can't understand why. The code will follow below the xaml description of my UI.
Quote:<TabControl x:Name="AppTabControl">
<TabItem x:Name="TabItemServerA" Header="{Binding Path=ServerAName}" Resources="{StaticResource TabControlStyling}" Foreground="{Binding Path=ServerAStatusBrush}">
<eo:WebControl >
<eo:WebControl.WebView >
<eo:WebView x:Name="WebViewA">
</eo:WebView>
</eo:WebControl.WebView>
</eo:WebControl>
</TabItem>
<TabItem x:Name="TabItemServerB" Header="{Binding Path=ServerBName}" Resources="{StaticResource TabControlStyling}" Foreground="{Binding Path=ServerBStatusBrush}">
<eo:WebControl>
<eo:WebControl.WebView>
<eo:WebView x:Name="WebViewB">
</eo:WebView>
</eo:WebControl.WebView>
</eo:WebControl>
</TabItem>
An extract of my code behind my XAML form is:
Quote:public partial class MainWindow : Window
{
//code committed for clarity
public ViewModel.MainViewModel ViewModel { get; }
private readonly ViewPublisher _viewPublisher;
private readonly ServiceManager _serviceManager;
public MainWindow()
{
#region Single Instance Only
// Stop application from launching multiple times.
String appProcessName = Process.GetCurrentProcess().ProcessName;
if (Process.GetProcesses().Count(p => p.ProcessName == appProcessName) > 1)
return;
#endregion
//Add the EO Browser Licence - Omitted for clarity
ApplicationConfigurationSettingsRepository applicationConfigurationSettingsRepository = new
ApplicationConfigurationSettingsRepository();
this._serviceManager = new ServiceManager(applicationConfigurationSettingsRepository);
//Start the timer that publishes updates
_serviceManager.StartTimer();
var serviceManagerConfigurationSettingsRepository = _serviceManager.ConfigurationSettingsRepository;
var serviceManagerPublisher = _serviceManager.ServicePublisher;
this.ViewModel = new ViewModel.MainViewModel(serviceManagerConfigurationSettingsRepository, ref serviceManagerPublisher);
this._viewPublisher = ViewModel.ViewPublisher;
_viewPublisher.UpdateViewOnChange += (sender, e) =>
{
DateTime updatedAt = e.UpdateDateTime;
OnViewChange(updatedAt);
};
this.DataContext = ViewModel;
InitializeComponent();
//Here is my event handler for Load Failed
this.WebViewB.LoadFailed += WebViewBOnLoadFailed;
this.Closing += MainWindow_Closing;
}
/// <summary>
/// An Event to try and re-load the URL in the WebViewB if it fails to load
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void WebViewBOnLoadFailed(object sender, LoadFailedEventArgs e)
{
e.ErrorMessage = $"Unable to load page from {ViewModel.ServerBName}. Error code {e.ErrorCode}. Program will will keep trying to connect.";
LoadFailedServerBCounter++; //An integer to count the number of fails, for reporting purposes.
Infrastructure.Common.StaticLogger.LogError(GetType(),
$"The WebView control in '{ViewModel.ServerBName}' failed to load Smart-Trac. Attempting to re-load the url {ViewModel.SmartTracServerBUrl} again. Number of tries {LoadFailedServerBCounter.ToString()}. " +
e.ErrorMessage); //This shows twice in the log file!
WebViewB.LoadUrlAndWait(ViewModel.SmartTracServerBUrl);
WebViewB.Reload(true);
}
}
The problem is my log file - created by the event handler
WebViewBOnLoadFailed shows something like this:
Quote:2020-10-28 09:18:53.4825 ERROR [1] The WebView control in 'Server B TUMMEL' failed to load Smart-Trac. Attempting to re-load the url http://tummel.epic-track.com again. Number of tries 1. A connection attempt timed out.
2020-10-28 09:18:53.4825 ERROR [1] The WebView control in 'Server B TUMMEL' failed to load Smart-Trac. Attempting to re-load the url http://tummel.epic-track.com again. Number of tries 1. A connection attempt timed out.
2020-10-28 09:18:53.7325 ERROR [1] The WebView control in 'Server B TUMMEL' failed to load Smart-Trac. Attempting to re-load the url http://tummel.epic-track.com again. Number of tries 2. A connection attempt timed out.
2020-10-28 09:18:53.7325 ERROR [1] The WebView control in 'Server B TUMMEL' failed to load Smart-Trac. Attempting to re-load the url http://tummel.epic-track.com again. Number of tries 2. A connection attempt timed out. The logs are duplicated for every event that is fired.When I start IIS again and click in WebViewB - the Reload Method or something like it seems to fire for all the times that it could not when the server was unavailable.
I have had a look at the following links for insights but have not found a solution yet:
https://stackoverflow.com/questions/25190270/c-sharp-event-handler-is-called-multiple-times-when-event-is-raised-oncehttps://stackoverflow.com/questions/31012969/custom-event-firing-multiple-timesAny help would be appreciated.
Many thanks.