C# wpf 工作学习方法总结

一  弹出新窗口方法  

1新窗口出现,直接new出新窗口

             VisitSet vis = new VisitSet(); 
            //打开一个窗口,并关闭新打开的窗口时,才返回。
            if (win.ShowDialog() == true)
            { 
            }

2  设置窗口出现在中央

       public MainWindow()
        {
            WindowStartupLocation = WindowStartupLocation.CenterScreen;
            InitializeComponent();
        }

3 获取或设置对话框结果值,即从返回的值,当设置为true,直接返回调用窗口

private void btn_Save(object sender, RoutedEventArgs e)
{
  DialogResult = true;
} 

4 绑定点击方法:

   <Button    Click="btnVisitSet" />

 二 设置列表grid,并绑定值

1 设置datagrid 名字  x:Name

        <DataGrid x:Name="ipList">

2 设置行名字,并绑定每一行的值

  <DataGrid.Columns>
                <DataGridTextColumn   Header="域名"  Binding="{Binding Path=url, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="*"/>
            </DataGrid.Columns>

3 给datagrid赋值

 ipList.ItemsSource = new info();

4 获取datagrid选中值

ipList.SelectedItem;

注意:所有绑定控件,都要在初始化的时候赋一个初值。不然无法获取到值

完整代码:

    <Grid Margin="0,196,0,0">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="49*"/>
            <ColumnDefinition Width="23*"/>
        </Grid.ColumnDefinitions>
        <DataGrid Grid.Row="0"  RowHeaderWidth="0"   AlternationCount="2"  AutoGenerateColumns="False" FontSize="15" CanUserAddRows="False" CanUserDeleteRows="False" IsReadOnly="True" HorizontalScrollBarVisibility="Hidden" EnableRowVirtualization="False" IsEnabled="True" EnableColumnVirtualization="False" VerticalGridLinesBrush="#FFBCC1BC" HorizontalGridLinesBrush="#FFBCC1BC" Margin="0,-117,0,0" Grid.ColumnSpan="2" x:Name="ipList">
            <DataGrid.Columns>
                <DataGridTextColumn   Header="域名"  Binding="{Binding Path=url, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="*"/>
                <DataGridTextColumn   Header="编码" Binding="{Binding Path=encoding, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="50"/>
                <DataGridTextColumn Header="正则表达式" Binding="{Binding Path=regex, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="*"/>
            </DataGrid.Columns>
        </DataGrid>
        <Button Content="添加" HorizontalAlignment="Left" Margin="15,-181,0,0" VerticalAlignment="Top" Width="74" RenderTransformOrigin="0.899,0.568" Click="btnAddIpList"/>
        <Button Content="删除" HorizontalAlignment="Left" Margin="114,-181,0,0" VerticalAlignment="Top" Width="76" Click="btn_Delete"/>
    </Grid>

三 添加管理员权限

右击项目——>属性——>安全性——>勾选启用ClickOnce安全设置(N)

这时候Properties下出现,app.manifest。打开app.manifest文件,找到第18行

  <requestedExecutionLevel level="asInvoker" uiAccess="false" />

改为:

 <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

启用ClickOnce安全设置(N)的勾选去掉

四 启动和终止项设置

1 已定义类用Main方法实现对WPF应用程序的启动

 public partial class App : Application
    {
        [STAThread]
        static void Main()
        {
          定义Application对象作为整个应用程序入口  
            Application app = new Application();
            // 方法一:调用Run方法,参数为启动的窗体对象 ,也是最常用的方法 
            Window2 win = new Window2();
            app.Run(win);
            // 方法二:指定Application对象的MainWindow属性为启动窗体,然后调用无参数的Run方法  
            Window2 win = new Window2();  
            app.MainWindow = win; 
            win.Show();       
            app.Run();  
            方法三:通过Url的方式启动 StartupUri 
            app.StartupUri = new Uri("Window2.xaml", UriKind.Relative); 
            app.Run();
        }
    }

你也可以在代码文件(App.xaml.cs)中进行更改

<Application   StartupUri="MainWindow.xaml">

Application.Current返回当前应用程序的当前Application实例

在WPF应用程序的关闭是有ShutdownMode属性设置,具有3种枚举类型的值:

OnLastWindowClose //应用程序最后一个窗体关闭时关闭应用程序
OnMainWindowClose //应用程序主窗体关闭时关闭应用程序
OnExplicitShutdown   //显示调用关闭

OnExplicitShutdown模式下必须显示调用Application实例的ShutDown方法

Application.Current.Shutdown(-1);
 
Application.Current.ShutdownMode=ShutdownMode.OnLastWindowClose;

另外,还有一种强制终止应用程序进程的方法

Environment.Exit(0)

winform实现: 

 
Application.Exit();

在主窗口设置Closed方法关闭资源:

<Window Closed="Window_Closed">

五 其他方法:

1 按钮变灰

IsEnabled = false

六 绑定多个数据源

绑定多个数据源,使用用户控件

1 创建多个用户控件UserControl

2 在主页面绑定多个用户控件 

        <local:FrpConfigControl x:Name="view1" Margin="0,93,0,-93" Loaded="view1_Loaded">
        </local:FrpConfigControl>
        <local:FrpConfigControl x:Name="view2" Margin="0,246,0,-140" Loaded="view2_Loaded">
        </local:FrpConfigControl>

 3 绑定用户控件的值

            this.view1.DataContext = new obj1();
            this.view2.DataContext = new obj2();

七 指启动一个进程

使用EventWaitHandle信号事件

    public partial class App : Application
    {
        public EventWaitHandle ProgramStarted { get; set; }


        protected override void OnStartup(StartupEventArgs e)
        {
            bool createNew;
            ProgramStarted = new EventWaitHandle(false, EventResetMode.AutoReset, "MyTctiApp", out createNew);


            if (!createNew)
            {
                MessageBox.Show("程序已启动");
                App.Current.Shutdown();
                Environment.Exit(0);
            }
            base.OnStartup(e);
        }

    }

打开文件对话框可以设置筛选格式。

Filter属性的构成: Excel文件|*.xls 

            ofd.Filter = "所有图片文件|*.tiff|TIFF(*.tiff)|*.tiff";

 Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog();
            ofd.Filter = "Video File(*.avi;*.mp4;*.mkv;*.wav;*.rmvb)|*.avi;*.mp4;*.mkv;*.wav;*.rmvb|All File(*.*)|*.*";

总结:

使用了EventWaitHandle信号事件,构造函数第四个参数,createNew创建相关命名系统事件返回true,如果已存在,返回false

 扩展:

EventWaitHandle:表示一个线程同步事件。

EventWaitHandle各个线程程之间互通信号。

线程相关性(Thread Affinity )

简单总结: Mutex与Monitor是线程相关性,必须获得对象锁的线程才能调用Pulse()/Wait()/Exit();

EventWaitHandle 线程无关,任何线程都可以发信号阻塞。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值