【问题标题】:Using scatterview as a region with Prism throws an exception将 scatterview 用作 Prism 的区域会引发异常
【发布时间】:2009-11-25 13:00:50
【问题描述】:

我正在玩 Surface,我正在尝试使用 scatterview 作为模块区域。

<s:ScatterView cal:RegionManager.RegionName="{x:Static common:RegionNames.MainRegion}"></s:ScatterView>

发生的情况是,当我运行应用程序时,会引发异常。稍作思考,我就到达了异常发生的地方:

DelayedRegionCreationBehavior 尝试创建区域:

protected virtual IRegion CreateRegion(DependencyObject targetElement, string regionName)
{
    try
    {
        // Build the region
        IRegionAdapter regionAdapter = this.regionAdapterMappings.GetMapping(targetElement.GetType());
        IRegion region = regionAdapter.Initialize(targetElement, regionName);

        return region;
    }
    catch (Exception ex)
    {
        throw new RegionCreationException(string.Format(CultureInfo.CurrentCulture, Resources.RegionCreationException, regionName, ex), ex);
    }
}

然后ItemsControlRegionAdapter尝试设置区域目标ItemsSource

protected override void Adapt(IRegion region, ItemsControl regionTarget)
{
    bool itemsSourceIsSet = regionTarget.ItemsSource != null;

    #if !SILVERLIGHT
    itemsSourceIsSet = itemsSourceIsSet || (BindingOperations.GetBinding(regionTarget, ItemsControl.ItemsSourceProperty) != null);
    #endif

    if (itemsSourceIsSet)
    {
        throw new InvalidOperationException(Resources.ItemsControlHasItemsSourceException);
    }

    // If control has child items, move them to the region and then bind control to region. Can't set ItemsSource if child items exist.
    if (regionTarget.Items.Count > 0)
    {
        foreach (object childItem in regionTarget.Items)
        {
            region.Add(childItem);
        }
        // Control must be empty before setting ItemsSource
        regionTarget.Items.Clear();
    }

    regionTarget.ItemsSource = region.Views;
}

scatterview 会触发 ItemsSource 更改的通知,并调用 ItemsControlHelper 类:

internal static bool IsItemsReadOnly(ItemsControl itemsControl)
{
    IList itemsControlItems = GetItemsControlItems(itemsControl);
    if (!itemsControlItems.IsReadOnly)
    {
        return itemsControlItems.IsFixedSize;
    }
    return true;
}

我认为GetItemsControlItems返回null,导致异常。

对如何克服这种情况有什么想法吗?

【问题讨论】:

    标签: prism pixelsense prism-2 scatterview


    【解决方案1】:

    我认为这是 ScatterView 的一个已知问题。我们已经意识到这个问题,并将在未来解决它。如果这是我们看到的同一个问题,它与 ScatterView 的 ItemsSource 是 IList&lt;foo&gt; 或其他一些“通用”列表有关。如果您可以将 ItemSource 更改为简单的 IList(而不是 IList&lt;foo&gt;),我认为这将解决您的问题。

    希望对你有帮助,

    -路易斯·卡布雷拉 软件设计工程师,微软 Surface

    【讨论】:

    • Prism 的 ItemsControlRegionAdapter 类传递给 scatterview 的 ItemsSource 的 region.Views 是一个 ViewsCollection,它是一个通用的 IEnumerable。我将通过更改 Prism 源来测试您的解决方案,然后通知您。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-28
    • 1970-01-01
    • 1970-01-01
    • 2016-07-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多