Async Function Only Updating One "Await" - c#

Excuse my ignorance on this subject, but I'm not schooled in asynchronous programming. However, I believe my code is close to achieving what I need it to do.
Basically the code below only works for the very first
C61 = new BridgeViewModel("C61");
await C61.Initialize();
statements. None of the other items are returning any data to the bound .xaml and I have no idea why. Does each one need to be in its own Initialize function?
Any help is greatly appreciated.
Code:
namespace SentinelApp
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var vm = new MainViewModel();
DataContext = vm;
vm.Initialize();
}
}
public class MainViewModel
{
public readonly string[] Bridges = { "C61", "C62", "C63", "C68", "C69", "K71", "K72", "K73", "K74", "T91", "GN01", "GE01", "GA01" };
public async Task Initialize()
{
C61 = new BridgeViewModel("C61");
await C61.Initialize();
C62 = new BridgeViewModel("C62");
await C62.Initialize();
C63 = new BridgeViewModel("C63");
await C63.Initialize();
C68 = new BridgeViewModel("C68");
await C68.Initialize();
C69 = new BridgeViewModel("C69");
await C69.Initialize();
K71 = new BridgeViewModel("K71");
await K71.Initialize();
K72 = new BridgeViewModel("K72");
await K72.Initialize();
K73 = new BridgeViewModel("K73");
await K73.Initialize();
K74 = new BridgeViewModel("K74");
await K74.Initialize();
T91 = new BridgeViewModel("T91");
await T91.Initialize();
GA01 = new BridgeViewModel("GA01");
await GA01.Initialize();
GE01 = new BridgeViewModel("GE01");
await GE01.Initialize();
GN01 = new BridgeViewModel("GN01");
await GN01.Initialize();
}
public BridgeViewModel C61 { get; set; }
public BridgeViewModel C62 { get; set; }
public BridgeViewModel C63 { get; set; }
public BridgeViewModel C68 { get; set; }
public BridgeViewModel C69 { get; set; }
public BridgeViewModel K71 { get; set; }
public BridgeViewModel K72 { get; set; }
public BridgeViewModel K73 { get; set; }
public BridgeViewModel K74 { get; set; }
public BridgeViewModel T91 { get; set; }
public BridgeViewModel GA01 { get; set; }
public BridgeViewModel GE01 { get; set; }
public BridgeViewModel GN01 { get; set; }
}
public class BridgeViewModel : ViewModel
{
private readonly ClientWrapper _client;
private readonly string _bridge;
private readonly Timer _timer = new Timer();
public BridgeViewModel(string bridge)
{
_client = new ClientWrapper();
_bridge = bridge;
}
public async Task Initialize()
{
await _client.Connect(_bridge);
await _client.SendMessage(new SessionStart("3", "25").CreateBridgeMessage());
_timer.Interval = 1000;
_timer.Elapsed += Update;
_timer.Start();
}
private async void Update(object sender, ElapsedEventArgs e)
{
try {
var response = await _client.SendMessage("BS~RESERVE~STATS~REQ~" + _bridge + "~0");
var split = response.Split('~');
Timestamp = split[4].Substring(0, 2) + ":" + split[4].Substring(2, 2) + ":" + split[4].Substring(4, 2);
FreePorts = split[6];
LongestHold = TimeSpan.FromSeconds(int.Parse(split[15])).ToString("hh:mm");
Bells = split[12];
Signals = split[8];
} catch { }
}
private string _timestamp;
public string Timestamp
{
get { return _timestamp; }
set { _timestamp = value; RaisePropertyChanged(); }
}
private string _bells;
public string Bells
{
get { return _bells; }
set { _bells = value; RaisePropertyChanged(); }
}
private string _signals;
public string Signals
{
get { return _signals; }
set { _signals = value; RaisePropertyChanged(); }
}
private string _freeports;
public string FreePorts
{
get { return _freeports; }
set { _freeports = value; RaisePropertyChanged(); }
}
private string _longesthold;
public string LongestHold
{
get { return _longesthold; }
set { _longesthold = value; RaisePropertyChanged(); }
}
}
}

