MVVM跨域绑定代码隐藏
MVVMCross Field Binding CodeBehind
我正在尝试使用 GoogleMaps nuget 包将 MapLongClicked 事件的 EventArgs 结果绑定到 Xamarin Forms 上的 ViewModel 上的 属性 "Location"。
问题是我只能访问 XAML 代码后面的那些 EventArgs。
当我使用 MVVMCross 时,我发现 something 关于字段绑定,但它不适用于代码隐藏。
我的 MapPage 现在看起来像这样:
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class MapPage : MvxContentPage
{
public MapPage()
{
InitializeComponent();
map.UiSettings.ZoomControlsEnabled = false;
map.MapLongClicked += (sender, e) =>
{
//((MapViewModel)map.BindingContext).Location = new Microsoft.Azure.Documents.Spatial.Point(e.Point.Longitude, e.Point.Latitude);
var pin = new Pin
{
Type = PinType.SearchResult,
Position = new Position(e.Point.Latitude, e.Point.Longitude),
Label = string.Format(e.Point.Latitude.ToString("0.000") + " / " + e.Point.Longitude.ToString("0.000"))
};
map.Pins.Add(pin);
};
}
}
XAML:
<mvx:MvxContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:mvx="clr-namespace:MvvmCross.Forms.Views;assembly=MvvmCross.Forms"
xmlns:maps="clr-namespace:Xamarin.Forms.GoogleMaps;assembly=Xamarin.Forms.GoogleMaps"
xmlns:behaviors="clr-namespace:XamFormsMaps.Core.Behaviors;assembly=XamFormsMaps.Core"
x:Class="XamFormsMaps.Core.Pages.MapPage">
<ContentPage.Content>
<StackLayout>
<maps:Map
x:Name="map"
WidthRequest="320"
HeightRequest="200"
IsShowingUser="true"
MapType="Hybrid">
<maps:Map.Behaviors>
<behaviors:MapBehavior ItemsSource="{Binding Parkings}" />
</maps:Map.Behaviors>
</maps:Map>
</StackLayout>
</ContentPage.Content>
</mvx:MvxContentPage>
我试过评论选项,但它给我一个转换错误,所以我没有更多的想法。
有谁知道如何将 XAML 代码后面的变量发送到 ViewModel?
这解决了我的问题:
((MapViewModel)map.BindingContext).Location = new Microsoft.Azure.Documents.Spatial.Point(e.Point.Longitude, e.Point.Latitude);
var pin = new Pin
确保 map.BindingContext 在运行时是 MapViewModel。
我正在尝试使用 GoogleMaps nuget 包将 MapLongClicked 事件的 EventArgs 结果绑定到 Xamarin Forms 上的 ViewModel 上的 属性 "Location"。 问题是我只能访问 XAML 代码后面的那些 EventArgs。 当我使用 MVVMCross 时,我发现 something 关于字段绑定,但它不适用于代码隐藏。 我的 MapPage 现在看起来像这样:
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class MapPage : MvxContentPage
{
public MapPage()
{
InitializeComponent();
map.UiSettings.ZoomControlsEnabled = false;
map.MapLongClicked += (sender, e) =>
{
//((MapViewModel)map.BindingContext).Location = new Microsoft.Azure.Documents.Spatial.Point(e.Point.Longitude, e.Point.Latitude);
var pin = new Pin
{
Type = PinType.SearchResult,
Position = new Position(e.Point.Latitude, e.Point.Longitude),
Label = string.Format(e.Point.Latitude.ToString("0.000") + " / " + e.Point.Longitude.ToString("0.000"))
};
map.Pins.Add(pin);
};
}
}
XAML:
<mvx:MvxContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:mvx="clr-namespace:MvvmCross.Forms.Views;assembly=MvvmCross.Forms"
xmlns:maps="clr-namespace:Xamarin.Forms.GoogleMaps;assembly=Xamarin.Forms.GoogleMaps"
xmlns:behaviors="clr-namespace:XamFormsMaps.Core.Behaviors;assembly=XamFormsMaps.Core"
x:Class="XamFormsMaps.Core.Pages.MapPage">
<ContentPage.Content>
<StackLayout>
<maps:Map
x:Name="map"
WidthRequest="320"
HeightRequest="200"
IsShowingUser="true"
MapType="Hybrid">
<maps:Map.Behaviors>
<behaviors:MapBehavior ItemsSource="{Binding Parkings}" />
</maps:Map.Behaviors>
</maps:Map>
</StackLayout>
</ContentPage.Content>
</mvx:MvxContentPage>
我试过评论选项,但它给我一个转换错误,所以我没有更多的想法。
有谁知道如何将 XAML 代码后面的变量发送到 ViewModel?
这解决了我的问题:
((MapViewModel)map.BindingContext).Location = new Microsoft.Azure.Documents.Spatial.Point(e.Point.Longitude, e.Point.Latitude);
var pin = new Pin
确保 map.BindingContext 在运行时是 MapViewModel。