Samriddha Chatterjee
Samriddha Chatterjee

Reputation: 103

Windows 8.1 Store App->Prism Framework-usage of Regions (UI Composition)

I am using Prism framework for Windows 8.1 Store App. I want to achieve dynamic View injection using the mechanism similar to what we have for Desktop applications using Prism Composition package (Region Manager).

I would highly appreciate, if you guys guide me on followings:-

  1. Do we have a Prism Composition package compatible for Windows Store App (Win RT). If yes, what's it's name.
  2. IF not, then any suggestion on how to achieve the same via Unity or anything.

Many thanks in advance.

Thanks Sam

Upvotes: 1

Views: 357

Answers (2)

Smagin Alexey
Smagin Alexey

Reputation: 345

I learned it. First, i tried move all classes and etc to winrt. I saw many depenecies from WPF. Main tought of region manager - attached property create view in contentcontrol and itemscontrol. In my simple example i didn't use weakreference and etc. You can improve it yourself. But it works (i tried))

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();
        }
    }
}

In app.cs you need:

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>());
    }

If you want, you can make it more better. You can write your module and etc

Upvotes: 0

Ezequiel Jadib
Ezequiel Jadib

Reputation: 14787

Prism for Windows Runtime doesn't provide a regions mechanism as in Prism for WPF.

Achieving something similar is not something easy to implement. I will think on ways to implement this and get back to you.

Thanks,

Ezequiel

Upvotes: 1

Related Questions