Instead of using your async method inside a constructor, register and use it inside the FrameworkElement.Loaded event where you can await:
public MainWindow()
{
InitializeComponent();
Loaded += InitializeOnLoaded;
}
public async void InitializeOnLoaded(object sender, RoutedEventArgs e)
{
var vm = new MainViewModel();
DataContext = vm;
await vm.InitializeAsync();
}

Related

What causes a method to pass null values?

So I wrote a method of code and it pulls from the database correctly (I am using Dapper), but it doesnt pass off to the next method. Can anyone tell me why and what I am doing wrong? Not quite understanding what I am doing wrong here. I have tried a few different ways including below and making and IEnumerable list. I can see the variables in the logger so I know I am pulling them correctly, just not sure why they arent sending to the CheckSite().
public class UptimeService
{
private readonly ILogger<UptimeService> _logger;
private readonly IWebsiteData _webdb;
private readonly IUptimeData _db;
public UptimeService(IWebsiteData webdb, IUptimeData db ,ILogger<UptimeService> logger)
{
_webdb = webdb;
_logger = logger;
_db= db;
}
public class SiteResponse
{
public int Websiteid { get; set; }
public string Name { get; set; }
public string Url { get; set; }
public bool Status { get; set; }
public long ResponseTime { get; set; }
}
public async Task GetAllWebsites()
{
var websites = await _webdb.GetWebsites();
foreach (var website in websites)
{
_logger.LogInformation($"WEBSITE::::: {website.Url} | {website.Name} | {website.Websiteid}");
CheckSite(website.Url, website.Name, website.Websiteid);
}
return ;
}
public SiteResponse CheckSite(string Url, string Name, int Websiteid)
{
var result = new SiteResponse();
var stopwatch = new Stopwatch();
stopwatch.Start();
var client = new HttpClient();
_logger.LogInformation(
$"TEST URL: {result.Url}");
try
{
var checkingResponse = client.GetAsync(Url).Result;
result.Status = checkingResponse.IsSuccessStatusCode &&
checkingResponse.StatusCode == HttpStatusCode.OK;
}
catch
{
result.Status = false;
// offline
}
stopwatch.Stop();
var elapsed = stopwatch.ElapsedMilliseconds;
result.ResponseTime = elapsed;
if (result.Status)
{
// archive record
RecordToDb(result);
}
else
{
_logger.LogInformation(
$"Status is {result.Status}");
}
return result;
}
public async void RecordToDb(SiteResponse response)
{
var newRecord = new UptimeModel
{
Time = DateTime.Now,
Status = response.Status,
ResponseTime = (int)response.ResponseTime,
Websiteid = response.Websiteid,
Name = response.Name,
};
_logger.LogInformation(
$"Trying to Save {response.Name}");
await _db.InsertUptime(newRecord);
}
}
If the result.Url is empty here:
_logger.LogInformation($"TEST URL: {result.Url}");
that's because it's a new instance of SiteResponse() method.
If it is showing as null, you'll need to create constructors on the class. Here is an example:
public class SiteResponse
{
public SiteResponse(){ }
public SiteResponse(string url){
Url = url;
}
public int Websiteid { get; set; }
public string Name { get; set; }
public string Url { get; set; }
public bool Status { get; set; }
public long ResponseTime { get; set; }
}
and then initialize the new one like:
var result = new SiteResponse(Url);
Based on the comments below, I would refactor to something like this.
public class UptimeService
{
private readonly ILogger<UptimeService> _logger;
private readonly IWebsiteData _webdb;
private readonly IUptimeData _db;
public UptimeService(IWebsiteData webdb, IUptimeData db ,ILogger<UptimeService> logger)
{
_webdb = webdb;
_logger = logger;
_db= db;
}
public class SiteResponse
{
public int Websiteid { get; set; }
public string Name { get; set; }
public string Url { get; set; }
public bool Status { get; set; }
public long ResponseTime { get; set; }
}
public async Task GetAllWebsites()
{
var websites = await _webdb.GetWebsites();
foreach (var website in websites)
{
_logger.LogInformation($"WEBSITE::::: {website.Url} | {website.Name} | {website.Websiteid}");
await CheckSite(website);
}
return ;
}
public async Task CheckSite(SiteResponse siteResponse)
{
var stopwatch = new Stopwatch();
stopwatch.Start();
var client = new HttpClient();
_logger.LogInformation(
$"TEST URL: {siteResponse.Url}");
try
{
var checkingResponse = await client.GetAsync(siteResponse.Url);
siteResponse.Status = checkingResponse.IsSuccessStatusCode &&
checkingResponse.StatusCode == HttpStatusCode.OK;
}
catch
{
siteResponse.Status = false;
// offline
}
stopwatch.Stop();
var elapsed = stopwatch.ElapsedMilliseconds;
siteResponse.ResponseTime = elapsed;
if (siteResponse.Status)
{
// archive record
RecordToDb(siteResponse);
}
else
{
_logger.LogInformation(
$"Status is {siteResponse.Status}");
}
return;
}
public async void RecordToDb(SiteResponse response)
{
var newRecord = new UptimeModel
{
Time = DateTime.Now,
Status = response.Status,
ResponseTime = (int)response.ResponseTime,
Websiteid = response.Websiteid,
Name = response.Name,
};
_logger.LogInformation(
$"Trying to Save {response.Name}");
await _db.InsertUptime(newRecord);
}
}

