我的Prism应用程序需要将几个模块中的按钮插入Shell区域.这些按钮将垂直堆叠,如Outlook 2010中的导航按钮(邮件,日历等).我使用自定义控件作为按钮,因此我不需要担心模板 - 我的问题与如果我插入普通单选按钮.
如何设置区域以使按钮垂直堆叠?谢谢你的帮助.
小智 6
在考虑垂直堆叠物品时,StackPanel会立即跳到脑海.不幸的是,Prism不支持StackPanel成为开箱即用的区域.幸运的是,您可以创建RegionAdapter来解决此问题.
Public Class StackPanelRegionAdapter
Inherits RegionAdapterBase(Of StackPanel)
Public Sub New(ByVal behaviorFactory As IRegionBehaviorFactory)
MyBase.New(behaviorFactory)
End Sub
Protected Overrides Sub Adapt(ByVal region As IRegion, ByVal regionTarget As StackPanel)
AddHandler region.Views.CollectionChanged, Sub(sender As Object, e As NotifyCollectionChangedEventArgs)
If e.Action = NotifyCollectionChangedAction.Add Then
For Each element As FrameworkElement In e.NewItems
regionTarget.Children.Add(element)
Next
Else
If e.Action = NotifyCollectionChangedAction.Remove Then
For Each element In e.OldItems
If regionTarget.Children.Contains(element) Then
regionTarget.Children.Remove(element)
End If
Next
End If
End Sub
End Sub
Protected Overrides Function CreateRegion() As Microsoft.Practices.Prism.Regions.IRegion
Return New AllActiveRegion
End Function
End Class
Run Code Online (Sandbox Code Playgroud)
从那里,您只需要在引导程序中的ConfigureRegionAdapterMappings Override中添加映射.
Protected Overrides Function ConfigureRegionAdapterMappings() As Microsoft.Practices.Prism.Regions.RegionAdapterMappings
Dim mappings = MyBase.ConfigureRegionAdapterMappings()
mappings.RegisterMapping(GetType(Grid), Container.Resolve(Of PrismExtensions.GridRegionAdapter))
mappings.RegisterMapping(GetType(StackPanel), Container.Resolve(Of PrismExtensions.StackPanelRegionAdapter))
Return mappings
End Function
Run Code Online (Sandbox Code Playgroud)
编辑:找到我最初从中获取代码的John Papa链接.(在C#中,如果这就是你正在使用的那个)请填写我的棱镜区域
Matt的解决方案在"微软棱镜开发者指南"(V4)第189-191页中有解释.
对于研究此问题的C#开发人员,这里是Matt适配器到C#的转换:
using System.Collections.Specialized;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Practices.Prism.Regions;
namespace FsNoteMaster3.Shell.Views.Utility
{
/// <summary>
/// Enables use of a StackPanel in a Prism region.
/// </summary>
/// <remarks> See stackoverflow.com/questions/4950464/prism-stacking-controls-in-a-region</remarks>
public class StackPanelRegionAdapter : RegionAdapterBase<StackPanel>
{
/// <summary>
/// Default constructor.
/// </summary>
/// <param name="behaviorFactory">Allows the registration of the default set of RegionBehaviors.</param>
public StackPanelRegionAdapter(IRegionBehaviorFactory behaviorFactory) : base(behaviorFactory)
{
}
/// <summary>
/// Adapts a ContentControl to an IRegion.
/// </summary>
/// <param name="region">The new region being used.</param>
/// <param name="regionTarget">The object to adapt.</param>
protected override void Adapt(IRegion region, StackPanel regionTarget)
{
region.Views.CollectionChanged += (sender, e) =>
{
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
foreach (FrameworkElement element in e.NewItems)
{
regionTarget.Children.Add(element);
}
break;
case NotifyCollectionChangedAction.Remove:
foreach (UIElement elementLoopVariable in e.OldItems)
{
var element = elementLoopVariable;
if (regionTarget.Children.Contains(element))
{
regionTarget.Children.Remove(element);
}
}
break;
}
};
}
/// <summary>
/// Template method to create a new instance of IRegion that will be used to adapt the object.
/// </summary>
/// <returns>A new instance of IRegion.</returns>
protected override Microsoft.Practices.Prism.Regions.IRegion CreateRegion()
{
return new AllActiveRegion();
}
}
}
Run Code Online (Sandbox Code Playgroud)
对于Bootstrapper,这里是C#中的ConfigureRegionAdapterMappings()覆盖,为Prism 4更新:
/// <summary>
/// Configures the default region adapter mappings to use in the application.
/// </summary>
/// <returns>The RegionAdapterMappings instance containing all the mappings.</returns>
protected override RegionAdapterMappings ConfigureRegionAdapterMappings()
{
var mappings = base.ConfigureRegionAdapterMappings();
mappings.RegisterMapping(typeof(StackPanel), ServiceLocator.Current.GetInstance<StackPanelRegionAdapter>());
return mappings;
}
Run Code Online (Sandbox Code Playgroud)
根据NVenhola 在Fill My Prism Region的评论,请提供一个简单,适应性强的解决方案:
<ItemsControl cal:RegionManager.RegionName="XXRegionNameXX">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)
为什么不直接使用ItemsControl作为区域?它垂直堆叠项目,Prism为它构建了内置区域适配器.我不明白你为什么要使用StackPanel来做任何事情而不是布局.
默认情况下,ItemsControl使用包含StackPanel的ItemsPanel,因此它等同于已提供的答案,但没有不必要的代码.
| 归档时间: |
|
| 查看次数: |
3491 次 |
| 最近记录: |