简体   繁体   中英

can't get MapControl element by name

I just discovering the new way to dev Windows app (UWP) and I am facing an issue I've never got before.

I've got this map control :

<HubSection x:Uid="xxx" Header="Map">
  <DataTemplate>
    <Maps:MapControl x:Name="myMapControl" MapServiceToken="XXXXXXXX" Width="700" Height="655" />
  </DataTemplate>
</HubSection>

and in my code behind,

myMapControl.MapElements.Add(mapIcon1);

But I got the error saying that "myMapControl" is not defined in the current scope. If I give a "x:Name" to another element (grid for example) it works.

How can I get my MapControl in my C# code ? Or, how can I bind my mapIcon to the map ?

Thanks for your answers

In the case of DataTemplates, the content of them are not directly in the Page Visual Tree untils it is loaded, so one way, in case you have just few controls to get the MapControl in the Page is the following:

  <Hub>
        <HubSection>
            <DataTemplate>
                <Maps:MapControl x:Name="MyMapControl" Loaded="MyMapControl_Loaded"/>
            </DataTemplate>
        </HubSection>
    </Hub>

And then in the Loaded event just the following:

 private MapControl myMapControl;
    private void MyMapControl_Loaded(object sender, RoutedEventArgs e)
    {
        myMapControl = (sender as MapControl);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM