Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF-在标签中显示命令行参数_C#_Wpf_Label_Args - Fatal编程技术网

C# WPF-在标签中显示命令行参数

C# WPF-在标签中显示命令行参数,c#,wpf,label,args,C#,Wpf,Label,Args,我有一个wpf应用程序,需要用几个命令行参数调用它。我如何将它们显示在我为此而放在窗口中的标签中? 我试图实现数据绑定,但没有成功,-变量被正确读取和分配,但由于一些荒谬的原因,没有显示在屏幕上,在我想要的标签中。 代码如下: public partial class MainWindow : Window { public Notification _notif = new Notification(); public MainWindow() { In

我有一个wpf应用程序,需要用几个命令行参数调用它。我如何将它们显示在我为此而放在窗口中的标签中? 我试图实现数据绑定,但没有成功,-变量被正确读取和分配,但由于一些荒谬的原因,没有显示在屏幕上,在我想要的标签中。 代码如下:

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
}
在这里,您可以看到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"/>


非常感谢。

该示例演示了如何在标签中显示参数

这是应用程序的入口点:

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";
    }
}
这是视图模型(视图的数据上下文):

这是视图(代码隐藏):

这是视图中的标签(XAML):



重要您必须删除
StartupUri=“MainWindow.xaml
来自App.xaml文件,因为
MainWindow
是从代码隐藏启动的。

应用程序OnStartUp方法中的MainWindow实例与App.xaml中指定的StartupUri不相同。例如,要将参数传递给MainWindow,必须在MainWindow中创建一个属性,例如,名为CommandArgs的列表属性,在创建MainWindow对象后,循环Args列表并将其内容添加到CommandArgs列表中
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));
    }
}
public partial class MainWindow : Window
{
    public MainWindow(IMainWindowViewModel viewModel)
    {
        InitializeComponent();
        DataContext = viewModel;
    }
}
<Label Content ="{Binding Arguments}"></Label>