How to access existing item inside TabViewItem

Apptacular Apps 386 Reputation points
2020-06-24T20:36:16.647+00:00

Why is it my reference to an existing map control isn't working?

  1. My main page loads another page inside the main page Grid
  2. The loaded page is a TabView containing a few TabViewItems
  3. In 1 of the TabViewItems there is a MapControl
  4. I want to access the MapControl dynamically to add markers to it

I tried using MapControl mapControl = pageMap.MyMapControl; but it's not working. Why?

MainPage.xaml

<Grid x:Name="MainGrid">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <StackPanel Grid.Row="0">
        <TextBlock Text="Hello World" />
    </StackPanel>
    <controls:TabView
        x:FieldModifier="public"
        Grid.Row="1"
        x:Name="MainTabs"
        TabWidthBehavior="Actual"
        CanCloseTabs="False"
        IsCloseButtonOverlay="False"
        CanDragItems="False"
        CanReorderItems="False"
        AllowDrop="False">
    </controls:TabView>
</Grid>

MainPage.xaml.cs

    public sealed partial class MainPage: Page
    {
        public MainPage()
        {
            this.InitializeComponent();

            var mapPage= new MapPage();

            TabView tabView = mainPage.MainTabs;

            var tab1 = new TabViewItem { Header = "Map" };
            tabView.Items.Add(tab1);

            var tab2 = new TabViewItem { Header = "Tab 2" };
            tabView.Items.Add(tab2);

            var tab3 = new TabViewItem { Header = "Tab 3" };
            tabView.Items.Add(tab3);

            tabView.SelectedItem = tab1;

            // 1st tab contents
            var mapPage = new MapPage();
            MapControl mapControl = pageMap.MyMapControl; ??

            tab1.Content = mapPage;

            MainGrid.Children.Add(pageTabs);
        }
    }

Map page

    <Grid x:Name="MapGrid">
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <Maps:MapControl
            Grid.Row="0"
            x:Name="MyMapControl"  
            MapServiceToken="[token]"
            ZoomLevel="8"/>
    </Grid>
Universal Windows Platform (UWP)
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Roy Li - MSFT 33,371 Reputation points Microsoft Vendor
    2020-06-25T02:55:34.287+00:00

    Hello,

    Welcome to Microsoft Q&A!

    May I know what is the pageMap control? I can't find an element called pageMap. And what is the pageTabs control?
    Another thing is that we call object.property to get the property we need. Is MyMapControl a property of pageMap?

    A simple solution for this is that you could create a public MapControl property in the pageMap, so that you could get it when you could get the pageMap property.

    Like this:

    public MapControl theMapControl { get; set; }  
    
    public MapPage()
    {
        this.InitializeComponent();
         theMapControl = MyMapControl;
    }
    

    If you have other questions, please let me know.
    Thank you.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.