添加在MVVM多个引脚来Xamarin.Maps

问题描述 投票:0回答:1

我有问题想了解如何引脚添加到MVVM模式的地图。 (I可以在视图中的多个地图)。

这里是我的ViewModel代码。

<flv:FlowListView SeparatorVisibility="Default" HasUnevenRows="True" FlowItemTappedCommand="{Binding ItemTappedCommand}" FlowLastTappedItem="{Binding LastTappedItem}" FlowColumnMinWidth="600" FlowItemsSource="{Binding DashboardWidgets}" >
    <flv:FlowListView.FlowColumnTemplate>
                <StackLayout BackgroundColor="White" Margin="1">
                    <!--Map Widgets -->
                    <StackLayout BackgroundColor="{Binding bgcolor}" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand" IsVisible="False" >
                        <StackLayout.Triggers>
                            <DataTrigger TargetType="StackLayout" Binding="{Binding resultsettype}" Value="map">
                                <Setter Property="IsVisible" Value="True"/>
                            </DataTrigger>
                        </StackLayout.Triggers>
                        <StackLayout Orientation="Vertical"  Padding="4" HeightRequest="400" WidthRequest="600">
                            <maps:Map x:Name="MyMap" IsShowingUser="true" MapType="Hybrid" />
                        </StackLayout>
                    </StackLayout>
                    <!--Image Widgets -->
                    <StackLayout BackgroundColor="{Binding bgcolor}" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand" HeightRequest="200" WidthRequest="600" Padding="4" IsVisible="False">
                        <StackLayout.Triggers>
                            <DataTrigger TargetType="StackLayout" Binding="{Binding resultsettype}" Value="image">
                                <Setter Property="IsVisible" Value="True"/>
                            </DataTrigger>
                        </StackLayout.Triggers>
                        <StackLayout Padding="1" HeightRequest="200" WidthRequest="600">
                            <Image Source="{Binding contentvalue, Converter={StaticResource LocalBase64ToImageConverter}}" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"/>
                        </StackLayout>
                    </StackLayout>
    </flv:FlowListView.FlowColumnTemplate>

我有一个可以容纳一个地图,图像,整数或列表DashboardWidgets的观察的集合。如果它是一个地图,我会在虚拟机中所有的引脚并以某种方式将它们绑定到地图。由于我使用的MVVM模式,我shouldnot访问地图在我的虚拟机,而且我不知道如何做到这一点无论是。

所以,我至今都试图扩展地图,使引脚可绑定但没有成功。

public class ExtendedMap : Map
    {
     public static readonly BindableProperty PinsProperty = BindableProperty.Create(nameof(MultiPins), typeof(IList<Pin>), typeof(ExtendedMap), new List<Pin>());
     public List<Pin> MultiPins
         {
            get {
                        var val = (List<Pin>)GetValue(PinsProperty);
                        return new List<Pin>();
                         }
                    set => SetValue(PinsProperty, value);
                    }                
                }      
        }

我试图创建一个内容查看结合它的内容属性里面的VM,但没有运气。

XAML:都尝试。无工作。

<contentview content={binding widgetResult } />
<contentview content={binding mapresult} />

VM:VM实现的PropertyChanged。

var map = new Map(MapSpan.FromCenterAndRadius(new Position(37, -122), Distance.FromMiles(0.3)))
                {
                IsShowingUser = true,
                HeightRequest = 100,
                WidthRequest = 960,
                VerticalOptions = LayoutOptions.FillAndExpand
                };
        (await GetMapPins(widget.filter)).ForEach(f => map.Pins.Add(f));
        widget.widgetResult = new ContentView
                {
                    Content = map
                 };
        widget.mapresult = map;
xamarin xamarin.forms maps
1个回答
0
投票

这是错误的:

public static readonly BindableProperty PinsProperty = BindableProperty.Create(nameof(MultiPins), typeof(IList<Pin>), typeof(ExtendedMap), new List<Pin>());

public List<Pin> MultiPins {...}

该BindableProperty的名称必须是物业本身的名称,在这种情况下,“MultiPins”,再加上“财产”,所以BindableProperty名称必须为“MultiPinsProperty”,例如:

public static readonly BindableProperty MultiPinsProperty = BindableProperty.Create(nameof(MultiPins), typeof(IList<Pin>), typeof(ExtendedMap), new List<Pin>());

public List<Pin> MultiPins {...}

https://docs.microsoft.com/en-us/xamarin/xamarin-forms/xaml/bindable-properties#creating-a-property

为绑定属性的命名约定是绑定属性标识符必须在创建方法中指定的属性名称,如同“属性”追加到它。

© www.soinside.com 2019 - 2024. All rights reserved.