Refresh datagrid when changes to database are made

I'm writing a wpf application, where I have music albums and corresponding songs. I can add albums and corresponding songs. But now I want to to refresh the view when a change to the database is made. I found many possible solutions, but as I'm new to wpf and c# I don't know which one would suite my code.
In my MainView have an album list and a add button which opens another window where I can add data with a textbox.
AlbumListViewModel
#region Constants
IWindowManager addAlbum = new WindowManager();
IWindowManager addSong = new WindowManager();
private AlbumViewModel _selectedAlbum;
private SongViewModel _selectedSong;
#endregion
#region Constructor
public AlbumListViewModel()
{
Albums = new ObservableCollection<AlbumViewModel>(GetAlbumList());
AddAlbumCommand = new RelayCommand(x => AddAlbum());
AddSongCommand = new RelayCommand(x => AddSong());
}
#endregion
#region Properties
public ICommand AddAlbumCommand { get; private set; }
public ICommand AddSongCommand { get; private set; }
public ObservableCollection<AlbumViewModel> Albums { get; set; }
public AlbumViewModel SelectedAlbum
{
get
{
return _selectedAlbum;
}
set
{
if (_selectedAlbum != value)
{
_selectedAlbum = value;
}
NotifyPropertyChanged("SelectedAlbum");
}
}
public SongViewModel SelectedSong
{
get
{
return _selectedSong;
}
set
{
if (_selectedSong != value)
{
_selectedSong = value;
}
NotifyPropertyChanged("SelectedSong");
}
}
#endregion
#region Methods
public List<AlbumViewModel> GetAlbumList()
{
var controller = new BandManagerController();
return controller.GetAlbumList()
.Select(a => new AlbumViewModel(a))
.ToList();
}
private void AddAlbum()
{
addAlbum.ShowDialog(new AlbumViewModel(new AlbumData()));
}
private void AddSong()
{
addSong.ShowDialog(new SongViewModel(new SongData { AlbumID = SelectedAlbum.AlbumID }));
}
It opens the AlbumView where I add albums to the database.
public class AlbumViewModel : Screen
{
#region Constants
private AlbumData _data;
#endregion
#region Constructor
public AlbumViewModel(AlbumData data)
{
_data = data;
SongListVM = new SongListViewModel(data.AlbumID);
SaveAlbumToDatabase = new RelayCommand(x => AlbumToDatabase(data));
}
#endregion
#region Properties
public SongListViewModel SongListVM { get; set; }
public ICommand SaveAlbumToDatabase { get; private set; }
public string AlbumName
{
get
{
return _data.AlbumName;
}
set
{
if (_data.AlbumName != value)
{
_data.AlbumName = value;
NotifyOfPropertyChange("AlbumName");
}
}
}
public int AlbumID
{
get
{
return _data.AlbumID;
}
set
{
if (_data.AlbumID != value)
{
_data.AlbumID = value;
NotifyOfPropertyChange("AlbumID");
}
}
}
public string AlbumYear
{
get
{
return _data.AlbumYear;
}
set
{
if (_data.AlbumYear != value)
{
_data.AlbumYear = value;
NotifyOfPropertyChange("AlbumYear");
}
}
}
#endregion
#region Methods
public AlbumData AddAlbumEntry(AlbumData albumData)
{
var controller = new BandManagerController();
return controller.AddAlbumEntry(albumData);
}
public void ExecuteCancelCommand()
{
(GetView() as Window).Close();
}
public void AlbumToDatabase(AlbumData data)
{
AddAlbumEntry(data);
ExecuteCancelCommand();
}
#endregion
}
The AddAlbumEntry Method in the ALbumView is in a different class which is the connections to my database. I already use an ObservableCollection but don't know how to tell it the Database was updated.
Thanks in advance!
Just want to answer my question. I just changed my AddAlbum method to use a Deactivated event, to reload the Collection after the Dialog closes like:
private void AddAlbum()
{
var vm = new AlbumViewModel(new AlbumData());
vm.Deactivated += (s, e) => GetAlbumList();
addAlbum.ShowDialog(vm);
}

