How can I reference my Bing Maps element from another form?

Viewed 192

I've got a Bing Map element in UserControl1.xaml file:

<UserControl x:Class="MyMaps.UserControl1"
             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>

I can access it like so from Form1, on which it sits:

this.userControl11.myMap.Mode = new RoadMode();

...but when I try to access it from another form, none of these attempts work:

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, ...

How can I get a handle on the UserControl from another form?

UPDATE

Using Reza's comment to change the Modifier property of the map from Private to Public, and utilizing the method shown at the link provided, the following works:

var frmMain = new Form1();
frmMain.userControl11.myMap.Children.Add(pin);

UPDATE 2

Reza's idea worked perfectly. This is how I tested it to verify:

In "Form 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);
}

...and "Form 2" being invoked from the main form (Form 1):

private void addLocationToolStripMenuItem_Click(object sender, EventArgs e)
{
    mdlDlgFrm_AddNewLocation frmAddNewLocation = new mdlDlgFrm_AddNewLocation(this);
    frmAddNewLocation.ShowDialog(this);
    frmAddNewLocation.Dispose()
}
2 Answers

When you create a new form, you must send the current form as a parameter to the creator of the new form, where you can make changes to that instance using the instance you submitted.

In addition to all the options that I've already explained in the linked post and has been told in other posts, including the one that I mentioned in the comments, you can consider the following options here:

  1. If you use ShowDialog to show Form2, you don't need a reference to Map or to Form1. Just return Pushpin from Form2 and use it.
  2. OR Just pass the dependency, the Map to your second form. (No need to make the usercontrol public or pass the whole form1).

Example 1 - Return Pushpin

If you use ShowDialog to show Form2, you don't need a Map in Form2. Just create the Pushpin and return it back to Form1 and use it there.

  1. In Form2, define a Pin property:

    //using Microsoft.Maps.MapControl.WPF;
    //...
    public Pushpin Pin {get; set;}
    
  2. In Form2, when you want to create the Pushpin, assign it to Pin property:

    //...
    this.Pin = new Pushpin(){Location = location};
    this.DialogResult = DialogResult.OK;
    
  3. Then in Form1 when you want to show Form2:

    using(var f2 = new Form2())
    {
        if(f2.ShowDialog() == DialogResult.OK)
        {
            this.userControl11.myMap.Children.Add(f2.Pin);
        }
    }
    

Example 2 - Pass the Map

Just pass the dependency, the Map to your second form. You don't need to make the user control public or you don't need to pass the whole form1:

  1. Change Form2 constructor to accept a Map:

    //using Microsoft.Maps.MapControl.WPF;
    //...
    private Map map;
    public Form2(Map map)
    {
        InitializeComponent();
        this.map = map;
    }
    
  2. In Form1, when you want to show Form2:

    var map = this.userControl11.myMap;
    using(var f2 = new Form2(map))
        f2.ShowDialog();
    
  3. In Form2, when you want to use the map:

    //...
    var pin = new Pushpin(){Location = locatin};
    map.Children.Add(pin);
    
Related