繁体   English   中英

MVVM跨域绑定代码背后

[英]MVVMCross Field Binding CodeBehind

我正在尝试使用GoogleMaps nuget包将MapLongClicked事件的EventArgs结果绑定到Xamarin Forms的ViewModel上的属性“ Location”。 问题是我只能在后面的XAML代码中访问那些EventArgs。 当我使用MVVMCross时,我发现了一些有关字段绑定的信息,但这并不是针对后面的代码的。 我的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。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM