title: Splash screen in WPF MVVM application
body: <p>I have a MVVM WPF application from which I show a splash screen while some long task is done.</p>
  
  <p>This splash screen is a typical WPF window and is very simple, it only has a label to show custom text such as "Loading..." and a spinner.</p>
  
  <p>Its code-behind only has the constructor in which it is only performed the InitializeComponent and and event Window_Loaded.</p>
  
  <p>From my main MVVM WPF application, when I perform a long task I instantiate this window and show the splash screen.</p>
  
  <p>So now, I want to customize the text shown in the label in the splash screen. So what is the best approach?</p>
  
  <p>What I have done is to pass as a parameter a string (custom message) to the constructor when I instantiate the window (Splash Screen).</p>
  
  <pre><code>Window mySplash = new SplashScreen("Loading or whatever I want");
  </code></pre>
  
  <p>As this splash screen is very simple, only a splash, I think it has no sense to apply MVVM here, so in the code-behind I create a private property which I set with the string passed as parameter to the constructor. Then, I bind the label in view with this private property and finally I implement INotifyPropertyChanged in the code-behind (view). There is no model, model view here.</p>
  
  <p>Is that the correct way to do it? or is there any other way?</p>
  
  <p>I know there is other solutions, like making public the label in the view by adding this:</p>
  
  <pre><code>x:FieldModifier="public"
  </code></pre>
  
  <p>and then access it once I instantiate the splash screen but I do not like this solution, I do not want to expose label outside.</p>
  
  <p>ATTEMPT #1:</p>
  
  <p>From my view model in main MVVM WPF application I perform below:</p>
  
  <pre><code>Window splashScreen = new SplashScreen("Loading ..."); 
  </code></pre>
  
  <p>Splash screen window:</p>
  
  <pre><code>&lt;Window x:Class="My.Apps.WPF.SplashScreen"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt;
  
      &lt;Grid&gt;
      &lt;!-- Grid row and column definitions here --&gt;
           &lt;Label Grid.Row="0" Content="{Binding Path=Message}"/&gt;  
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>Splash screen code-behind:</p>
  
  <pre><code>public partial class SplashScreen: Window
  {
      public string Message
      {
          get
          {
              return (string)GetValue(MessageProperty);
          }
          set { SetValue(MessageProperty, value); }
      }
      public static readonly DependencyProperty
          MessageProperty =
          DependencyProperty.Register("Message",
          typeof(string), typeof(System.Window.Controls.Label),
          new UIPropertyMetadata("Working, wait ..."));
  
      public SplashScreen(string message)
      {
          InitializeComponent();
  
          if (!String.IsNullOrEmpty(message))
              this.Message = message;
      }
  </code></pre>
  
  <p>}</p>
  
  <p>I have set a default value for Label.It will be used if it is not passed as a parameter to the constructor.</p>
  
  <p>It is not working, in the xaml preview is not showing the default message in the label in the Visual Studio IDE environment. Also for some reason, when I pass as a parameter the custom message from the view model, it is not shown in the label. What am I doing wrong?</p>