Correct way to get results from a blocking collection

I have 50 IMountCmd objects from one or more threads and drop them in a blocking collection. Each is executed and some get results or errors. They are put into a ConcurrentDictionary where I loop for ContainsKey and return the objects. Does this seems thread safe and correct way to process a blocking queue?
public class CmdGetAxisDegrees : IMountCommand
{
public long Id { get; }
public DateTime CreatedUtc { get; private set; }
public bool Successful { get; private set; }
public Exception Exception { get; private set; }
public dynamic Result { get; private set; }
private readonly AxisId _axis;
public CmdGetAxisDegrees(long id, AxisId axis)
{
Id = id;
_axis = axis;
CreatedUtc = HiResDateTime.UtcNow;
Successful = false;
Queues.AddCommand(this);
}
public void Execute(Actions actions)
{
try
{
Result = actions.GetAxisDegrees(_axis);
Successful = true;
}
catch (Exception e)
{
Successful = false;
Exception = e;
}
}
}
private static void ProcessCommandQueue(IMountCommand command)
{
command.Execute(_actions);
if (command.Id > 0)
{
_resultsDictionary.TryAdd(command.Id, command);
}
}
public static IMountCommand GetCommandResult(long id)
{
IMountCommand result;
while (true)
{
if (!_resultsDictionary.ContainsKey(id)) continue;
var success = _resultsDictionary.TryRemove(id, out result);
if (!success)
{
//log
}
break;
}
return result;
}
static Queues()
{
_actions = new Actions();
_resultsDictionary = new ConcurrentDictionary<long, IMountCommand>();
_commandBlockingCollection = new BlockingCollection<IMountCommand>();
Task.Factory.StartNew(() =>
{
foreach (var command in _commandBlockingCollection.GetConsumingEnumerable())
{
ProcessCommandQueue(command);
}
});
}
public interface IMountCommand
{
long Id { get; }
DateTime CreatedUtc { get; }
bool Successful { get; }
Exception Exception { get; }
dynamic Result { get; }
void Execute(Actions actions);
}

