WpfResourceApp1f( WPF )

Objectives:

Resources :

  • Logical :
  • Static and Dynamic
  • -->XAML Mark UP : Window.Resources , LinearGradientBrush
    • Background="{StaticResource statBrush1}"
    • Background="{DynamicResource statBrush2}
  • --- >Code Behind
    • SP1.Resources.Add("background", Brushes.DarkTurquoise);
    • statusText.Text ="App:Status "+ Application.Current.ShutdownMode. ToString();
      createNew.Background = (Brush)createNew.FindResource ("background");
      createNew.Click += new RoutedEventHandler(newWindowButton_Click);
      closeWindow.Click += new RoutedEventHandler(shutdownButton_Click);
    • winMenu.SubmenuOpened += new RoutedEventHandler(winMenu_SubmenuOpened);
      Closing += MainWindow_Closing;
      App_Shutdown.Click += new RoutedEventHandler(Shutdown_All);

There are two types of Resources  http://msdn.microsoft.com/en-us/library/ms752326.aspx

  • Logical ,Object or XAML Resources
    • Can be used at Code-Behind and XAML markups
      • Static and Dynamic
        • The StaticResource Markup Extension processes a key by looking up the value for that key in all available resource dictionaries. This happens during loading, which is the point in time when the loading process needs to assign the property value that takes the static resource reference.

        • The DynamicResource Markup Extension instead processes a key by creating an expression, and that expression remains unevaluated until the application is actually run, at which time the expression is evaluated and provides a value

  • Assembly, or Binary or
    • Using Object As a Key
    • These are DLL or Exe

Static Resource Markup Extension

Reference : http://msdn.microsoft.com/en-us/library/ms750950.aspx

 

Dynamic

Reference : http://msdn.microsoft.com/en-us/library/ms748942.aspx

The Resource Directory:

The WPF provides a dictionary class called "ResourceDictionary", that serves in logical roesources.

http://msdn.microsoft.com/en-us/library/aa970494.aspx#Resource_Files

WPF Application Resource, Content, and Data Files
Microsoft Windows applications often depend on files that contain non-executable data, such as Extensible Application Markup Language (XAML), images, video, and audio. Windows Presentation Foundation (WPF) offers special support for configuring, identifying, and using these types of data files, which are called application data files. This support revolves around a specific set of application data file types, including:
  • Resource Files: Data files that are compiled into either an executable or library WPF assembly.
  • Content Files: Standalone data files that have an explicit association with an executable WPF assembly.
  • Site of Origin Files: Standalone data files that have no association with an executable WPF assembly.

One important distinction to make between these three types of files is that resource files and content files are known at build time; an assembly has explicit knowledge of them. For site of origin files, however, an assembly may have no knowledge of them at all, or implicit knowledge through a pack uniform resource identifier (URI) reference; the case of the latter, there is no guarantee that the referenced site of origin file actually exists.

To reference application data files, Windows Presentation Foundation (WPF) uses the Pack uniform resource identifier (URI) Scheme, which is described in detail in Pack URIs in WPF).

This topic describes how to configure and use application data files.

This topic contains the following sections.

If an application data file must always be available to an application, the only way to guarantee availability is to compile it into an application's main executable assembly or one of its referenced assemblies. This type of application data file is known as a resource file.

You should use resource files when:

  • You don't need to update the resource file's content after it is compiled into an assembly.
  • You want to simplify application distribution complexity by reducing the number of file dependencies.
  • Your application data file needs to be localizable (see WPF Globalization and Localization Overview).

 

Step:1 Create a New project

We added few controls as shown in the illustration below.

Step:2 Individual Items

a) Menu Control:

b) Stack Panel

Step:3 We may Start "MainWindow" using "StartupUri = App.xaml " or with an Event after crerating the event as "AppStartUp"

Step:4 Runtime Views

a) Status Bar showing :  Application.Current.ShutdownMode.ToString();

b) Creating new Window: with "New Window" button's event.

c) Testing Hidden Sub-Menu, select any new window to activate

c) Single window will close independently

Note Window 11 was closed

d) Testing Static Resources : Background did not change. ( However,  the local code using Brushes,  like this one "// button2.Background = Brushes.SeaGreen;" will override the static constraints

 

e) Testing Dynamic Resources: click on Second button.

Note the changes in background color.

 

Step:5
Step:6
Step:7
Step:8
Step:9
Step:10
Step:11
Step:12
Step:13
Step:14
Step:15
Step:16
Step:17