Exemplo n.º 1
0
            private CoinViewModels()
            {
#if DEBUG
                NTStopwatch.Start();
#endif
                if (WpfUtil.IsInDesignMode)
                {
                    return;
                }
                VirtualRoot.AddEventPath <ServerContextReInitedEvent>("ServerContext刷新后刷新VM内存", LogEnum.DevConsole,
                                                                      action: message => {
                    _dicById.Clear();
                    Init();
                }, location: this.GetType());
                VirtualRoot.AddEventPath <ServerContextVmsReInitedEvent>("ServerContext的VM集刷新后刷新视图界面", LogEnum.DevConsole,
                                                                         action: message => {
                    AllPropertyChanged();
                }, location: this.GetType());
                AddEventPath <CoinAddedEvent>("添加了币种后刷新VM内存", LogEnum.DevConsole,
                                              action: (message) => {
                    _dicById.Add(message.Target.GetId(), new CoinViewModel(message.Target));
                    AppContext.Instance.MinerProfileVm.OnPropertyChanged(nameof(NTMiner.AppContext.Instance.MinerProfileVm.CoinVm));
                    AllPropertyChanged();
                }, location: this.GetType());
                AddEventPath <CoinRemovedEvent>("移除了币种后刷新VM内存", LogEnum.DevConsole,
                                                action: message => {
                    _dicById.Remove(message.Target.GetId());
                    AppContext.Instance.MinerProfileVm.OnPropertyChanged(nameof(NTMiner.AppContext.Instance.MinerProfileVm.CoinVm));
                    AllPropertyChanged();
                }, location: this.GetType());
                AddEventPath <CoinUpdatedEvent>("更新了币种后刷新VM内存", LogEnum.DevConsole,
                                                action: message => {
                    CoinViewModel coinVm = _dicById[message.Target.GetId()];
                    bool justAsDualCoin  = coinVm.JustAsDualCoin;
                    coinVm.Update(message.Target);
                    coinVm.TestWalletVm.Address = message.Target.TestWallet;
                    coinVm.OnPropertyChanged(nameof(coinVm.Wallets));
                    coinVm.OnPropertyChanged(nameof(coinVm.WalletItems));
                    if (AppContext.Instance.MinerProfileVm.CoinId == message.Target.GetId())
                    {
                        AppContext.Instance.MinerProfileVm.OnPropertyChanged(nameof(NTMiner.AppContext.Instance.MinerProfileVm.CoinVm));
                    }
                    CoinKernelViewModel coinKernelVm = AppContext.Instance.MinerProfileVm.CoinVm.CoinKernel;
                    if (coinKernelVm != null &&
                        coinKernelVm.CoinKernelProfile.SelectedDualCoin != null &&
                        coinKernelVm.CoinKernelProfile.SelectedDualCoin.GetId() == message.Target.GetId())
                    {
                        coinKernelVm.CoinKernelProfile.OnPropertyChanged(nameof(coinKernelVm.CoinKernelProfile.SelectedDualCoin));
                    }
                    if (justAsDualCoin != coinVm.JustAsDualCoin)
                    {
                        OnPropertyChanged(nameof(MainCoins));
                    }
                }, location: this.GetType());
                AddEventPath <CoinIconDownloadedEvent>("下载了币种图标后", LogEnum.DevConsole,
                                                       action: message => {
                    try {
                        if (string.IsNullOrEmpty(message.Target.Icon))
                        {
                            return;
                        }
                        string iconFileFullName = SpecialPath.GetIconFileFullName(message.Target);
                        if (string.IsNullOrEmpty(iconFileFullName) || !File.Exists(iconFileFullName))
                        {
                            return;
                        }
                        if (_dicById.TryGetValue(message.Target.GetId(), out CoinViewModel coinVm))
                        {
                            try {
                                coinVm.IconImageSource = new Uri(iconFileFullName, UriKind.Absolute).ToString();
                            }
                            catch (Exception e) {
                                File.Delete(iconFileFullName);
                                Logger.ErrorDebugLine(e);
                            }
                        }
                    }
                    catch (Exception e) {
                        Logger.ErrorDebugLine(e);
                    }
                }, location: this.GetType());
                Init();
#if DEBUG
                var elapsedMilliseconds = NTStopwatch.Stop();
                if (elapsedMilliseconds.ElapsedMilliseconds > NTStopwatch.ElapsedMilliseconds)
                {
                    Write.DevTimeSpan($"耗时{elapsedMilliseconds} {this.GetType().Name}.ctor");
                }
#endif
            }