C# WPF 接收启动命令行参数

C#   修改Program.cs文件的入口函数,窗口增加构造函数,传递接收到的参数

class TestClass
{
    static void Main(string[] args)
    {
        // Display the number of command line arguments:
        System.Console.WriteLine(args.Length);
    }
}

  • Main 方法是程序的入口点,程序控制在该方法中开始和结束。

  • 该方法在类或结构的内部声明。它必须为静态方法,而不应为公共方法。(在上面的示例中,它接受默认访问级别 private。)

  • 它可以具有 void 或 int 返回类型。

  • 声明 Main 方法时既可以使用参数,也可以不使用参数。

  • 参数可以作为从零开始索引的命令行参数来读取。

  • 与 C 和 C++ 不同,程序的名称不会被当作第一个命令行参数。

     

    找到程序的入口文件:”Program.cs“;

    修改如下红色字体:

     static void Main(string[] args)
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);


                if (args.Length == 0)
                {
                    Application.Run(new frmSMS());
                }
                else
                { 
                    Application.Run(new frmSMS(args));
                }


            }

     

    在启动的Form中加入构造函数:

    public frmSMS(string[] args)
            {
                InitializeComponent();
                m_Content = args[0].ToString();                         //可以接收到命令行中传递的第一个参数
            }

     

    测试命令行参数: 进入DOS窗口————>进入你程序的【Debug】文件夹下

                                    --------》输入你程序执行文件(即为.exe执行文件)

                                   ————》空格 在输入你要传递的值,回车即可。

    在dos窗口下示例:cd    程序所在位置\bin\Debug 回车

                                       测试.exe "测试1" “测试2”          回车                    //如果要传递多个参数每个参数加上【""】用空格隔开即可。



    WPF  修改App.xaml 和 App.xaml.cs 文件

    XAML代码:App.xaml

    <Application x:Class="WPFDemo.App"  
                 xmlns=
    "http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
                 xmlns:x=
    "http://schemas.microsoft.com/winfx/2006/xaml"  
                 StartupUri=
    "MainWindow.xaml" Startup="Application_Startup">   
    </Application>  


    后台代码:App.xaml.cs

    public partial  class App : Application   
    {   
         private  void Application_Startup( object sender, StartupEventArgs e)   
        {   
             string s =  string.Empty;   
             for ( int i = 0; i < e.Args.Length; i++)   
            {   
                s += e.Args[i] +  " ";   
            }   
            MessageBox.Show(s);   
        }   
    }  

      Application_Startup 方法中 e 的 Args 就是参数的集合。



  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
C# WPF中,使用MVVM模式来实现DataGrid自动添加行序号是比较常见的需求。下面是一种实现方式: 1. 首先,在ViewModel中定义一个ObservableCollection来存储数据源,并在构造函数中初始化该集合。 2. 在XAML中,使用DataGrid控件绑定到ViewModel中的数据源集合,并设置AutoGenerateColumns为False。 3. 在DataGrid的列定义中,添加一个新的列,用于显示行序号。可以使用DataGridTextColumn,并设置Binding为"{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGrid}}, Path=Items.IndexOf(.)+1}"。 4. 这样,当数据源集合发生变化时,DataGrid会自动更新行序号。 下面是一个示例代码: ViewModel.cs: ```csharp public class ViewModel : INotifyPropertyChanged { private ObservableCollection<Item> items; public ObservableCollection<Item> Items { get { return items; } set { items = value; OnPropertyChanged(nameof(Items)); } } public ViewModel() { Items = new ObservableCollection<Item>(); // 初始化数据源集合 } // INotifyPropertyChanged接口实现代码省略... } ``` MainWindow.xaml: ```xaml <Window x:Class="WpfApp.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfApp" Title="MainWindow" Height="450" Width="800"> <Grid> <DataGrid ItemsSource="{Binding Items}" AutoGenerateColumns="False"> <DataGrid.Columns> <DataGridTextColumn Header="#" Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGrid}}, Path=Items.IndexOf(.)+1}" /> <!-- 其他列定义 --> </DataGrid.Columns> </DataGrid> </Grid> </Window> ``` 在这个示例中,ViewModel类中的Items属性是用来存储数据源的ObservableCollection。在MainWindow.xaml中,使用DataGrid控件绑定到Items属性,并添加一个新的列来显示行序号。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值