Xamarin rest web service doesn't deserialize

I want to use rest with get method. My code is below;
public class RegisterPage : ContentPage
{
Label label, l4, label2;
public RegisterPage()
{
Button btn = new Button
{
Text = "register"
};
btn.Clicked += Btn_Clicked;
label = new Label();
l4 = new Label();
label2 = new Label();
Content = new StackLayout
{
Children = {
btn,
label,
l4,
label2
}
};
}
private async void Btn_Clicked(object sender, EventArgs e)
{
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Add(Constants.API_KEY_HEADER_KEY, Constants.API_KEY);
string URL = Constants.URL;
var response = await client.GetAsync(URL);
var content = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<Models.Result>(content);
label.Text = result.Success.ToString();
l4.Text = result.Error.ToString();
label2.Text = ((RegisteredDevice)result.Retval).Clientuuid + " - " + ((RegisteredDevice)result.Retval).Deviceuuid;
}
}
The url is working good. And my content value has json string. But the serialization is not working.
var result = JsonConvert.DeserializeObject(content);
This code doesn't deserilize.
My model is;
public class Result
{
private object retval = null;
private bool success = false;
private Error error = null;
internal Error Error
{
get { return error; }
set { error = value; }
}
public bool Success
{
get { return success; }
set { success = value; }
}
public object Retval
{
get { return retval; }
set { retval = value; }
}
}
Json:
{
"result":{
"retail":{
"#xsi.type":"registeredDevice",
"clientuuid":"28asgargb-acfe‌​-41dfgsdg51",
"deviceuuid":123456
},
"success":true
}
}
I think the problem comes from :
private object retval = null;
So for me the best way to construct serialization objects in C# is to use this web site :
http://json2csharp.com/
This will tell you if your json is correct and he will generate the classes you need for you, here the classes generated by json2csharp
public class Retail
{
public string __invalid_name__#xsi.type { get; set; }
public string clientuuid { get; set; }
public int deviceuuid { get; set; }
}
public class Result
{
public Retail retail { get; set; }
public bool success { get; set; }
}
public class RootObject
{
public Result result { get; set; }
}

Updating another ViewModel when button clicked

