2015-09-21 1 views
1

J'ai une application wpf qui doit être appelée avec plusieurs arguments de ligne de commande. Comment puis-je les montrer dans les étiquettes que j'ai placées dans la fenêtre juste pour cette raison? J'ai essayé d'implémenter la liaison de données, mais sans succès, - la variable est lue et affectée correctement, mais pour une raison absurde n'est pas affichée à l'écran, dans l'étiquette que je veux. Voici le code:WPF - Afficher les arguments de ligne de commande dans une étiquette

public partial class MainWindow : Window 
{ 
    public Notification _notif = new Notification(); 
    public MainWindow() 
    { 
     InitializeComponent(); 
     this.DataContext = new Notification(); 
    } 

    protected override void OnClosed(EventArgs e) 
    { 
     base.OnClosed(e); 
     App.Current.Shutdown(); 
    } 
} 

public partial class App : Application 
{ 
    protected override void OnStartup(StartupEventArgs e){ 
     if (e.Args.Length >= 4) 
     { 
      MainWindow mainWindow = new MainWindow(); 

      Label count_label = (Label)mainWindow.FindName("count"); 
      count_label.DataContext = mainWindow._notif; 

      System.Diagnostics.Debug.WriteLine(mainWindow._notif.count + " - notif.count"); 
      // bind the Date to the UI 
      count_label.SetBinding(Label.ContentProperty, new Binding("count") 
      { 
       Source = mainWindow._notif, 
       Mode = BindingMode.TwoWay 
      }); 
      //assigning values to the labels 

      System.Diagnostics.Debug.WriteLine(count_label.Content + " - content of the label 'count'"); 
      mainWindow._notif.count = e.Args[0]; 
      System.Diagnostics.Debug.WriteLine(e.Args[0] + " is the argument n. 0"); 
      System.Diagnostics.Debug.WriteLine(mainWindow._notif.count + " - notif.count"); 


      System.Diagnostics.Debug.WriteLine(count_label.Content + "-------------------"); 

      System.Diagnostics.Debug.WriteLine(count_label.Content + " - content of the label 'count'"); 
      mainWindow._notif.count = "1234"; 
      System.Diagnostics.Debug.WriteLine(mainWindow._notif.count + " - notif.count"); 
      System.Diagnostics.Debug.WriteLine(count_label.Content + " - content of the label 'count'"); 

     } 
    } 
} 

public class Notification : INotifyPropertyChanged 
{ 
    private string _count; 

    public string count { 
     get { 
      return _count; 
     } 

     set { 
      _count = value; 
      OnPropertyChanged("count"); 
     } 
    } 

    #region INotifyPropertyChanged Members 

    public event PropertyChangedEventHandler PropertyChanged; 

    protected virtual void OnPropertyChanged(string propertyName) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 

    #endregion 
} 

Et ici vous pouvez voir un extrait de la XAML:

<Label x:Name="count" Content="{Binding count}" HorizontalAlignment="Center" Margin="0,10,486,0" VerticalAlignment="Top" RenderTransformOrigin="-2.895,-0.769" Height="80" Width="145" FontFamily="Arial" FontSize="64" HorizontalContentAlignment="Center"/> 

Merci anticipately.

+1

L'instance de MainWindow à l'intérieur de votre App La méthode OnStartUp n'est pas la même que la StartupUri spécifiée dans App.xaml –

+0

Voir par ex. http://stackoverflow.com/a/25661138/1061668 –

+0

Pour transmettre vos arguments à MainWindow, vous devez créer une propriété à l'intérieur de MainWindow, par exemple une propriété List nommée CommandArgs, et après avoir créé votre cycle d'objet MainWindow, les Args liste et ajoute son contenu à la liste CommandArgs –

Répondre

2

L'exemple montre comment afficher des arguments dans une étiquette.

C'est le point d'entrée de l'application:

public partial class App : Application 
{ 
    protected override void OnStartup(StartupEventArgs e) 
    { 
     var argumentsInfo = BuildArgumentsInfo(e.Args); 
     var viewModel = new MainWindowViewModel(argumentsInfo); 
     var window = new MainWindow(viewModel); 
     window.Show(); 
    } 

    private string BuildArgumentsInfo(string[] args) 
    { 
     return args.Any() 
      ? args.Aggregate((arg1, arg2) => arg1 + " " + arg2) 
      : "No arguments"; 
    } 
} 

C'est le modèle de vue (un contexte de données de la vue):

public interface IMainWindowViewModel 
{ 
    string Arguments { get; set; } 
} 

public class MainWindowViewModel : IMainWindowViewModel, INotifyPropertyChanged 
{ 
    private string _arguments; 

    public MainWindowViewModel(string argumentsInfo) 
    { 
     Arguments = argumentsInfo; 
    } 

    public string Arguments 
    { 
     get { return _arguments; } 
     set 
     { 
      _arguments = value; 
      RaisePropertyChanged("Arguments"); 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged = delegate {}; 

    private void RaisePropertyChanged(string propertyName) 
    { 
     PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
    } 
} 

Ceci est le point de vue (code derrière):

public partial class MainWindow : Window 
{ 
    public MainWindow(IMainWindowViewModel viewModel) 
    { 
     InitializeComponent(); 
     DataContext = viewModel; 
    } 
} 

C'est une étiquette dans la vue (XAML): 01

<Label Content ="{Binding Arguments}"></Label> 

Important! Vous devez supprimer StartupUri="MainWindow.xaml du fichier App.xaml car MainWindow est lancé à partir d'un code derrière.