ResourceDictionary management

Topics: Prism v2 - WPF 3.5
Aug 8, 2009 at 10:59 AM

Hi everybody

I'm developing a application using a composite application guidance. I encounter a resourceDictionary management problem. What i mean is that.

I define a ResourceDictionary file in each module. What i would like to do is to update some of my resources(the values) from my shell when the module is load.

Basically if the color of my button is defined as red in the module, i would like to change this color to black when it is displayed in my shell.

How can i deal with this issu in the composite application guidance context.

Thanks

 

Aug 8, 2009 at 3:53 PM

Hi blochaou,

If your PRISM bootstrapper application contains a Resource file that looks as follows:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">    

    <SolidColorBrush x:Key="backgroundBrush">Red</SolidColorBrush>
    
    <Style TargetType="Button">
        <Setter Property="Background" Value="{DynamicResource backgroundBrush}"/>
    </Style>
</ResourceDictionary>

Then all of your buttons will be red - if you wanted to change them globally - the resource file would be the place to do it. 

Let's say in your module you have a presenter that wants to change "only" it's view to where it's buttons are blue - you could do the following:

/// <summary>
/// 
/// </summary>
public class DetailsViewPresenter : PresenterBase<IDetailsView>
{
    /// <summary>
    /// Initializes a new instance of the <see cref="DetailsViewPresenter"/> class.
    /// </summary>
    /// <param name="view">The view.</param>
    /// <param name="model">The model.</param>
    /// <param name="container">The container.</param>
    public DetailsViewPresenter(
        IDetailsView view, 
        IDemoPresentationModel model, 
        IUnityContainer container) : base(view, model, container)
    {
        if (View.Resources.Contains("backgroundBrush"))
            View.Resources.Remove("backgroundBrush");

        View.Resources.Add("backgroundBrush", new SolidColorBrush(Colors.Blue));
    }
}

Where IDetailsView looks as follows:

public interface IDetailsView : IView { }
And IView looks as follows:
using System.Windows.Threading;
using Microsoft.Practices.Unity;
using System.Windows;
using System.Windows.Controls;
using win = System.Windows.Forms;

namespace PrismContrib.Interfaces
{
    /// <summary>
    /// View Interface
    /// </summary>
    public interface IView
    {
#if !WinForm
        /// <summary>
        /// Occurs when [size changed].
        /// </summary>
        event SizeChangedEventHandler SizeChanged;

        /// <summary>
        /// Gets the controls.
        /// </summary>
        /// <value>The controls.</value>
        UIElementCollection Controls { get; }

        /// <summary>
        /// Gets or sets the resources.
        /// </summary>
        /// <value>The resources.</value>
        ResourceDictionary Resources { get; set; }

#else

        /// <summary>
        /// Gets the controls.  Specifically for WinForms
        /// </summary>
        /// <value>The controls.</value>
        win.Control.ControlCollection Controls { get; }

#endif


        /// <summary>
        /// Gets or sets the data context.
        /// </summary>
        /// <value>The data context.</value>
        object DataContext { get; set; }

        /// <summary>
        /// Gets or sets the container.
        /// </summary>
        /// <value>The container.</value>
        IUnityContainer Container { get; set; }

        /// <summary>
        /// Finds the name.
        /// </summary>
        /// <param name="controlName">Name of the control.</param>
        /// <returns></returns>
        object FindName(string controlName);

        /// <summary>
        /// Gets the name.
        /// </summary>
        /// <value>The name.</value>
        string Name { get; }

#if WinForm
        /// <summary>
        /// Gets or sets the width.
        /// </summary>
        /// <value>The width.</value>
        int Width { get; set; }
        
        /// <summary>
        /// Gets or sets the height.
        /// </summary>
        /// <value>The height.</value>
        int Height {get; set; }
#else
        /// <summary>
        /// Gets or sets the width.
        /// </summary>
        /// <value>The width.</value>
        double Width { get; set; }

        /// <summary>
        /// Gets or sets the height.
        /// </summary>
        /// <value>The height.</value>
        double Height { get; set; }
#endif
    }
}

 

 

 

 

Aug 8, 2009 at 5:27 PM

Hi billkrat

Thanks for message. This is not exactly what i'm looking for but i think i'll work around using your solution.

My goal is to inject the resources in the module from the shell. The idea is as follow:

1. i've just received the .dll of the module

2. no way to change anything in the module

3. i'have a documentation telling me the keys and the types of the resources defined in the module.

Based on  these informations i'll change  the resources as i want.

May be the composition application guidance doesn't deal with this issu because all modules have to know the shell (View injection or discovery).

In my idea no module know the shell. Only the shell knows the nodules.

Thanks