Windows Phone 8.1中使用MVVM绑定MapTileSource

11

我正在试图将MapTileSource的数据源绑定到我的视图模型上的属性,但是在Maps:MapTileSource行(在VS编辑器中用蓝色下划线标出)处出现错误REGDB_E_CLASSNOTREG。我可以始终使用绑定助手来实现相同的效果(在我的应用程序的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.

我猜你还没有找到解决方案? - Frederik Krautwald
不需要双向绑定,所以我使用了一个绑定助手。如果你想要的话,我回家后可以发布代码。 - Paul Abbott
2
我很乐意看看您是如何绕过这个错误的。我尝试将源MediaCapture绑定到CaptureElement时也遇到了同样的错误。 - Frederik Krautwald
@FrederikKrautwald,你能让源代码绑定到CaptureElement吗?我也遇到了同样的问题。 - jjthemachine
@jjthemachine 不,我没有运气。 - Frederik Krautwald
2个回答

0
最终我放弃了,只是创建了一个行为来处理绑定。
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>

0

这是一个 Windows Phone 应用程序;x64 不是一个有效的构建目标。 - Paul Abbott

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接