[英]Show map from VM
I try to show on a ContentPage an ObservableCollection of type Activity (class I created).我尝试在 ContentPage 上显示Activity类型的ObservableCollection (我创建的类)。 This one contains a Title (string) and a Map (xamarin.forms.maps) :
这个包含一个标题(字符串)和一个Map(xamarin.forms.maps) :
Map myMap = new Map();
myMap.IsShowingUser = true;
activity.Map = myMap;
obActivities.Add(activity);
Here is my XAML:这是我的 XAML:
<ListView x:Name="listActivities" HorizontalOptions="StartAndExpand" VerticalOptions="FillAndExpand"
SeparatorColor="LightGray" SeparatorVisibility="Default" HasUnevenRows="True"
ItemsSource="{Binding obActivities}" CachingStrategy="RecycleElement"
ItemSelected="ListActivities_ItemSelected" SelectedItem="{Binding selectedActivity,Mode=TwoWay}" >
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Horizontal" Padding="5" VerticalOptions="FillAndExpand">
<ContentView Content="{Binding Map}"></ContentView>
<Label Text="{Binding Title}" FontSize="12" TextColor="Gray"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
The Title is well displayed .标题显示得很好。
I tried a lot of things (may be everything I can ^^) to show the Map but nothing works .我尝试了很多东西(可能是我能做的一切^^)来展示Map 但没有任何效果。 The last thing I tried before this post...
<ContentView Content="{Binding Map}"></ContentView>
but no result.我在这篇文章之前尝试的最后一件事......
<ContentView Content="{Binding Map}"></ContentView>
但没有结果。
EDIT: I also tried to add my map control onto the XAML like:编辑:我还尝试将我的 map 控件添加到 XAML 上,例如:
<map:Map>
<map:Map.MapElements>
<map:Polyline>
<map:Polyline.Geopath>
</map:Polyline.Geopath>
</map:Polyline>
</map:Map.MapElements>
</map:Map>
But I don't know where I have to add the polyline's position from my VM.但我不知道我必须在哪里从我的虚拟机中添加折线的 position。 May be you can help me to find the right XAML...
也许你能帮我找到合适的 XAML...
.xaml.cs
file..xaml.cs
文件后面的页面代码中。 There is still a "model" for each item ("Activity"), but no longer a ViewModel corresponding to the Page itself. NOTE: This is an "impure" approach, as Activity is a mixture of model and UI information.注意:这是一种“不纯”的方法,因为 Activity 是 model 和 UI 信息的混合体。 Nevertheless, it is simpler code to do it this way.
尽管如此,这样做的代码更简单。 So I won't attempt to show a "pure" solution.
所以我不会试图展示一个“纯”的解决方案。 In a simple case, the complications of doing so outweigh the "maintenance" benefit [easier to make changes without causing a bug] of keeping view and model separate.
在一个简单的情况下,这样做的复杂性超过了保持视图和 model 分开的“维护”好处[更容易进行更改而不会导致错误]。 If this causes some problem later, refactor at that time.
如果这会导致以后出现问题,请在那时重构。
Then BindingContext becomes the page itself:然后 BindingContext 成为页面本身:
public MyPage() {
InitializeComponent();
InitMyData();
BindingContext = this;
}
public ObservableCollection<Activity> obActivities { get; set; } = new ObservableCollection<Activity>();
void InitMyData() {
Map myMap = new Map();
myMap.IsShowingUser = true;
activity.Map = myMap;
obActivities.Add(activity);
}
The Map may lack a height when it is created. Map 在创建时可能缺少高度。
StackLayout
may have given it "0" height by default.默认情况下,
StackLayout
可能已经给它“0”高度。 Specify some height:指定一些高度:
Map myMap = new Map(); myApp.HeightRequest = 100;
Having a "hardcoded" height (eg 100
) also helps speed up the initial layout of the page.具有“硬编码”高度(例如
100
)也有助于加快页面的初始布局。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.