@@ -26,7 +26,6 @@ sealed class CSharpInstantiatorGenerator : InstantiatorGeneratorBase
26
26
{
27
27
readonly Stringifier _s ;
28
28
readonly string _winUiNamespace ;
29
- readonly string _winUi3CastHack ;
30
29
31
30
CSharpInstantiatorGenerator (
32
31
CodegenConfiguration configuration ,
@@ -41,15 +40,10 @@ sealed class CSharpInstantiatorGenerator : InstantiatorGeneratorBase
41
40
if ( AnimatedVisualSourceInfo . WinUIVersion . Major >= 3 )
42
41
{
43
42
_winUiNamespace = "Microsoft.UI" ;
44
-
45
- // This is a hack that is required to use Win2D with WinUI3 as of August 2020. It
46
- // will not be necessary when an official Win2D for WinUI3 is released.
47
- _winUi3CastHack = "(IGeometrySource2D)(object)" ;
48
43
}
49
44
else
50
45
{
51
46
_winUiNamespace = "Windows.UI" ;
52
- _winUi3CastHack = string . Empty ;
53
47
}
54
48
}
55
49
@@ -95,7 +89,15 @@ protected override void WriteImplementationFileStart(CodeBuilder builder)
95
89
if ( SourceInfo . UsesCanvasGeometry )
96
90
{
97
91
// Windows.Graphics is needed for IGeometrySource2D.
98
- namespaces . Add ( "Windows.Graphics" ) ;
92
+ if ( SourceInfo . WinUIVersion . Major < 3 )
93
+ {
94
+ namespaces . Add ( "Windows.Graphics" ) ;
95
+ }
96
+ else
97
+ {
98
+ namespaces . Add ( "Microsoft.Graphics" ) ;
99
+ }
100
+
99
101
namespaces . Add ( "Microsoft.Graphics.Canvas.Geometry" ) ;
100
102
}
101
103
@@ -472,7 +474,15 @@ void WriteIDynamicAnimatedVisualSource(CodeBuilder builder)
472
474
builder . WriteLine ( "bool _isImageLoadingAsynchronous;" ) ;
473
475
builder . WriteLine ( "bool _isTryCreateAnimatedVisualCalled;" ) ;
474
476
builder . WriteLine ( "bool _isImageLoadingStarted;" ) ;
475
- builder . WriteLine ( "EventRegistrationTokenTable<TypedEventHandler<IDynamicAnimatedVisualSource, object>> _animatedVisualInvalidatedEventTokenTable;" ) ;
477
+
478
+ if ( AnimatedVisualSourceInfo . WinUIVersion . Major >= 3 )
479
+ {
480
+ builder . WriteLine ( "HashSet<TypedEventHandler<IDynamicAnimatedVisualSource, object>> _animatedVisualInvalidatedEventTokenTable = new HashSet<TypedEventHandler<IDynamicAnimatedVisualSource, object>>();" ) ;
481
+ }
482
+ else
483
+ {
484
+ builder . WriteLine ( "EventRegistrationTokenTable<TypedEventHandler<IDynamicAnimatedVisualSource, object>> _animatedVisualInvalidatedEventTokenTable;" ) ;
485
+ }
476
486
477
487
// Declare the variables to hold the LoadedImageSurfaces.
478
488
foreach ( var n in SourceInfo . LoadedImageSurfaces )
@@ -587,10 +597,13 @@ void WriteIDynamicAnimatedVisualSource(CodeBuilder builder)
587
597
builder . WriteLine ( ) ;
588
598
589
599
// Generate the method that get or create the EventRegistrationTokenTable.
590
- builder . WriteLine ( "EventRegistrationTokenTable<TypedEventHandler<IDynamicAnimatedVisualSource, object>> GetAnimatedVisualInvalidatedEventRegistrationTokenTable()" ) ;
591
- builder . OpenScope ( ) ;
592
- builder . WriteLine ( "return EventRegistrationTokenTable<TypedEventHandler<IDynamicAnimatedVisualSource, object>>.GetOrCreateEventRegistrationTokenTable(ref _animatedVisualInvalidatedEventTokenTable);" ) ;
593
- builder . CloseScope ( ) ;
600
+ if ( AnimatedVisualSourceInfo . WinUIVersion . Major < 3 )
601
+ {
602
+ builder . WriteLine ( "EventRegistrationTokenTable<TypedEventHandler<IDynamicAnimatedVisualSource, object>> GetAnimatedVisualInvalidatedEventRegistrationTokenTable()" ) ;
603
+ builder . OpenScope ( ) ;
604
+ builder . WriteLine ( "return EventRegistrationTokenTable<TypedEventHandler<IDynamicAnimatedVisualSource, object>>.GetOrCreateEventRegistrationTokenTable(ref _animatedVisualInvalidatedEventTokenTable);" ) ;
605
+ builder . CloseScope ( ) ;
606
+ }
594
607
}
595
608
596
609
/// <summary>
@@ -860,7 +873,7 @@ protected override void WriteCanvasGeometryCombinationFactory(CodeBuilder builde
860
873
/// <inheritdoc/>
861
874
protected override void WriteCanvasGeometryEllipseFactory ( CodeBuilder builder , CanvasGeometry . Ellipse obj , string typeName , string fieldName )
862
875
{
863
- builder . WriteLine ( $ "var result = { FieldAssignment ( fieldName ) } { _winUi3CastHack } CanvasGeometry.CreateEllipse(") ;
876
+ builder . WriteLine ( $ "var result = { FieldAssignment ( fieldName ) } CanvasGeometry.CreateEllipse(") ;
864
877
builder . Indent ( ) ;
865
878
builder . WriteLine ( $ "null,") ;
866
879
builder . WriteLine ( $ "{ _s . Float ( obj . X ) } , { _s . Float ( obj . Y ) } , { _s . Float ( obj . RadiusX ) } , { _s . Float ( obj . RadiusY ) } );") ;
@@ -870,7 +883,7 @@ protected override void WriteCanvasGeometryEllipseFactory(CodeBuilder builder, C
870
883
/// <inheritdoc/>
871
884
protected override void WriteCanvasGeometryGroupFactory ( CodeBuilder builder , CanvasGeometry . Group obj , string typeName , string fieldName )
872
885
{
873
- builder . WriteLine ( $ "var result = { FieldAssignment ( fieldName ) } { _winUi3CastHack } CanvasGeometry.CreateGroup(") ;
886
+ builder . WriteLine ( $ "var result = { FieldAssignment ( fieldName ) } CanvasGeometry.CreateGroup(") ;
874
887
builder . Indent ( ) ;
875
888
builder . WriteLine ( $ "null,") ;
876
889
builder . WriteLine ( $ "new CanvasGeometry[] {{ { string . Join ( ", " , obj . Geometries . Select ( g => CallFactoryFor ( g ) ) ) } }},") ;
@@ -911,14 +924,14 @@ protected override void WriteCanvasGeometryPathFactory(CodeBuilder builder, Canv
911
924
}
912
925
}
913
926
914
- builder . WriteLine ( $ "result = { FieldAssignment ( fieldName ) } { _winUi3CastHack } CanvasGeometry.CreatePath(builder);") ;
927
+ builder . WriteLine ( $ "result = { FieldAssignment ( fieldName ) } CanvasGeometry.CreatePath(builder);") ;
915
928
builder . CloseScope ( ) ;
916
929
}
917
930
918
931
/// <inheritdoc/>
919
932
protected override void WriteCanvasGeometryRoundedRectangleFactory ( CodeBuilder builder , CanvasGeometry . RoundedRectangle obj , string typeName , string fieldName )
920
933
{
921
- builder . WriteLine ( $ "var result = { FieldAssignment ( fieldName ) } { _winUi3CastHack } CanvasGeometry.CreateRoundedRectangle(") ;
934
+ builder . WriteLine ( $ "var result = { FieldAssignment ( fieldName ) } CanvasGeometry.CreateRoundedRectangle(") ;
922
935
builder . Indent ( ) ;
923
936
builder . WriteLine ( "null," ) ;
924
937
builder . WriteLine ( $ "{ _s . Float ( obj . X ) } ,") ;
@@ -978,11 +991,28 @@ void WriteAnimatedVisualInvalidatedEvent(CodeBuilder builder)
978
991
builder . OpenScope ( ) ;
979
992
builder . WriteLine ( "add" ) ;
980
993
builder . OpenScope ( ) ;
981
- builder . WriteLine ( "return GetAnimatedVisualInvalidatedEventRegistrationTokenTable().AddEventHandler(value);" ) ;
994
+ if ( AnimatedVisualSourceInfo . WinUIVersion . Major >= 3 )
995
+ {
996
+ builder . WriteLine ( "_animatedVisualInvalidatedEventTokenTable.Add(value);" ) ;
997
+ }
998
+ else
999
+ {
1000
+ builder . WriteLine ( "return GetAnimatedVisualInvalidatedEventRegistrationTokenTable().AddEventHandler(value);" ) ;
1001
+ }
1002
+
982
1003
builder . CloseScope ( ) ;
983
1004
builder . WriteLine ( "remove" ) ;
984
1005
builder . OpenScope ( ) ;
985
- builder . WriteLine ( "GetAnimatedVisualInvalidatedEventRegistrationTokenTable().RemoveEventHandler(value);" ) ;
1006
+
1007
+ if ( AnimatedVisualSourceInfo . WinUIVersion . Major >= 3 )
1008
+ {
1009
+ builder . WriteLine ( "_animatedVisualInvalidatedEventTokenTable.Remove(value);" ) ;
1010
+ }
1011
+ else
1012
+ {
1013
+ builder . WriteLine ( "GetAnimatedVisualInvalidatedEventRegistrationTokenTable().RemoveEventHandler(value);" ) ;
1014
+ }
1015
+
986
1016
builder . CloseScope ( ) ;
987
1017
builder . CloseScope ( ) ;
988
1018
builder . WriteLine ( ) ;
@@ -1036,7 +1066,15 @@ void WriteHandleLoadCompleted(CodeBuilder builder)
1036
1066
// the previous result is now invalidated.
1037
1067
builder . WriteLine ( "if (_isImageLoadingAsynchronous)" ) ;
1038
1068
builder . OpenScope ( ) ;
1039
- builder . WriteLine ( "_animatedVisualInvalidatedEventTokenTable?.InvocationList?.Invoke(this, null);" ) ;
1069
+ if ( AnimatedVisualSourceInfo . WinUIVersion . Major >= 3 )
1070
+ {
1071
+ builder . WriteLine ( "foreach (var v in _animatedVisualInvalidatedEventTokenTable) v.Invoke(this, null);" ) ;
1072
+ }
1073
+ else
1074
+ {
1075
+ builder . WriteLine ( "_animatedVisualInvalidatedEventTokenTable?.InvocationList?.Invoke(this, null);" ) ;
1076
+ }
1077
+
1040
1078
builder . CloseScope ( ) ;
1041
1079
builder . CloseScope ( ) ;
1042
1080
builder . CloseScope ( ) ;
0 commit comments