首页 > 解决方案 > 如何在不同的 xaml 文件中访问 xaml 属性

问题描述

我有 2 个两个不同的 xaml 文件,例如 about 和 addgeojson。第一个(大约)是:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="myMapApp.Views.AboutPage"
             xmlns:vm="clr-namespace:myMapApp.ViewModels" xmlns:local="clr-namespace:Naxam.Controls.Forms;assembly=Naxam.Mapbox.Forms"
             Title="{Binding Title}">

    <ContentPage.BindingContext>
        <vm:AboutViewModel />
    </ContentPage.BindingContext>

    <StackLayout>
        <local:MapView 
                    x:Name="map" 
                    x:FieldModifier="Public"
                    VerticalOptions="FillAndExpand" 
                    MapStyle="mapbox://styles/mapbox/satellite-v9" 
                    ZoomLevel="3"
                />
    </StackLayout>

</ContentPage>

我想访问 x:Name 是 addgeojson xaml 文件中的映射的 mapview 对象。about.map 不起作用。如何访问地图属性?我需要在其他文件中使用 map.functions。

- 更新 -

现在,我创建了一个视图模型。我使用了一个实例来获取地图的值。我可以从任何地方访问中心、缩放和属性。此外,“customMap2”实际上具有 Functions 属性。但是,除了程序的开头之外,它返回 null。因此,我不能从其他文件中使用这些地图功能。我希望该用户从移动设备中选择一个 geojson 文件并加载到地图。怎么了?

using Naxam.Controls.Forms;
using Naxam.Mapbox;
using System;
using System.Collections.Generic;
using System.Text;
using Xamarin.Forms;

namespace myMapApp.ViewModels
{
    public class mapPage
    {
        private static mapPage _instance = new mapPage();
        public static mapPage Instance { get { return _instance; } }

        public MapView customMap2 = new MapView
        {
            MapStyle = MapStyle.SATELITE,
            VerticalOptions = LayoutOptions.FillAndExpand,
            ZoomLevel = 3,
            Functions = MapFunc
        };


        private static IMapFunctions MapFunc { get; set; }

        public IMapFunctions GetFunc
        {
            get { return MapFunc; }
            set { return; }
        }

    }
}

标签: c#xamarinmapbox

解决方案


推荐阅读