第525篇-Prism学习系列1_Event Aggregator

本文深入探讨了Prism框架中EventAggregator的使用方法,讲解了如何在WPF或Silverlight应用中实现事件的全局管理。从初始化事件到ViewPresenter的注册,再到事件的发布与订阅,提供了详细的步骤与代码示例。

在开发WPF或是Silverlight应用程序的过程中,Prism是一种非常好的框架,今天和大家一起学习Prism中的Event Aggregator,应该机制组织一个全局事件,在应用程序任何地方,只要Subscribe事件,再Publish事件,即可完成.NET事件的“全局管理”.

以下是相关注意点:

1 要有一个Container有两种方法后成,一种是直接导入

  public IUnityContainer Container; //{ get; set; }
        [ImportingConstructor]
        public Shell(IUnityContainer Container)


2 得到ViewModel的时候,得到Container:

  public MainViewmodel viewModel; //{ get; set; }
        [ImportingConstructor]
        public Shell(MainViewModel viewmodel)

 在MainViewModel中得到Container.

3 用Resolve方法出来的View, 默认的构造方法不会执行,所以应该:

 public RightView(RightViewModel vm)
        {
            InitializeComponent();
            viewModel = vm;
            // 在View中设值的时候,就初始化订阅事件
            viewModel.View = this;
         }

4 正常情况下,应该通过在Module中用RegionManager把view注入到Shell中的。不知道为什么RegionManager加入没有用?
 Module A: 

public void Initialize()

{

//var view = Container.Resolve<LeftView>();

//RegionManager.Regions["LeftRegion"].Add(view);

}

shell:
   <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <ItemsControl cal:RegionManager.RegionName="LeftRegion"  Grid.Column="0" />
        <ItemsControl cal:RegionManager.RegionName="RightRegion"  Grid.Column="1" />
    </Grid>


但是本例用了直接的方法:在Shell后台直接加上了:

 public IUnityContainer Container; //{ get; set; }
        [ImportingConstructor]
        public Shell(IUnityContainer Container)
        {
            InitializeComponent();
            this.Container = Container;
            var view = this.Container.Resolve<LeftView>();
            panel.Children.Add(view);
            var view2 = this.Container.Resolve<RightView>();
            var view3 = this.Container.Resolve<RightView>();
            view2.SubscribeEvents();
            view3.SubscribeEvents();
            panel.Children.Add(view2);
            panel.Children.Add(view3);
        }


5 Module A中的事件通过,在设置View和后台的ViewPresenter联结的时候,就在ViewPresenter中进行了注册:

 [ImportingConstructor]
        public LeftView(SelectViewModel viewModel)
        {
            InitializeComponent();
            this.AddButton.Click += AddCustomer;
          
            this.viewModel = viewModel;
            // 在View中设值的时候,就初始化订阅事件
            viewModel.View = this;
        }


  ViewPresenter:

 private ISelectView _view;
       /// <summary>
       /// 在View中设值的时候,就初始化订阅事件
       /// </summary>
       public ISelectView View
       {
           get{return _view ;}
           set {
               _view = value;
               View.ButtonClick += new EventHandler(View_ButtonClick);
           }
       }


6 发布Event Aggregator事件:

 void View_ButtonClick(object sender, EventArgs e)
       {
           Customer customer = new Customer() { CustomerId = View.CustomerId };
           if (!string.IsNullOrEmpty(customer.CustomerId))
           {
               this.eventAggregator.GetEvent<CustomerAddedEvent>().Publish(customer);
           }
       }

注册事件:

 public void Subscribe()
        {
            this.eventAggregator.GetEvent<CustomerAddedEvent>().Subscribe(OnSubscribeEvent);
        }

Demo下载地址:https://skydrive.live.com/#cid=6B286CBEF1610557&id=6B286CBEF1610557!683

转载于:https://www.cnblogs.com/shanghaijimzhou/archive/2013/03/31/2991550.html

