我正在使用Windows 8.1 Store App的Prism框架。 我想使用类似于我们使用Prism Composition包(Region Manager)的桌面应用程序的机制来实现动态View注入。
如果你们指导我关注以下事项,我将非常感激: -
非常感谢提前。
由于 萨姆
答案 0 :(得分:1)
Windows Runtime的Prism不像Prism for WPF那样提供区域机制。
实现类似的东西并不容易实现。我会想办法实现这个并回复你。
谢谢,
埃塞基耶尔
答案 1 :(得分:0)
public class RegionManager
{
private static readonly Dictionary<string, Func<object>> RegisteredContent = new Dictionary<string, Func<object>>();
public static readonly DependencyProperty RegionNameProperty = DependencyProperty.RegisterAttached("RegionName", typeof(string), typeof(RegionManager), new PropertyMetadata(null));
public static void SetRegionName(DependencyObject target, string name)
{
target.SetValue(RegionNameProperty, name);
if (string.IsNullOrEmpty(name)) return;
CreateContentRegion(target, name);
}
public static string GetRegionName(DependencyObject target)
{
return (string)target.GetValue(RegionNameProperty);
}
public void RegisterViewWithRegion(string regionName, Func<object> getContentDelegate)
{
RegisteredContent.Add(regionName, getContentDelegate);
}
private static void CreateContentRegion(DependencyObject target, string name)
{
if (target is ContentControl)
{
Func<object> value;
RegisteredContent.TryGetValue(name, out value);
if (value == null) return;
((ContentControl)target).Content = value.DynamicInvoke();
}
}
}
在app.cs中你需要:
ealed partial class App
{
readonly IUnityContainer _container = new UnityContainer();
readonly RegionManager _regionManager = new RegionManager();
public App()
{
InitializeComponent();
}
protected override Task OnLaunchApplicationAsync(LaunchActivatedEventArgs args)
{
NavigationService.Navigate("Main", null);
return Task.FromResult<object>(null);
}
protected override Task OnInitializeAsync(IActivatedEventArgs args)
{
var locator = new UnityServiceLocator(_container);
ServiceLocator.SetLocatorProvider(() => locator);
_container.RegisterInstance(SessionStateService);
_container.RegisterInstance(NavigationService);
_container.RegisterInstance(_regionManager);
_container.RegisterInstance<IEventAggregator>(new EventAggregator());
ViewModelLocationProvider.SetDefaultViewModelFactory(viewModelType => _container.Resolve(viewModelType));
InizializeRegionManager();
return Task.FromResult<object>(null);
}
private void InizializeRegionManager()
{
var region = _container.Resolve<RegionManager>();
region.RegisterViewWithRegion("RegionName", () => _container.Resolve<RegionView>());
}
如果你愿意,你可以让它变得更好。你可以写你的模块等