Родитель
8e36dc0a9d
Коммит
5a555e6dbb
|
@ -21,6 +21,8 @@ namespace Microsoft.DotNet.UpgradeAssistant.Extensions.Windows
|
||||||
[ApplicableComponents(ProjectComponents.WinUI)]
|
[ApplicableComponents(ProjectComponents.WinUI)]
|
||||||
public class WinUIContentDialogCodeFixer : CodeFixProvider
|
public class WinUIContentDialogCodeFixer : CodeFixProvider
|
||||||
{
|
{
|
||||||
|
private const string DialogSetterComment = "/* TODO You should replace 'this' with the instance of UserControl that is ContentDialog is meant to be a part of. */";
|
||||||
|
|
||||||
// The Upgrade Assistant will only use analyzers that have an associated code fix provider registered including
|
// The Upgrade Assistant will only use analyzers that have an associated code fix provider registered including
|
||||||
// the analyzer's ID in the code fix provider's FixableDiagnosticIds array.
|
// the analyzer's ID in the code fix provider's FixableDiagnosticIds array.
|
||||||
public sealed override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(WinUIContentDialogAnalyzer.DiagnosticId);
|
public sealed override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(WinUIContentDialogAnalyzer.DiagnosticId);
|
||||||
|
@ -63,24 +65,27 @@ namespace Microsoft.DotNet.UpgradeAssistant.Extensions.Windows
|
||||||
{
|
{
|
||||||
var newMethodDeclarationSibling = contentDialogMemberAccess.Ancestors().OfType<MethodDeclarationSyntax>().First();
|
var newMethodDeclarationSibling = contentDialogMemberAccess.Ancestors().OfType<MethodDeclarationSyntax>().First();
|
||||||
|
|
||||||
var newMethodAccess = SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName("this"),
|
var newMethodCall = SyntaxFactory.InvocationExpression(SyntaxFactory.ParseExpression("SetContentDialogRoot"),
|
||||||
SyntaxFactory.IdentifierName("SetContentDialogRoot"));
|
SyntaxFactory.ArgumentList(SyntaxFactory.SeparatedList(new[]
|
||||||
var newMethodCall = SyntaxFactory.InvocationExpression(newMethodAccess,
|
{
|
||||||
SyntaxFactory.ArgumentList(SyntaxFactory.SeparatedList(new[] { SyntaxFactory.Argument(contentDialogMemberAccess.Expression) })));
|
SyntaxFactory.Argument(contentDialogMemberAccess.Expression),
|
||||||
|
SyntaxFactory.Argument(SyntaxFactory.ParseExpression("this"))
|
||||||
|
})));
|
||||||
|
|
||||||
|
var newMethodCallComment = SyntaxFactory.Comment(DialogSetterComment);
|
||||||
var documentEditor = await DocumentEditor.CreateAsync(document, cancellationToken).ConfigureAwait(false);
|
var documentEditor = await DocumentEditor.CreateAsync(document, cancellationToken).ConfigureAwait(false);
|
||||||
documentEditor.ReplaceNode(contentDialogMemberAccess.Expression, newMethodCall);
|
documentEditor.ReplaceNode(contentDialogMemberAccess.Expression, newMethodCall.WithLeadingTrivia(newMethodCallComment));
|
||||||
if (!newMethodDeclarationSibling.Parent!.ChildNodes().OfType<MethodDeclarationSyntax>()
|
if (!newMethodDeclarationSibling.Parent!.ChildNodes().OfType<MethodDeclarationSyntax>()
|
||||||
.Any(sibling => sibling.Identifier.ValueText == "SetContentDialogRoot"))
|
.Any(sibling => sibling.Identifier.ValueText == "SetContentDialogRoot"))
|
||||||
{
|
{
|
||||||
var newMethodRoot = await CSharpSyntaxTree.ParseText(@"
|
var newMethodRoot = await CSharpSyntaxTree.ParseText(@"
|
||||||
class A
|
class A
|
||||||
{
|
{
|
||||||
private ContentDialog SetContentDialogRoot(ContentDialog contentDialog)
|
private static ContentDialog SetContentDialogRoot(ContentDialog contentDialog, UserControl control)
|
||||||
{
|
{
|
||||||
if (Windows.Foundation.Metadata.ApiInformation.IsApiContractPresent(""Windows.Foundation.UniversalApiContract"", 8))
|
if (Windows.Foundation.Metadata.ApiInformation.IsApiContractPresent(""Windows.Foundation.UniversalApiContract"", 8))
|
||||||
{
|
{
|
||||||
contentDialog.XamlRoot = this.Content.XamlRoot;
|
contentDialog.XamlRoot = control.Content.XamlRoot;
|
||||||
}
|
}
|
||||||
return contentDialog;
|
return contentDialog;
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,6 +20,10 @@ namespace Microsoft.DotNet.UpgradeAssistant.Extensions.Windows
|
||||||
[ApplicableComponents(ProjectComponents.WinUI)]
|
[ApplicableComponents(ProjectComponents.WinUI)]
|
||||||
public class WinUIInitializeWindowCodeFixer : CodeFixProvider
|
public class WinUIInitializeWindowCodeFixer : CodeFixProvider
|
||||||
{
|
{
|
||||||
|
private const string WindowInitializerComment = @"/* TODO You should replace 'App.WindowHandle' with the your window's handle (HWND)
|
||||||
|
Read more on retrieving window handle here: https://docs.microsoft.com/en-us/windows/apps/develop/ui-input/retrieve-hwnd */
|
||||||
|
";
|
||||||
|
|
||||||
// The Upgrade Assistant will only use analyzers that have an associated code fix provider registered including
|
// The Upgrade Assistant will only use analyzers that have an associated code fix provider registered including
|
||||||
// the analyzer's ID in the code fix provider's FixableDiagnosticIds array.
|
// the analyzer's ID in the code fix provider's FixableDiagnosticIds array.
|
||||||
public sealed override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(DiagnosticId);
|
public sealed override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(DiagnosticId);
|
||||||
|
@ -66,12 +70,16 @@ namespace Microsoft.DotNet.UpgradeAssistant.Extensions.Windows
|
||||||
var newMethodDeclarationSibling = objectCreationDeclaration.Ancestors().OfType<MethodDeclarationSyntax>().First();
|
var newMethodDeclarationSibling = objectCreationDeclaration.Ancestors().OfType<MethodDeclarationSyntax>().First();
|
||||||
var typeName = objectCreationDeclaration.Type.ToString();
|
var typeName = objectCreationDeclaration.Type.ToString();
|
||||||
|
|
||||||
var newMethodAccess = SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName("this"),
|
var newMethodCall = SyntaxFactory.InvocationExpression(SyntaxFactory.ParseExpression("InitializeWithWindow"),
|
||||||
SyntaxFactory.IdentifierName("InitializeWithWindow"));
|
SyntaxFactory.ArgumentList(SyntaxFactory.SeparatedList(new[]
|
||||||
var newMethodCall = SyntaxFactory.InvocationExpression(newMethodAccess,
|
{
|
||||||
SyntaxFactory.ArgumentList(SyntaxFactory.SeparatedList(new[] { SyntaxFactory.Argument(objectCreationDeclaration) })));
|
SyntaxFactory.Argument(objectCreationDeclaration),
|
||||||
|
SyntaxFactory.Argument(SyntaxFactory.ParseExpression("App.WindowHandle"))
|
||||||
|
})));
|
||||||
|
|
||||||
|
var newMethodCallComment = SyntaxFactory.Comment(WindowInitializerComment);
|
||||||
|
documentEditor.ReplaceNode(objectCreationDeclaration, newMethodCall.WithLeadingTrivia(newMethodCallComment));
|
||||||
|
|
||||||
documentEditor.ReplaceNode(objectCreationDeclaration, newMethodCall);
|
|
||||||
if (!newMethodDeclarationSibling.Parent!.ChildNodes().OfType<MethodDeclarationSyntax>()
|
if (!newMethodDeclarationSibling.Parent!.ChildNodes().OfType<MethodDeclarationSyntax>()
|
||||||
.Any(sibling => sibling.Identifier.ValueText == "InitializeWithWindow"
|
.Any(sibling => sibling.Identifier.ValueText == "InitializeWithWindow"
|
||||||
&& sibling.ParameterList.Parameters.Any(parameter => parameter.Type!.ToString() == typeName)))
|
&& sibling.ParameterList.Parameters.Any(parameter => parameter.Type!.ToString() == typeName)))
|
||||||
|
@ -79,9 +87,9 @@ namespace Microsoft.DotNet.UpgradeAssistant.Extensions.Windows
|
||||||
var newMethodRoot = await CSharpSyntaxTree.ParseText(@$"
|
var newMethodRoot = await CSharpSyntaxTree.ParseText(@$"
|
||||||
class A
|
class A
|
||||||
{{
|
{{
|
||||||
private {typeName} InitializeWithWindow({typeName} obj)
|
private static {typeName} InitializeWithWindow({typeName} obj, IntPtr windowHandle)
|
||||||
{{
|
{{
|
||||||
WinRT.Interop.InitializeWithWindow.Initialize(obj, App.WindowHandle);
|
WinRT.Interop.InitializeWithWindow.Initialize(obj, windowHandle);
|
||||||
return obj;
|
return obj;
|
||||||
}}
|
}}
|
||||||
}}",
|
}}",
|
||||||
|
|
|
@ -20,13 +20,13 @@ namespace TestProject.TestClasses
|
||||||
PrimaryButtonText = "Leave this page",
|
PrimaryButtonText = "Leave this page",
|
||||||
SecondaryButtonText = "Stay"
|
SecondaryButtonText = "Stay"
|
||||||
};
|
};
|
||||||
ContentDialogResult result = this.SetContentDialogRoot(saveDialog).ShowAsync();
|
ContentDialogResult result = /* TODO You should replace 'this' with the instance of UserControl that is ContentDialog is meant to be a part of. */SetContentDialogRoot(saveDialog, this).ShowAsync();
|
||||||
}
|
}
|
||||||
private ContentDialog SetContentDialogRoot(ContentDialog contentDialog)
|
private static ContentDialog SetContentDialogRoot(ContentDialog contentDialog, UserControl control)
|
||||||
{
|
{
|
||||||
if (Windows.Foundation.Metadata.ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 8))
|
if (Windows.Foundation.Metadata.ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 8))
|
||||||
{
|
{
|
||||||
contentDialog.XamlRoot = this.Content.XamlRoot;
|
contentDialog.XamlRoot = control.Content.XamlRoot;
|
||||||
}
|
}
|
||||||
return contentDialog;
|
return contentDialog;
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,18 +14,22 @@ namespace TestProject.TestClasses
|
||||||
{
|
{
|
||||||
private async void CallContentDialog()
|
private async void CallContentDialog()
|
||||||
{
|
{
|
||||||
var filePicker = this.InitializeWithWindow(new FileSavePicker());
|
var filePicker = /* TODO You should replace 'App.WindowHandle' with the your window's handle (HWND)
|
||||||
var folderPicker = this.InitializeWithWindow(new FolderPicker());
|
Read more on retrieving window handle here: https://docs.microsoft.com/en-us/windows/apps/develop/ui-input/retrieve-hwnd */
|
||||||
|
InitializeWithWindow(new FileSavePicker(), App.WindowHandle);
|
||||||
|
var folderPicker = /* TODO You should replace 'App.WindowHandle' with the your window's handle (HWND)
|
||||||
|
Read more on retrieving window handle here: https://docs.microsoft.com/en-us/windows/apps/develop/ui-input/retrieve-hwnd */
|
||||||
|
InitializeWithWindow(new FolderPicker(), App.WindowHandle);
|
||||||
var fileOpenPicker = this.InitializeWithWindow(new FileOpenPicker());
|
var fileOpenPicker = this.InitializeWithWindow(new FileOpenPicker());
|
||||||
}
|
}
|
||||||
private FolderPicker InitializeWithWindow(FolderPicker obj)
|
private static FolderPicker InitializeWithWindow(FolderPicker obj, IntPtr windowHandle)
|
||||||
{
|
{
|
||||||
WinRT.Interop.InitializeWithWindow.Initialize(obj, App.WindowHandle);
|
WinRT.Interop.InitializeWithWindow.Initialize(obj, windowHandle);
|
||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
private FileSavePicker InitializeWithWindow(FileSavePicker obj)
|
private static FileSavePicker InitializeWithWindow(FileSavePicker obj, IntPtr windowHandle)
|
||||||
{
|
{
|
||||||
WinRT.Interop.InitializeWithWindow.Initialize(obj, App.WindowHandle);
|
WinRT.Interop.InitializeWithWindow.Initialize(obj, windowHandle);
|
||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Загрузка…
Ссылка в новой задаче