你遇到的问题是:**所有模型的磁异常值都为 0.0 nT**,这说明正演计算没有正确执行。问题的根本原因在于: > **`mesh` 在定义之前就被使用了!** 你在 `create_modelX` 函数中使用了 `mesh.cell_centers`,但此时 `mesh` 还没有被创建! --- ### ✅ 错误顺序(当前代码逻辑错误): 1. 定义 `create_model1...` 等函数(里面用到了 `mesh`) 2. 调用这些函数并存入 `all_models` 列表 3. **之后才定义 `mesh = TensorMesh(...)`** → 所以当函数内部访问 `mesh.cell_centers` 时,其实 `mesh` 是未定义或旧值,导致判断失效,`indices` 全为 False,模型全为 0 --- ### ✅ 正确顺序应该是: 1. 先创建 `mesh` 2. 再调用模型生成函数(此时 `mesh` 已存在) --- ### ✅ 修改后的完整代码(修复关键顺序错误): ```python import numpy as np import matplotlib.pyplot as plt from discretize import TensorMesh import os print("=== Coal Fire Magnetic Forward Modeling Dataset ===") output_dir = "Magnetic_Results" os.makedirs(output_dir, exist_ok=True) print(f"Results will be saved to: {output_dir}") # =============== 必须先创建 mesh!=============== mesh = TensorMesh([ np.ones(100) * 10, np.ones(1) * 10, np.ones(80) * 5 ], x0=['C', 'C', 0]) print(f"Mesh created: {mesh.nC} cells") # =============== 定义接收点位置 ================== x_locations = np.linspace(-150, 150, 61) y_locations = np.zeros_like(x_locations) z_locations = np.ones_like(x_locations) * 10 receiver_locations = np.c_[x_locations, y_locations, z_locations] print("Survey system setup completed: 61 stations") # =============== 模型定义函数(现在可以安全使用 mesh)=============== def create_model1_horizontal_slab(): model = np.zeros(mesh.nC) centers = mesh.cell_centers indices = ( (centers[:, 0] >= -40) & (centers[:, 0] <= 40) & (centers[:, 2] >= -25) & (centers[:, 2] <= -15) & (centers[:, 1] >= -5) & (centers[:, 1] <= 5) ) model[indices] = -0.08 return model, "01_Horizontal_Slab" def create_model2_dipping_slab(): model = np.zeros(mesh.nC) centers = mesh.cell_centers angle = np.radians(30) rotated_x = (centers[:, 0] - 10) * np.cos(angle) - (centers[:, 2] + 20) * np.sin(angle) rotated_z = (centers[:, 0] - 10) * np.sin(angle) + (centers[:, 2] + 20) * np.cos(angle) indices = ( (rotated_x >= -30) & (rotated_x <= 30) & (rotated_z >= -5) & (rotated_z <= 5) & (centers[:, 1] >= -5) & (centers[:, 1] <= 5) ) model[indices] = -0.09 return model, "02_Dipping_Slab" def create_model3_rectangular_prism(): model = np.zeros(mesh.nC) centers = mesh.cell_centers indices = ( (centers[:, 0] >= -30) & (centers[:, 0] <= 30) & (centers[:, 2] >= -30) & (centers[:, 2] <= -10) & (centers[:, 1] >= -5) & (centers[:, 1] <= 5) ) model[indices] = -0.085 return model, "03_Rectangular_Prism" def create_model4_two_anomalies(): model = np.zeros(mesh.nC) centers = mesh.cell_centers indices1 = ( (centers[:, 0] >= -60) & (centers[:, 0] <= -30) & (centers[:, 2] >= -20) & (centers[:, 2] <= -10) ) indices2 = ( (centers[:, 0] >= 30) & (centers[:, 0] <= 60) & (centers[:, 2] >= -45) & (centers[:, 2] <= -35) ) model[indices1] = -0.07 model[indices2] = -0.09 return model, "04_Two_Anomalies" def create_model5_complex_model(): model = np.zeros(mesh.nC) centers = mesh.cell_centers def add_block(cx, cz, width_x, width_z, susc): idx = ( (centers[:, 0] >= cx - width_x/2) & (centers[:, 0] <= cx + width_x/2) & (centers[:, 2] >= cz - width_z/2) & (centers[:, 2] <= cz + width_z/2) & (centers[:, 1] >= -5) & (centers[:, 1] <= 5) ) model[idx] = susc add_block(-50, -18, 20, 10, -0.08) add_block(0, -35, 25, 12, -0.09) add_block(50, -22, 18, 8, -0.07) return model, "05_Complex_Model" # =============== 正演计算函数 ================ def calculate_magnetic_anomaly(mesh, model, receiver_locations): H0 = 50000 incl = np.radians(50) decl = np.radians(-5) l = np.cos(incl) * np.cos(decl) m = np.cos(incl) * np.sin(decl) n = np.sin(incl) anomaly = np.zeros(len(receiver_locations)) for i, rx_loc in enumerate(receiver_locations): total_field = 0.0 for j, cell_center in enumerate(mesh.cell_centers): if abs(model[j]) > 1e-6: r_vec = rx_loc - cell_center r = np.linalg.norm(r_vec) if r > 1.0: dx, dy, dz = r_vec / r dot_product = l*dx + m*dy + n*dz kernel = (3 * dot_product**2 - 1) / r**3 total_field += model[j] * kernel anomaly[i] = H0 * total_field * 1e-7 * 100000 # 单位换算到 nT return anomaly # =============== 开始建模与正演 ================ print("\nStarting forward modeling...") results = {} all_models = [ create_model1_horizontal_slab, create_model2_dipping_slab, create_model3_rectangular_prism, create_model4_two_anomalies, create_model5_complex_model ] for i, model_func in enumerate(all_models): print(f"Progress: {i+1}/{len(all_models)} - Calculating model {i+1}...") model, model_name = model_func() data = calculate_magnetic_anomaly(mesh, model, receiver_locations) results[model_name] = { 'model': model, 'data': data, 'receiver_locations': receiver_locations } print(f" {model_name}: {np.min(data):.1f} ~ {np.max(data):.1f} nT") fig, axes = plt.subplots(1, 2, figsize=(15, 6)) fig.suptitle(f'Magnetic Modeling - {model_name}', fontsize=16) model_2d = model.reshape(mesh.vnC[0], mesh.vnC[1], mesh.vnC[2])[:, 0, :] X, Z = np.meshgrid(mesh.cell_centers_x, mesh.cell_centers_z, indexing='ij') im1 = axes[0].pcolormesh(X, Z, model_2d, cmap='jet', shading='auto') axes[0].set_xlabel('Distance (m)') axes[0].set_ylabel('Depth (m)') axes[0].set_title('Underground Model') axes[0].set_aspect('equal') plt.colorbar(im1, ax=axes[0], label='Susceptibility') axes[0].axhline(y=0, color='k', linestyle='-', linewidth=1) axes[1].plot(receiver_locations[:,0], data, 'b-', linewidth=2, marker='o', markersize=3) axes[1].set_xlabel('Station Position (m)') axes[1].set_ylabel('Magnetic Anomaly (nT)') axes[1].set_title('Surface Magnetic Data') axes[1].grid(True, alpha=0.3) axes[1].fill_between(receiver_locations[:,0], data, alpha=0.3, color='blue') plt.tight_layout() plt.savefig(f'{output_dir}/{model_name}.png', dpi=300, bbox_inches='tight') plt.close(fig) print(f" Saved: {model_name}.png") # =============== 保存数据和报告 ================ print("\nSaving data files...") np.save(f'{output_dir}/all_models_data.npy', results) print("Generating report...") with open(f'{output_dir}/data_report.txt', 'w') as f: f.write("Coal Fire Magnetic Forward Modeling Dataset\n") f.write("="*50 + "\n\n") f.write(f"Generated: 2025-10-06\n") f.write(f"Total Models: {len(all_models)}\n") f.write(f"Stations: {len(receiver_locations)}\n") f.write(f"Survey Range: -150m to 150m\n\n") f.write("Magnetic Anomaly Statistics:\n") f.write("-"*30 + "\n") for name in results: data = results[name]['data'] f.write(f"{name}: {np.min(data):7.1f} ~ {np.max(data):7.1f} nT (Peak: {np.max(np.abs(data)):7.1f} nT)\n") print("="*50) print("🎉 Project Completed!") print(f"✅ Generated {len(all_models)} typical fire zone models") print(f"✅ All results saved to '{output_dir}' folder") print(f"✅ Data file: all_models_data.npy") print(f"✅ Report: data_report.txt") print("\nYou now have a complete dataset for deep learning!") print("- Underground models are your [LABELS]") print("- Surface magnetic data are your [FEATURES]") ``` --- ### 🔍 总结:你原来的问题是 > ❌ 在 `mesh` 创建前就定义了依赖它的函数,并立即调用了它们。 ### ✅ 解决方案: > ✔️ 把 `mesh = TensorMesh(...)` 放在所有模型函数定义**之前**,确保它们能正确访问网格信息。 --- ### ✅ 预期输出示例(类似): ``` 01_Horizontal_Slab: -8.7 ~ 66.7 nT (Peak: 66.7 nT) 02_Dipping_Slab: -9.4 ~ 59.4 nT (Peak: 59.4 nT) ... ``` 现在你的 `data_report.txt` 就会显示真实的非零磁异常值了! ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值