I'm trying to update another ViewModel when a button is clicked. That Viewmodel already has a button that updates its own ViewModel but I want the exact same functionallity on another viewModel
Here is my code:
OlyckorViewModel (The view that I want to update and that already has a button that updates it) SearchActiveInvestigationsCommand is the button binding that updates it
public class OlyckorViewModel : NotificationObject
{
private readonly ISosServiceDelegate _sosService;
private readonly IDialogService _dialogService;
private string _searchConditionDiarienummer;
private string _searchConditionFartygsnamn;
private string _searchConditionRegisterbeteckning;
private int? _searchConditionIMONummer;
private DateTime? _searchConditionHaendelseDatum;
private string _searchConditionOlyckshaendelsetypKod;
private bool _isSearching;
private bool _isSearchExpanded = true;
private ObservableCollection<SosListOlycka> _olyckor;
private SosListOlycka _selectedOlycka;
private string _statusText;
public OlyckorViewModel(ISosServiceDelegate sosService, ILoggerFacade logger, IDialogService dialogServce)
{
logger.Log("Initializing OlyckorViewModel", Category.Debug, Priority.Low);
_sosService = sosService;
_dialogService = dialogServce;
SearchCommand = new DelegateCommand(ExecuteSearch, CanExecuteSearch);
SearchActiveInvestigationsCommand = new DelegateCommand(ExecuteSearchActiveInvestigations, CanExecuteSearchActiveInvestigations);
OpenCommand = new DelegateCommand(ExecuteOpen, CanExecuteOpen);
DeleteCommand = new DelegateCommand(ExecuteDelete, CanExecuteDelete);
CreatePdfReportCommand = new DelegateCommand(ExecuteCreatePdfReport, CanExecuteCreatePdfReport);
}
private void ExecuteSearch()
{
if (IsSearching)
return;
IsSearching = true;
StatusText = "Söker olyckor...";
var criteria = new SosOlyckaSearchCriteria();
criteria.Diarienummer = SearchConditionDiarienummer;
criteria.Fartygsnamn = SearchConditionFartygsnamn;
criteria.Registerbeteckning = SearchConditionRegisterbeteckning;
criteria.IMONummer = SearchConditionIMONummer;
criteria.HaendelseDatum = SearchConditionHaendelseDatum;
criteria.OlyckshaendelseTypKod = SearchConditionOlyckshaendelsetypKod;
_sosService.SearchOlyckor(
criteria,
olyckor =>
{
HandleResultFromSearch(olyckor);
},
exception =>
{
IsSearching = false;
StatusText = "Misslyckades att söka olyckor";
DialogHelper.ShowException(exception);
}
);
}
private void HandleResultFromSearch(SosSearchResult<SosListOlycka> olyckor)
{
IsSearching = false;
IsSearchExpanded = false;
Olyckor = new ObservableCollection<SosListOlycka>(olyckor.Items);
StatusText = Olyckor.Count + " " + (Olyckor.Count == 1 ? "olycka." : "olyckor.");
if (olyckor.IsResultTruncated)
{
var statusTextResultTruncated = "Resultatet var för stort (" + olyckor.OriginalNumberOfHits + " olyckor) och trunkerades på servern.";
StatusText += " " + statusTextResultTruncated;
}
// if search result only contains a single item, it should be opened
if (Olyckor.Count == 1)
DialogHelper.OpenOlyckaDialog(Olyckor[0].OlyckaId);
}
OlyckaViewModel (The viewModel that is supposed to have a save button to update the viewmodel above) SaveCommand is the button binding that I want to update like the button above
public class OlyckaViewModel : DialogWindowViewModel
{
private readonly ISosServiceDelegate _sosService;
private readonly Repository _repository;
private readonly IDialogService _dialogService;
private readonly IInteractionService _interactionService;
private readonly FroCodesViewModel _froCodesViewModel;
private SosOlycka _model;
private SosOlycksorsak _selectedOlycksorsak;
private SosOlycksorsak _selectedHuvudorsak;
private SosStegIOlycksfoerlopp _selectedStegIOlycksfoerlopp;
private SosStegIOlycksfoerlopp _selectedInledandeSteg;
private SosOlycka _selectedOtherOlycka;
private OlyckorViewModel _olyckorlist;
private readonly ObservableCollection<CheckBoxListItemViewModel<TrsFartygsunderkategori>> _fartygsunderkategorier = new ObservableCollection<CheckBoxListItemViewModel<TrsFartygsunderkategori>>();
private int? _olycksrapportIdToHaemtmarkeraWhenSaving;
private ObservableCollection<SosListOlycka> _olyckor;
private SosListOlycka _selectedOlycka;
private bool _isSearching;
private bool _isSearchExpanded = true;
private readonly List<KnownValue> _oestEllerVaest = new List<KnownValue>()
{
new KnownValue("Välj", null),
new KnownValue("E", "E"),
new KnownValue("W", "W")
};
private readonly List<KnownValue> _nordEllerSyd = new List<KnownValue>()
{
new KnownValue("Välj", null),
new KnownValue("N", "N"),
new KnownValue("S", "S")
};
public OlyckaViewModel(ISosServiceDelegate sosService, Repository repository, IDialogService dialogService, IInteractionService interactionService)
{
_sosService = sosService;
_repository = repository;
_dialogService = dialogService;
_interactionService = interactionService;
_froCodesViewModel = new FroCodesViewModel(ServiceLocator.Current.GetInstance<ISitsServiceDelegate>());
SaveCommand = new DelegateCommand(ExecuteSave);
ReloadCommand = new DelegateCommand(ExecuteReload, CanExecuteIfExistingOlycka);
CloseCommand = new DelegateCommand(Close);
DeleteCommand = new DelegateCommand(ExecuteDelete, CanExecuteIfExistingOlycka);
AvslutaUtredningCommand = new DelegateCommand(ExecuteAvslutaUtredning, CanExecuteIfExistingOlycka);
CreatePdfReportCommand = new DelegateCommand(ExecuteCreatePdfReport, CanExecuteIfExistingOlycka);
AddOlycksorsakCommand = new DelegateCommand(ExecuteAddOlycksorsak);
RemoveOlycksorsakCommand = new DelegateCommand(ExecuteRemoveOlycksorsak, CanExecuteRemoveOlycksorsak);
AddStegIOlycksfoerloppCommand = new DelegateCommand(ExecuteAddStegIOlycksfoerlopp);
RemoveStegIOlycksfoerloppCommand = new DelegateCommand(ExecuteRemoveStegIOlycksfoerlopp, CanExecuteRemoveStegIOlycksfoerlopp);
FetchFartygsinformationFromSitsCommand = new DelegateCommand(ExecuteFetchFartygsinformationFromSits);
NewOlyckaFromHaendelseCommand = new DelegateCommand(ExecuteNewOlyckaFromHaendelse);
OpenOtherOlyckaCommand = new DelegateCommand(ExecuteOpenOtherOlycka, CanExecuteOpenOtherOlycka);
MoveOlyckaToNewHaendelseCommand = new DelegateCommand(ExecuteMoveOlyckaToNewHaendelse);
MoveOlyckaToOtherHaendelseCommand = new DelegateCommand(ExecuteMoveOlyckaToOtherHaendelse);
MarkSelectedOlycksorsakAsHuvudorsakCommand = new DelegateCommand(ExecuteMarkSelectedOlycksorsakAsHuvudorsak);
MarkSelectedStegIOlycksfoerloppAsInledandeStegCommand = new DelegateCommand(MarkSelectedStegIOlycksfoerloppAsInledandeSteg);
SearchActiveInvestigationsCommand = new DelegateCommand(ExecuteSearchActiveInvestigations, CanExecuteSearchActiveInvestigations);
}
public DelegateCommand SaveCommand { get; private set; }
public DelegateCommand ReloadCommand { get; private set; }
public DelegateCommand CloseCommand { get; private set; }
public DelegateCommand DeleteCommand { get; private set; }
public DelegateCommand AvslutaUtredningCommand { get; private set; }
public DelegateCommand CreatePdfReportCommand { get; private set; }
public DelegateCommand AddOlycksorsakCommand { get; private set; }
public DelegateCommand RemoveOlycksorsakCommand { get; private set; }
public DelegateCommand AddStegIOlycksfoerloppCommand { get; private set; }
public DelegateCommand RemoveStegIOlycksfoerloppCommand { get; private set; }
public DelegateCommand FetchFartygsinformationFromSitsCommand { get; private set; }
public DelegateCommand NewOlyckaFromHaendelseCommand { get; private set; }
public DelegateCommand OpenOtherOlyckaCommand { get; private set; }
public DelegateCommand MoveOlyckaToNewHaendelseCommand { get; private set; }
public DelegateCommand MoveOlyckaToOtherHaendelseCommand { get; private set; }
public DelegateCommand MarkSelectedOlycksorsakAsHuvudorsakCommand { get; private set; }
public DelegateCommand MarkSelectedStegIOlycksfoerloppAsInledandeStegCommand { get; private set; }
public DelegateCommand SearchActiveInvestigationsCommand { get; private set; }
public DelegateCommand SearchCommand { get; private set; }
public FroCodesViewModel FroCodesViewModel { get { return _froCodesViewModel; } }
public OlyckorViewModel OlyckorViewModel { get { return _olyckorlist; } }
public SosOlycka Model
{
get { return _model; }
private set
{
if (value != _model)
{
_model = value;
RaisePropertyChanged(() => Model);
RaisePropertyChanged(() => IsExistingOlycka);
RaiseCanExecuteChangedForOlyckaCommands();
LoadDescriptionsForFroCodes();
SynchronizeFromModel();
}
}
}
private void ExecuteSave()
{
if (IsBusy)
throw new InvalidOperationException("Cannot save olycka, is already busy");
SynchronizeToModel();
// Validate ad-acta
List<string> missingAdActaFields = null;
if (Model.MyndighetensUtredningAvslutad.HasValue)
{
missingAdActaFields = DataHelper.ValidateAdActa(Model);
}
// Validate skrovskada
List<string> missingSkrovskadaFields = null;
if (Model.SosFartygsskada.Skrovskada == "J")
{
missingSkrovskadaFields = DataHelper.ValidateSkrovskada(Model);
}
if ((missingAdActaFields != null && missingAdActaFields.Count > 0)
|| (missingSkrovskadaFields != null && missingSkrovskadaFields.Count > 0))
{
// Bring up window of missing fields (or update if already open)
var vm = _dialogService.GetOpenDialogs().OfType<MissingFieldsViewModel>().FirstOrDefault();
if (vm != null)
{
vm.AdActaFields = missingAdActaFields;
vm.SkrovskadaFields = missingSkrovskadaFields;
_dialogService.Activate(vm);
}
else
{
vm = ServiceLocator.Current.GetInstance<MissingFieldsViewModel>();
vm.AdActaFields = missingAdActaFields;
vm.SkrovskadaFields = missingSkrovskadaFields;
_dialogService.Show(vm);
}
//
// Cancel save operation
return;
}
else
{
// Close window with missing fields if open)
var vm = _dialogService.GetOpenDialogs().OfType<MissingFieldsViewModel>().FirstOrDefault();
if (vm != null)
{
vm.Close();
}
}
SetBusy("Sparar ändringar...");
_sosService.SaveOlycka(
Model,
result =>
{
// load olycka using the id returned from save method (useful if olycka was new / not previously persisted)
ResetBusy();
if (IsGoingToHaemtmarkeraWhenSaving)
{
SetBusy("Markerar olycksrapport som hämtad...");
var rosService = ServiceLocator.Current.GetInstance<IRosServiceDelegate>();
rosService.HaemtmarkeraOlycksrapport(
OlycksrapportIdToHaemtmarkeraWhenSaving.Value,
result,
() =>
{
ResetBusy();
OlycksrapportIdToHaemtmarkeraWhenSaving = null;
LoadOlycka(result);
},
haemtmarkeraException =>
{
ResetBusy();
DialogHelper.ShowException(haemtmarkeraException);
}
);
}
else
{
LoadOlycka(result);
}
},
exception =>
{
ResetBusy();
DialogHelper.ShowException(exception);
}
);
ExecuteSearchActiveInvestigations();
}
I know it's alot of code, I just wanted to make sure not to miss anything.
I tried to copy the same code from the already working button to the button I want to work the same but it didn't work and I suspect it's because I didn't tell it to update another view and not itself. I could be wrong tho.
I appreciate any help,
Raise an event from OlyckaViewModel with the required parameters and subscribe in OlyckorViewModel.
Make the code common in the SearchActiveInvestigationsCommand and call the same function from the subscribed event method.
I'm trying to update another ViewModel when a button is clicked.
In the another viewmodel centralize the update code into a public method.
In the App code, make a static reference to the VM in question.
Make the another viewmodel place a reference to itself onto the app as a static reference.
Centralize the update code into a public method on that VM. The method may have multiple variables which have to be pass through to do any update from any foreign VMs or button clicks from views.
On the button click, get the global app from (see Application.Current Property (System.Windows)) and find the reference to the VM in question. Then call the update method.

Categories