简体   繁体   English

XAML data.binding-如何将方法中的属性绑定到视图中的经度/纬度?

[英]XAML data.binding - how can I bind properties in my method to the longitude/latitude in my view?

I'm using GeoLocator to find out my position. 我正在使用GeoLocator找出我的位置。 This is the method: 这是方法:

private async void GetCurrentLocation()
{
    Geolocator locationFinder = new Geolocator
    {
        DesiredAccuracyInMeters = 50,
        DesiredAccuracy = PositionAccuracy.Default
    };

    try
    {
        Geoposition currentLocation = await  locationFinder.GetGeopositionAsync(
            maximumAge: TimeSpan.FromSeconds(120),
            timeout: TimeSpan.FromSeconds(10));
        String longitude = currentLocation.Coordinate.Longitude.ToString("0.00");
        String latitude = currentLocation.Coordinate.Latitude.ToString("0.00");
        MyTextBlock.Text = "Long: " + longitude + "Lat: " + latitude;
    }
    catch (UnauthorizedAccessException)
    {
        MyTextBlock.Text = "some message";
    }
}

Now, I'm my view I have this code (here with hard-coded values that I would like to replace with longitude/latitude from the above method). 现在,我认为我有这段代码(这里是硬编码的值,我想用上述方法中的经度/纬度替换)。

<bm:Map ZoomLevel="7.5" Credentials="my key" x:Name="myMap">
    <bm:Map.Center>
        <bm:Location Latitude="48" Longitude="-122.580489" />
    </bm:Map.Center>
</bm:Map>

I'm very new to this kind of developing and would like to know how I can bind the properties in the method to the long/lat in my view. 我对这种开发方式非常陌生,想知道如何将方法中的属性绑定到我认为的long / lat上。 Thank you! 谢谢!

This would require some sort of object (usually called a ViewModel) from which your Map control would get its DataContext . 这将需要某种对象(通常称为ViewModel), Map控件将从该对象获取其DataContext (DataContext moves down the visual tree, from parent to child, unless explicitly set on any child control.) The ViewModel exposes information you'd like the view (your Window/other visible control) to visually represent somehow to the user. (除非在任何子控件上明确设置,否则DataContext从父级到子级在可视树中向下移动。)ViewModel公开您希望视图(您的Window /其他可见控件)以某种方式向用户直观表示的信息。

Note that one of the crucial components of ViewModels in the MVVM style for WPF is that it implements INotifyPropertyChanged . 请注意,WPF的MVVM样式的ViewModels的关键组件之一是它实现了INotifyPropertyChanged This interface allows an object to signal to the UI that the a value which is bound in the above fashion has changed, and the UI should be updated to reflect that. 此接口允许对象向UI发出信号,通知以上述方式绑定的a值已更改,并且应该更新UI以反映该情况。

Here's a really simple implementation of a ViewModel for your purposes: 这是一个针对您目的的ViewModel的非常简单的实现:

// uses System.ComponentModel
public class YourViewModel : INotifyPropertyChanged
{
    private double longitude;
    public double Longitude
    { 
        get { return longitude; }
        set
        {
            longitude = value;
            NotifyPropertyChanged("Longitude");
        }
    } 

    private double latitude;
    public double Latitude
    { 
        get { return latitude; }
        set
        {
            latitude = value;
            NotifyPropertyChanged("Latitude");
        }
    } 

    public event PropertyChangedEventHandler PropertyChanged;
    protected void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

This exposes a Longitude property and a Latitude property which you can use for binding in your XAML. 这将公开一个经度属性和一个纬度属性,可用于在XAML中进行绑定。 If you're going to do true MVVM, those properties should probably be contained within a model object, and the ViewModel should in turn expose a collection of model objects to the View. 如果您要执行真正的MVVM,则这些属性可能应包含在模型对象中,并且ViewModel应该反过来向View公开模型对象的集合。 As it stands, this is basically as simple an example as possible. 就目前而言,这基本上是一个尽可能简单的示例。

In general, you'd just use a simple binding to connect a control's property to your ViewModel's property, like so: 通常,您只需要使用一个简单的绑定将控件的属性连接到ViewModel的属性,如下所示:

<TextBlock Text="{Binding Longitude}" />

where the TextBlock 's DataContext had been set to an instance of YourViewModel. 其中TextBlock的DataContext已设置为YourViewModel的实例。

However, it looks like the Bing Map object isn't a FrameworkElement, so it doesn't have a DataContext property, and binding is a little more complicated because it needs StaticResource s. 但是,看起来Bing Map对象不是FrameworkElement,因此它没有DataContext属性,并且绑定要复杂一些,因为它需要使用StaticResource I don't really have the capacity to test out the Bing maps API, but this link on binding to non-FrameworkElements should help. 我实际上没有能力测试Bing maps API,但是有关绑定到非FrameworkElements的链接应该会有所帮助。

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

相关问题 如何绑定到绑定所在的窗口? - How can I bind to the window my binding is in? 如何在 ac# windows 窗体中获取我的特定位置(纬度和经度)? - How can I get my specific location (latitude and longitude) in a c# windows form? 无法成功将ViewModel数据公开给View以进行XAML绑定 - Failing to successfully expose my ViewModel data to my View for XAML binding 如何让我的MultiSelectList绑定到我的数据模型? - How can I get my MultiSelectList to bind to my data model? 如何将我的自定义用户控件及其自定义视图 model 包含到我的 xaml 视图中? - How can I include my custom user control with its custom view model into my xaml view? 无论如何,我可以将两个值绑定到我的XAML中吗? - Is there anyway I can bind two values into my XAML? 在Umbraco中,如何将对象的通用列表绑定到我的视图? - In Umbraco, how can I bind a generic list of Objects to my View? 如何将此方法作为扩展方法添加到我的类的属性中? - How can I add this method as an extension method to properties of my class? 必应地图经度纬度数据绑定 - Bing Map Longitude Latitude Data Binding 在 XAML 中调用时,如何使我的自定义依赖项属性排序到顶部? - How can I make my custom dependency properties sorted to the top when called in XAML?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM