【问题标题】:Windows Phone 8.1 MapTileSource binding with MVVMWindows Phone 8.1 MapTileSource 与 MVVM 绑定
【发布时间】:2014-05-19 03:47:28
【问题描述】:

我正在尝试将 MapTileSource 的 DataSource 绑定到我的视图模型上的属性,但我在 Maps:MapTileSource 行上收到错误 REGDB_E_CLASSNOTREG(蓝色下划线是 VS 编辑器)。我总是可以使用绑定助手来实现相同的效果(我需要在我的应用程序的 8.0 版本中)但这似乎应该只是......工作。知道有什么问题吗?

<Maps:MapControl Style="{Binding Path=MapStyle}" Center="{Binding Path=MapCenter, Mode=TwoWay}" ZoomLevel="{Binding Path=ZoomLevel, Mode=TwoWay}" MapServiceToken="">
    <Maps:MapControl.TileSources>
        <Maps:MapTileSource Layer="BackgroundReplacement" DataSource="{Binding Path=BaseLayerDataSource}" />
    </Maps:MapControl.TileSources>
</Maps:MapControl>

我也尝试过仅使用具有相同效果的静态数据源:

<Maps:MapControl Style="{Binding Path=MapStyle}" Center="{Binding Path=MapCenter, Mode=TwoWay}" ZoomLevel="{Binding Path=ZoomLevel, Mode=TwoWay}" MapServiceToken="">
    <Maps:MapControl.TileSources>
        <Maps:MapTileSource Layer="BackgroundReplacement">
            <Maps:MapTileSource.DataSource>
                <Maps:HttpMapTileDataSource UriFormatString="" />
            </Maps:MapTileSource.DataSource>
        </Maps:MapTileSource>
    </Maps:MapControl.TileSources>
</Maps:MapControl>

编辑:我在http://msdn.microsoft.com/en-us/library/windows/apps/xaml/dn632728.aspx 尝试了示例代码,它运行良好,因此很明显 MapTileSource 本身并未取消注册。但这都是代码隐藏,不使用数据绑定,所以对我来说用处不大。

编辑 2:如果我忽略错误并尝试将应用程序部署到手机模拟器,我会在视图的 InitializeComponent() 上得到这个:

An exception of type 'Windows.UI.Xaml.Markup.XamlParseException' occurred in HikePoint.exe but was not handled in user code

WinRT information: Cannot deserialize XBF metadata type list as '%1' was not found in namespace '%0'. [Line: 0 Position: 0]

Additional information: The text associated with this error code could not be found.



Cannot deserialize XBF metadata type list as '%1' was not found in namespace '%0'. [Line: 0 Position: 0]

If there is a handler for this exception, the program may be safely continued.

【问题讨论】:

  • 我猜你还没有找到解决方案?
  • 不,我使用了绑定助手,因为我不需要双向。如果你愿意,我可以在我回家时发布代码。
  • 很高兴看到您如何规避此错误。我在尝试将源 MediaCapture 绑定到 CaptureElement 时遇到同样的错误。
  • @FrederikKrautwald 您是否能够将源绑定到 CaptureElement。我有同样的问题。
  • @jjthemachine 不,我运气不好。

标签: windows-phone-8.1 win-universal-app mvvm-light bing-maps


【解决方案1】:

您的项目平台目标是什么?尝试将其更改为 x64。

Similar Question on SO

【讨论】:

  • 这是一个Windows手机应用程序; x64 不是有效的构建目标。
【解决方案2】:

我最终放弃了,只是做了一个行为来为我处理绑定。

public class TileSourceBehavior : DependencyObject, IBehavior
{
    public DependencyObject AssociatedObject { get; private set; }

    public void Attach(Windows.UI.Xaml.DependencyObject associatedObject)
    {
        var mapControl = associatedObject as MapControl;

        if (mapControl == null)
            throw new ArgumentException("TileSourceBehavior can be attached only to MapControl");

        AssociatedObject = associatedObject;
    }

    public void Detach() { }

    public static readonly DependencyProperty TileSourceProperty =
        DependencyProperty.Register("TileSource", typeof(MapTileSource), typeof(TileSourceBehavior), new PropertyMetadata(null, OnTileSourcePropertyChanged));

    public MapTileSource TileSource
    {
        get { return GetValue(TileSourceProperty) as MapTileSource; }
        set { SetValue(TileSourceProperty, value); }
    }

    private static void OnTileSourcePropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
    {
        var behavior = dependencyObject as TileSourceBehavior;
        var mapControl = behavior.AssociatedObject as MapControl;

        // remove the existing tile source

        var existingTileSource = mapControl.TileSources.FirstOrDefault(t => t.Layer == MapTileLayer.BackgroundReplacement);

        if (existingTileSource != null)
            mapControl.TileSources.Remove(existingTileSource);

        // add the tile source

        behavior.TileSource.Layer = MapTileLayer.BackgroundReplacement;
        mapControl.TileSources.Add(behavior.TileSource);
    }
}

您可以这样使用它,其中TileSource 是您的 ViewModel 上的 MapTileSource 属性。

<Maps:MapControl>
  <i:Interaction.Behaviors>
    <behaviors:TileSourceBehavior TileSource="{Binding Path=TileSource}" />
  </i:Interaction.Behaviors>
</Maps:MapControl>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-10
    • 2015-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-12
    相关资源
    最近更新 更多