问题描述
我在 UserControl1.xaml 文件中有一个 Bing Map 元素:
<UserControl x:Class="@R_136_502[email protected]"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:m="clr-namespace:Microsoft.Maps.MapControl.WPF;assembly=Microsoft.Maps.MapControl.WPF">
<Grid>
<m:Map CredentialsProvider="Gr8GooglyMoogly" x:Name="myMap" />
</Grid>
</UserControl>
我可以像这样从它所在的 Form1 访问它:
this.userControl11.myMap.Mode = new RoadMode();
...但是当我尝试从另一个表单访问它时,这些尝试都不起作用:
userControl11.myMap.Children.Add(pin); // does not exist in the current context
Form1.userControl11.myMap.Children.Add(pin); // inaccessible due to its protection level
UserControl1.myMap.Children.Add(pin); // object reference is required for the static field,...
更新
使用 Reza 的评论将地图的 Modifier 属性从 Private 更改为 Public,并利用 the link provided 中显示的方法,以下工作:
var frmMain = new Form1();
frmMain.userControl11.myMap.Children.Add(pin);
更新 2
Reza 的想法很完美。这是我测试它以验证的方式:
在“表格 2”(mdlDlgFrm_AddNewLocation)中:
// to access map on main form (Form1)
private Form1 frmMain;
// second constructor so as to access map on main form (to add pushpins)
public mdlDlgFrm_AddNewLocation(Form1 f1)
{
InitializeComponent();
this.frmMain = f1;
// test
Addpushpin("blaJustATest");
}
private void Addpushpin(string fullAddress)
{
pushpin pin = new pushpin();
// "brute-forcing" the coordinates for this test
pin.Location = new Location(37.1481402218342,-119.644248783588); // Interesting location: out in the "boondocks" between Oakhurst and AuBerry
this.frmMain.userControl11.myMap.Children.Add(pin);
}
...以及从主表单(表单 1)调用的“表单 2”:
private void addLocationToolStripMenuItem_Click(object sender,EventArgs e)
{
mdlDlgFrm_AddNewLocation frmAddNewLocation = new mdlDlgFrm_AddNewLocation(this);
frmAddNewLocation.ShowDialog(this);
frmAddNewLocation.dispose()
}
解决方法
当您创建新表单时,您必须将当前表单作为参数发送给新表单的创建者,在那里您可以使用您提交的实例对该实例进行更改。
,除了我在 linked post 中已经解释并在其他帖子中提到的所有选项(包括我在评论中提到的选项)之外,您还可以在此处考虑以下选项:>
- 如果您使用
const LineChart = () => { ... function toggleLiveData() { if (!intervalRef) { setIntervalRef( setInterval(() => { if (chartComponent.current) { chartComponent.current.chart.series[0].addPoint( Math.random(),true,false ); } },500) ); } else { clearInterval(intervalRef); setIntervalRef(null); } } return ( <div> <HighchartsReact ref={chartComponent} highcharts={Highcharts} options={chartOptions} /> </div> ); };
来显示ShowDialog
,则不需要对 Map 或 Form1 的引用。只需从Form2
返回 Pushpin 并使用它。 - OR 只需将依赖项
Form2
传递给您的第二个表单。 (无需公开用户控件或传递整个 form1)。
示例 1 - 返回图钉
如果您使用 ShowDialog 来显示 Form2,则在 Map
中不需要 Map
。只需创建 Form2
并将其返回给 Pushpin
并在那里使用它。
-
在
Form1
中,定义一个Form2
属性:Pin
-
在
//using Microsoft.Maps.MapControl.WPF; //... public Pushpin Pin {get; set;}
中,当您要创建Form2
时,将其分配给Pushpin
属性:Pin
-
然后在
//... this.Pin = new Pushpin(){Location = location}; this.DialogResult = DialogResult.OK;
中,当您想显示Form1
时:Form2
示例 2 - 传递地图
只需将依赖项 Map 传递给您的第二个表单。您不需要将用户控件设为公开或不需要传递整个 form1:
-
更改
using(var f2 = new Form2()) { if(f2.ShowDialog() == DialogResult.OK) { this.userControl11.myMap.Children.Add(f2.Pin); } }
构造函数以接受 Map:Form2
-
在
//using Microsoft.Maps.MapControl.WPF; //... private Map map; public Form2(Map map) { InitializeComponent(); this.map = map; }
中,当您想显示Form1
时:Form2
-
在
var map = this.userControl11.myMap; using(var f2 = new Form2(map)) f2.ShowDialog();
中,当您想使用地图时:Form2