-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiary8.txt
946 lines (810 loc) · 25.2 KB
/
Diary8.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
【2019-08-03】WPF Style
要在WPF某个控件上应用Style,如果在声明Style的时候指明Key则必须在
具体控件的Style上BaseOn这个带有Key的基础Style。
如果声明这个Style的时候没有指明Key,则该Style会全局被应用上。
例:
1.有效方式
<Style TargetType="{x:Type Control}" x:Key="FontBase">
<Setter Property="FontFamily" Value="{StaticResource Tahoma Bold}"></Setter>
</Style>
<Style TargetType="{x:Type Label}" BasedOn="{StaticResource ResourceKey=FontBase}"></Style>
2.无效方式
<Style TargetType="{x:Type Label}" x:Key="FontBase">
<Setter Property="FontFamily" Value="{StaticResource Tahoma Bold}"></Setter>
</Style>
3.有效方式
<Style TargetType="{x:Type Label}">
<Setter Property="FontFamily" Value="{StaticResource Tahoma Bold}"></Setter>
</Style>
【2019-08-10】python import
# https://www.cnblogs.com/tp1226/p/8453854.html
# https://www.cnblogs.com/lzc978/p/10105194.html
1.from [package] import * 相当于把pakcage里面__init__.py的内容拷贝到import者py文件执行
例:
.
|--subpackage0
| |--__init__.py
| |--module10.py
|--main.py
【__init__.py】
print "[package] [{}] is imported".format(__name__)
import module10
import module11
【module10.py】
def func():
print "module0"
【main.py】
from subpackage0 import *
module10.func()
输出:
[package] [subpackage0] is imported
module0
2.在package里面的__init__.py 里声明并赋值__all__变量会在from [package] import *的时候将
__init__.py里面__all__变量的模块在import者py文件都执行一次import
例:
.
|--subpackage0
| |--__init__.py
| |--module10.py
|--main.py
【__init__.py】
print "[package] [{}] is imported".format(__name__)
__all__ = ['module10']
【module10.py】
def func():
print "module0"
【main.py】
from subpackage0 import *
module10.func()
输出:
[package] [subpackage0] is imported
module0
3.from [module] import * 相当于把module里面的对象拷贝(注意非引用)到import者py文件
例:
.
|--testmodule.py
|--main.py
【testmodule.py】
print "[module] [{}] imported".format(__name__)
message = 'i am module: {}'.format(__name__)
【main.py】
from testmodule import *
print "output message inside testmodule"
print message
message = 'i am main'
import testmodule
print "message inside testmodule remains unchanged"
print testmodule.message
testmodule.message = message
print "message inside testmodule changes"
print testmodule.message
输出:
[module] [testmodule] imported
output message inside testmodule
i am module: testmodule
message inside testmodule remains unchanged
i am module: testmodule
message inside testmodule changes
i am main
4.from module import [object] 相当于把module里面的object拷贝(注意非引用)到import者py文件
例:
.
|--testmodule.py
|--main.py
【testmodule.py】
print "[module] [{}] imported".format(__name__)
message = 'i am module: {}'.format(__name__)
【main.py】
from testmodule import message
print "output message inside testmodule"
print message
message = 'i am main'
import testmodule
print "message inside testmodule remains unchanged"
print testmodule.message
testmodule.message = message
print "message inside testmodule changes"
print testmodule.message
输出:
[module] [testmodule] imported
output message inside testmodule
i am module: testmodule
message inside testmodule remains unchanged
i am module: testmodule
message inside testmodule changes
i am main
5.import [package].[module] 或者 import [package] 都会把package里面__init__.py的内容执行一次
(package被首次import __init__.py的内容会被执行一次)
例:
.
|--subpackage0
| |--__init__.py
| |--module10.py
|--main.py
【__init__.py】
print "[package] [{}] is imported".format(__name__)
【module10.py】
print "[module] [{}] imported".format(__name__)
def func():
print "module0"
【main.py】
import subpackage0.module10
输出:
[package] [subpackage0] is imported
[module] [subpackage0.module10] imported
6.只有import [package].module (as ...)或者 import [package] (as ...)语法 没有 import ([package].)[module].[object] (as ...)语法
例:
.
|--subpackage0
| |--__init__.py
| |--module10.py
|--testmodule.py
|--main.py
【__init__.py】
print "[package] [{}] is imported".format(__name__)
【module10.py】
print "[module] [{}] imported".format(__name__)
message = 'i am module: {}'.format(__name__)
【testmodule.py】
print "[module] [{}] imported".format(__name__)
message = 'i am module: {}'.format(__name__)
【main.py】
import testmodule as m
import subpackage0 as p
try:
import testmodule.message
except:
import traceback
traceback.print_exc()
try:
import subpackage0.module10.message
except:
import traceback
traceback.print_exc()
输出:
[module] [testmodule] imported
[package] [subpackage0] is imported
Traceback (most recent call last):
File "D:\Test\main.py", line 5, in <module>
import testmodule.message
ImportError: No module named message
[module] [subpackage0.module10] imported
Traceback (most recent call last):
File "D:\Test\main.py", line 11, in <module>
import subpackage0.module10.message
ImportError: No module named message
[Finished in 0.1s]
【2019-08-11】【1】WPF RoutedEvent
// https://docs.microsoft.com/zh-tw/dotnet/framework/wpf/advanced/routed-events-overview
// https://www.cnblogs.com/JerryWang1991/archive/2013/03/29/2981103.html
WPF RoutedEvent用于在WPF控件层次结构中传递事件
例:
<Window x:Class="MiniWindow.RouteEventTest"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MiniWindow"
Title="RouteEventTest" Height="300" Width="300"
>
<StackPanel x:Name="Panel">
<Button Content="Button" x:Name="Button"></Button>
</StackPanel>
</Window>
public partial class RouteEventTest : Window
{
/// <summary>
/// Tunnel Event
/// 该Event将从层次结构中的上层对象往下层对象传递
/// </summary>
public static readonly RoutedEvent TunnelEvent = EventManager.RegisterRoutedEvent(
"Tunnel", RoutingStrategy.Tunnel, typeof(RoutedEventHandler), typeof(RouteEventTest));
// Provide CLR accessors for the event
public event RoutedEventHandler Tunnel
{
add { AddHandler(TunnelEvent, value); }
remove { RemoveHandler(TunnelEvent, value); }
}
/// <summary>
/// Bubble Event
/// 该Event将从层次结构中的下层对象往上层对象传递
/// </summary>
public static readonly RoutedEvent BubbleEvent = EventManager.RegisterRoutedEvent(
"Bubble", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(RouteEventTest));
// Provide CLR accessors for the event
public event RoutedEventHandler Bubble
{
add { AddHandler(BubbleEvent, value); }
remove { RemoveHandler(BubbleEvent, value); }
}
/// <summary>
/// DirectEvent
/// 该Event将直接传递到目标对象
/// </summary>
public static readonly RoutedEvent DirectEvent = EventManager.RegisterRoutedEvent(
"Direct", RoutingStrategy.Direct, typeof(RoutedEventHandler), typeof(RouteEventTest));
// Provide CLR accessors for the event
public event RoutedEventHandler Direct
{
add { AddHandler(DirectEvent, value); }
remove { RemoveHandler(DirectEvent, value); }
}
public RouteEventTest()
{
InitializeComponent();
// Add handler for window
RoutedEventHandler windowHandler = new RoutedEventHandler((sender, e) =>
{
MessageBox.Show("Window receive event");
});
this.AddHandler(TunnelEvent, windowHandler);
this.AddHandler(BubbleEvent, windowHandler);
this.AddHandler(DirectEvent, windowHandler);
// Add handler for panel
RoutedEventHandler panelHandler = new RoutedEventHandler((sender, e) =>
{
MessageBox.Show("Panel receive event");
});
Panel.AddHandler(TunnelEvent, panelHandler);
Panel.AddHandler(BubbleEvent, panelHandler);
Panel.AddHandler(DirectEvent, panelHandler);
// Add handler for button
RoutedEventHandler buttonHandler = new RoutedEventHandler((sender, e) =>
{
MessageBox.Show("Button receive event");
});
Button.AddHandler(TunnelEvent, buttonHandler);
Button.AddHandler(BubbleEvent, buttonHandler);
Button.AddHandler(DirectEvent, buttonHandler);
// https://stackoverflow.com/questions/11336140/handle-tunneled-custom-routed-event
// 发起TunnelEvent 发起对象为Tunnel传递的终点
// 第一个参数为要发起的event 第二个参数将赋值到event处理函数中RoutedEventArgs的OriginalSource
Button.RaiseEvent(new RoutedEventArgs(TunnelEvent, this));
// 发起BubbleEvent 发起对象为Bubble传递的起点
// 第一个参数为要发起的event 第二个参数将赋值到event处理函数中RoutedEventArgs的OriginalSource
Button.RaiseEvent(new RoutedEventArgs(BubbleEvent, Button));
// 发起DirectEvent 发起对象为Direct的起点和终点
// 第一个参数为要发起的event 第二个参数将赋值到event处理函数中RoutedEventArgs的OriginalSource
Button.RaiseEvent(new RoutedEventArgs(DirectEvent, Button));
}
}
输出:
"Window receive event"
"Panel receive event"
"Button receive event"
"Button receive event"
"Panel receive event"
"Window receive event"
"Button receive event"
【2019-08-11】【2】WPF ControlTemplate
WFP ControlTemplate用于重建控件样式,同时可以设置触发器响应控件属性更变
例:
<Window x:Class="MiniWindow.ControlTemplateTest"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MiniWindow"
Title="ControlTemplateTest" Height="330" Width="300"
>
<Window.Resources>
<!-- assign a style for reusing -->
<Style x:Key="RedButtonStyle" TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<!-- the behavour is to set the Foreground property into Yellow -->
<Setter Property="Foreground" Value="Yellow"></Setter>
</Trigger>
</ControlTemplate.Triggers>
<Grid>
<Ellipse Fill="Red" Width="150" Height="100"/>
<ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!-- assign a template for reusing -->
<ControlTemplate x:Key="GreenButtonTemplate" TargetType="{x:Type Button}">
<ControlTemplate.Triggers>
<!-- the trigger will work when the [property] of the button is IsMouseOver
and [value] is True -->
<Trigger Property="IsMouseOver" Value="True">
<!-- the behavour is to set the Foreground property into Yellow -->
<Setter Property="Foreground" Value="Yellow"></Setter>
</Trigger>
</ControlTemplate.Triggers>
<Grid>
<Ellipse Fill="Green" Width="150" Height="100"/>
<ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center"/>
</Grid>
</ControlTemplate>
</Window.Resources>
<StackPanel>
<!-- setup Template property inside a button-->
<Button Content="BlueButton" Click="Button_Click">
<Button.Template>
<!-- setup the TargetType to help the ContentPresenter show the content-->
<ControlTemplate TargetType="{x:Type Button}">
<!-- setup triggers to make hover effect-->
<ControlTemplate.Triggers>
<!-- the trigger will work when the [property] of the button is IsMouseOver
and [value] is True -->
<Trigger Property="IsMouseOver" Value="True">
<!-- the behavour is to set the Foreground property into Yellow -->
<Setter Property="Foreground" Value="Yellow"></Setter>
</Trigger>
</ControlTemplate.Triggers>
<Grid>
<Ellipse Fill="Blue" Width="150" Height="100"/>
<ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center"/>
</Grid>
</ControlTemplate>
</Button.Template>
</Button>
<!-- setup Style property inside a button-->
<Button Content="RedButton" Click="Button_Click" Style="{StaticResource RedButtonStyle}"/>
<!-- setup Template property inside a button-->
<Button Content="GreenButton" Click="Button_Click" Template="{StaticResource GreenButtonTemplate}"/>
</StackPanel>
</Window>
public partial class ControlTemplateTest : Window
{
public ControlTemplateTest()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("I am clicked");
}
}
【2019-08-11】【3】WPF DataTemplate
WFP DataTemplate用于重建控件Content属性的显示方式
例:
<Window x:Class="MiniWindow.DataTemplateTest"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MiniWindow"
Title="DataTemplateTest" Height="330" Width="300"
>
<Window.Resources>
<!-- assign a style for reusing -->
<Style x:Key="LabelTemplateStyle" TargetType="{x:Type ContentControl}">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<Label Content="{Binding Text}"></Label>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
<!-- assign a dataTemplate for reusing -->
<DataTemplate x:Key="TextBlockTemplate">
<TextBlock Text="{Binding Text}"></TextBlock>
</DataTemplate>
</Window.Resources>
<StackPanel>
<!-- setup the ContentTemplate property of a ContentControl -->
<!-- https://stackoverflow.com/questions/24534021/wpf-datatemplate-binding -->
<!-- setup binding for content to let DataTemplate able to bind attribute from the original binding source -->
<ContentControl Content="{Binding ContainerA}">
<ContentControl.ContentTemplate>
<DataTemplate>
<Button Content="{Binding Text}"></Button>
</DataTemplate>
</ContentControl.ContentTemplate>
</ContentControl>
<!-- setup the ContentTemplate property of by using Style -->
<ContentControl Content="{Binding ContainerB}" Style="{StaticResource LabelTemplateStyle}"/>
<!-- setup the ContentTemplate property of by using Template -->
<ContentControl Content="{Binding ContainerC}" ContentTemplate="{StaticResource TextBlockTemplate}"/>
</StackPanel>
</Window>
public partial class DataTemplateTest : Window, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged = (sender, e) => { };
public class ContentContainer : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged = (sender, e) => { };
string _Text;
public string Text
{
get
{
return _Text;
}
set
{
_Text = value;
PropertyChanged(this, new PropertyChangedEventArgs("Text"));
}
}
};
ContentContainer _ContainerA;
public ContentContainer ContainerA
{
get
{
return _ContainerA;
}
set
{
_ContainerA = value;
PropertyChanged(this, new PropertyChangedEventArgs("ContainerA"));
}
}
ContentContainer _ContainerB;
public ContentContainer ContainerB
{
get
{
return _ContainerB;
}
set
{
_ContainerB = value;
PropertyChanged(this, new PropertyChangedEventArgs("ContainerB"));
}
}
ContentContainer _ContainerC;
public ContentContainer ContainerC
{
get
{
return _ContainerC;
}
set
{
_ContainerC = value;
PropertyChanged(this, new PropertyChangedEventArgs("ContainerC"));
}
}
public DataTemplateTest()
{
InitializeComponent();
// setup DataContext to this
DataContext = this;
ContainerA = new ContentContainer
{
Text = "TestButton"
};
ContainerB = new ContentContainer
{
Text = "TestLabel"
};
ContainerC = new ContentContainer
{
Text = "TestTextBlock"
};
}
}
【2019-08-17】【1】C# abstract virtual override new
1.abstract用于修饰函数为抽象函数,该函数只能存在于abstract修饰的抽象类中,并且不能带有实现,
是一种特殊的virtual
2.virtual用于修饰函数为虚函数,允许子类重写该函数,用于动态函数绑定
3.override用于修饰函数为重写父类虚函数,若子类要重写父类虚函数实现,重写函数必须带有override修饰
4.new用于修饰子类重写父类常规静态绑定函数
例:
abstract class Base
{
public void Func()
{
Console.WriteLine("Base::Func");
}
public virtual void VirtualFunc()
{
Console.WriteLine("Base::VirtualFunc");
}
public abstract void AbstractFunc();
}
class Derived : Base
{
public new void Func()
{
Console.WriteLine("Derived::Func");
}
public override void VirtualFunc()
{
Console.WriteLine("Derived::VirtualFunc");
}
public override void AbstractFunc()
{
Console.WriteLine("Derived::AbstractFunc");
}
}
static void Main(string[] args)
{
object obj = new Derived();
Console.WriteLine("----------regular func override----------");
(obj as Base).Func();
(obj as Derived).Func();
Console.WriteLine("----------virtual func override----------");
(obj as Base).VirtualFunc();
(obj as Derived).VirtualFunc();
Console.WriteLine("----------abstract func override----------");
(obj as Base).AbstractFunc();
(obj as Derived).AbstractFunc();
}
输出:
----------regular func override----------
Base::Func
Derived::Func
----------virtual func override----------
Derived::VirtualFunc
Derived::VirtualFunc
----------abstract func override----------
Derived::AbstractFunc
Derived::AbstractFunc
【2019-08-17】【2】C#类访问性修饰符 internal public
C#类只能被两个访问修饰符修饰internal public
internal表示类不能够跨模块访问,public表示类可以跨模块访问
internal修饰的子类可以继承public修饰的父类,而public修饰的子类不能继承internal修饰的父类
【2019-08-17】【3】C#类继承修饰符 abstract sealed
abstract修饰的类对象不能被实例化,在此类中abstract修饰的函数不能带有实现,该类必须作为另一个
子类的父类
sealed修饰的类不能被其他子类继承,限制该类为继承层次之中的最后一层
【2019-08-17】【4】WPF Command与RoutedCommand
// https://stackoverflow.com/questions/1135983/wpf-icommand-vs-routedcommand
WPF Command作用是为了让界面业务逻辑与界面(View)分离
普通继承IComand接口的RelayCommand本身实现了业务逻辑
而RoutedCommand本身没有实现业务逻辑,其业务逻辑是通过命令绑定CommandBinding绑定的
并且CommandBinding只能绑定RoutedCommand
例:
<Window x:Class="MiniWindow.CommandTest"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MiniWindow"
Title="CommandTest" Height="330" Width="300"
>
<Window.Resources>
<local:ViewModel x:Key="ViewModel"/>
</Window.Resources>
<StackPanel>
<Button x:Name="ViewModelButton" Content="ButtonA" Command="{Binding mCommand, Source={StaticResource ViewModel}}" CommandParameter="{Binding ElementName=ViewModelButton}"></Button>
<Button x:Name="RoutedCommandButton" Content="ButtonB"></Button>
</StackPanel>
</Window>
class ButtonCommand : ICommand
{
Action<object> mAction;
Func<object, bool> mFunc;
public event EventHandler CanExecuteChanged = (sender, e) => { };
public bool CanExecute(object parameter)
{
return mFunc(parameter);
}
public void Execute(object parameter)
{
mAction(parameter);
}
public ButtonCommand(Action<object> execute, Func<object, bool> canExecute)
{
mAction = execute;
mFunc = canExecute;
}
}
class ViewModel : INotifyPropertyChanged
{
ICommand _Command;
public ICommand mCommand
{
get
{
return _Command;
}
set
{
_Command = value;
PropertyChanged(this, new PropertyChangedEventArgs("mCommand"));
}
}
public ViewModel()
{
mCommand = new ButtonCommand(
(parameter) =>
{
MessageBox.Show("I am clicked(ViewModel)");
},
(parameter) =>
{
return true;
}
);
}
public event PropertyChangedEventHandler PropertyChanged = (sender, e) => { };
};
/// <summary>
/// CommandTest.xaml 的交互逻辑
/// </summary>
public partial class CommandTest : Window
{
public CommandTest()
{
InitializeComponent();
// 创建一个RoutedCommand
RoutedCommand routedCommand = new RoutedCommand("MyCommand", typeof(CommandTest));
// 将这个RoutedCommand绑定到一个按钮上
RoutedCommandButton.Command = routedCommand;
// 指定RoutedCommand的目标 这将会成为ExecutedRoutedEventHandler CanExecuteRoutedEventHandler 事件被调用时候的
// RoutedEventArgs类型参数的 Source 成员
RoutedCommandButton.CommandTarget = ViewModelButton;
// 绑定Command 指定 RoutedCommand ExecutedRoutedEventHandler CanExecuteRoutedEventHandler
CommandBinding binding = new CommandBinding(
routedCommand,
new ExecutedRoutedEventHandler(
(sender, e) =>
{
MessageBox.Show(string.Format("I am clicked(RoutedCommand Source{0}", e.Source));
e.Handled = true;
}),
new CanExecuteRoutedEventHandler(
(sender, e) =>
{
e.CanExecute = true;
})
);
// 在层次结构中添加绑定
this.CommandBindings.Add(binding);
// 显式调用Command
routedCommand.Execute(null, this);
}
}
【2019-08-17】【5】C# partial关键字
C#允许使用partial关键字修饰类使得类的实现可以拆开编写
例:
partial class TestClass
{
public void FuncA()
{
Console.WriteLine("TestClass::FuncA");
}
};
partial class TestClass
{
public void FuncB()
{
Console.WriteLine("TestClass::FuncB");
}
};
class Program
{
static void Main(string[] args)
{
TestClass t = new TestClass();
t.FuncA();
t.FuncB();
}
}
输出:
TestClass::FuncA
TestClass::FuncB
【2019-08-21】修复Python ImportError: No module named pkg_resources
# https://www.easyaslinux.com/quick-fix/fix-for-importerror-no-module-named-pkg_resources-error/
通常这个问题是因为升级python造成的,一个简单的方法是通过pip卸载掉原来的setuptools再重新安装新的setuptools
pip uninstall setuptools
pip install setuptools
【2019-09-03】C# Equals与==
// https://blog.csdn.net/liuchang19950703/article/details/82915380
1.值类型上 Equals与==运算符行为一致,都是比较内容是否相等
2.引用类型上 默认的Equals方法比较对象地址是否相同
3.引用类型上 默认的==运算符就是比较对象地址是否相同(与默认的Equals没有区别 但默认==运算符没有调用到对象的Equals方法)
4.重写对象==运算符方法 必须同时重写!=运算符方法
5.重写对象==运算符方法或者Equals方法必须重写GetHashCode方法
6.若要比较两个列表内容是否相同 最好调用List对象的SequenceEqual方法
7.可以使用对象ReferenceEquals方法比较值类型或者引用类型 地址是否相等
例:
public struct TestStruct
{
public int x;
}
public class TestClass
{
public List<TestStruct> list;
int x;
public TestClass(int val)
{
list = new List<TestStruct>();
x = val;
}
// 重写Equals方法
public override bool Equals(object rhs)
{
if (!list.SequenceEqual(((TestClass)rhs).list))
return false;
if (x != ((TestClass)rhs).x)
return false;
return true;
}
public static bool operator ==(TestClass lhs, TestClass rhs)
{
return lhs.Equals(rhs);
}
// 重写 operator == 方法必须重写operator !=方法
public static bool operator !=(TestClass lhs, TestClass rhs)
{
return !(lhs == rhs);
}
// 重写 Equals 或者 operator == 方法必须重写GetHashCode方法
public override int GetHashCode()
{
return list.GetHashCode() * x.GetHashCode();
}
}
class Program
{
static void Main(string[] args)
{
{
Console.WriteLine("----------Value----------");
int x = 0;
int y = 0;
// 值类型 比较值是否相等
Console.WriteLine("-----Equal-----");
bool equal = Equals(x, y);
Console.WriteLine(string.Format("{0}", equal));
// 值类型 比较对用是否相等
Console.WriteLine("-----ReferenceEquals-----");
bool refEqual = ReferenceEquals(x, y);
Console.WriteLine(string.Format("{0}", refEqual));
// 值类型 比较值是否相等
Console.WriteLine("-----object.Equals-----");
bool selfEqual = x.Equals(y);
Console.WriteLine(string.Format("{0}", selfEqual));
// 值类型 比较值是否相等
Console.WriteLine("----- == -----");
bool opEqualt = x == y;
Console.WriteLine(string.Format("{0}", opEqualt));
}
{
Console.WriteLine("----------Object----------");
TestClass a = new TestClass(10);
TestClass b = new TestClass(10);
a.list.Add(
new TestStruct
{
x = 20
});
b.list.Add(
new TestStruct
{
x = 20
});
// 引用类型 调用object Equals方法
// 如果对象重写了Equals方法则调用之
Console.WriteLine("-----Equal-----");
bool equal = Equals(a, b);
Console.WriteLine(string.Format("{0}", equal));
// 引用类型 比较引用是否相等
Console.WriteLine("-----ReferenceEquals-----");
bool refEqual = ReferenceEquals(a, b);
Console.WriteLine(string.Format("{0}", refEqual));
// 引用类型 调用对象Equal方法
Console.WriteLine("-----object.Equals-----");
bool selfEqual = a.Equals(b);
Console.WriteLine(string.Format("{0}", selfEqual));
// 引用类型 调用对象operator ==方法
Console.WriteLine("----- == -----");
bool opEqualt = a == b;
Console.WriteLine(string.Format("{0}", opEqualt));
}
}
}
输出:
----------Value----------
-----Equal-----
True
-----ReferenceEquals-----
False
-----object.Equals-----
True
----- == -----
True
----------Object----------
-----Equal-----
True
-----ReferenceEquals-----
False
-----object.Equals-----
True
----- == -----
True