我正在玩 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,导致异常。
关于如何克服这种情况的任何想法?