From 442bffa865c8f7b3ca590317ab83b1a9b2227aad Mon Sep 17 00:00:00 2001 From: Niels Laute Date: Mon, 21 Aug 2023 17:40:24 +0200 Subject: [PATCH 1/3] Adding NavigateToUriBehavior --- components/Behaviors/samples/Behaviors.md | 5 +++ .../samples/NavigateToUriBehaviorSample.xaml | 15 +++++++ .../NavigateToUriBehaviorSample.xaml.cs | 16 +++++++ .../Behaviors/src/NavigateToUriBehavior.cs | 43 +++++++++++++++++++ 4 files changed, 79 insertions(+) create mode 100644 components/Behaviors/samples/NavigateToUriBehaviorSample.xaml create mode 100644 components/Behaviors/samples/NavigateToUriBehaviorSample.xaml.cs create mode 100644 components/Behaviors/src/NavigateToUriBehavior.cs diff --git a/components/Behaviors/samples/Behaviors.md b/components/Behaviors/samples/Behaviors.md index 7188a217..8157deee 100644 --- a/components/Behaviors/samples/Behaviors.md +++ b/components/Behaviors/samples/Behaviors.md @@ -42,3 +42,8 @@ A control only receives focus if it is enabled and loaded into the visual tree: Empty lists do not receive focus: > [!Sample FocusBehaviorListSample] + +## NavigateToUriBehavior +This behavior allows you to define a Uri in XAML, similiar to a `HyperlinkButton`. + +> [!Sample NavigateToUriBehaviorSample] diff --git a/components/Behaviors/samples/NavigateToUriBehaviorSample.xaml b/components/Behaviors/samples/NavigateToUriBehaviorSample.xaml new file mode 100644 index 00000000..0cdc8226 --- /dev/null +++ b/components/Behaviors/samples/NavigateToUriBehaviorSample.xaml @@ -0,0 +1,15 @@ + + + + diff --git a/components/Behaviors/samples/NavigateToUriBehaviorSample.xaml.cs b/components/Behaviors/samples/NavigateToUriBehaviorSample.xaml.cs new file mode 100644 index 00000000..04466889 --- /dev/null +++ b/components/Behaviors/samples/NavigateToUriBehaviorSample.xaml.cs @@ -0,0 +1,16 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using CommunityToolkit.WinUI.Behaviors; + +namespace BehaviorsExperiment.Samples; + +[ToolkitSample(id: nameof(NavigateToUriBehaviorSample), nameof(NavigateToUriBehavior), description: $"A sample demonstrating how to use {nameof(NavigateToUriBehavior)}.")] +public sealed partial class NavigateToUriBehaviorSample : Page +{ + public NavigateToUriBehaviorSample() + { + this.InitializeComponent(); + } +} diff --git a/components/Behaviors/src/NavigateToUriBehavior.cs b/components/Behaviors/src/NavigateToUriBehavior.cs new file mode 100644 index 00000000..b501bc06 --- /dev/null +++ b/components/Behaviors/src/NavigateToUriBehavior.cs @@ -0,0 +1,43 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using Microsoft.Xaml.Interactivity; + +namespace CommunityToolkit.WinUI.Behaviors; + +public sealed partial class NavigateToUriBehavior : DependencyObject, IAction +{ + /// + /// Gets or sets the linked instance to invoke. + /// + public Uri NavigateUri + { + get => (Uri)GetValue(NavigateUriProperty); + set => SetValue(NavigateUriProperty, value); + } + + /// + /// Identifies the dependency property. + /// + public static readonly DependencyProperty NavigateUriProperty = DependencyProperty.Register( + nameof(NavigateUri), + typeof(Uri), + typeof(NavigateToUriBehavior), + new PropertyMetadata(null)); + + /// + public object Execute(object sender, object parameter) + { + if (NavigateUri != null) + { + _ = Windows.System.Launcher.LaunchUriAsync(NavigateUri); + } + else + { + throw new ArgumentNullException(nameof(NavigateUri)); + } + + return true; + } +} From d1162b4434849927ba625d2f1c6f9ab87c2fb03a Mon Sep 17 00:00:00 2001 From: Niels Laute Date: Tue, 22 Aug 2023 10:47:33 +0200 Subject: [PATCH 2/3] Address feedback --- components/Behaviors/samples/Behaviors.md | 6 +++--- ...ehaviorSample.xaml => NavigateToUriActionSample.xaml} | 4 ++-- ...rSample.xaml.cs => NavigateToUriActionSample.xaml.cs} | 6 +++--- .../{NavigateToUriBehavior.cs => NavigateToUriAction.cs} | 9 ++++++--- 4 files changed, 14 insertions(+), 11 deletions(-) rename components/Behaviors/samples/{NavigateToUriBehaviorSample.xaml => NavigateToUriActionSample.xaml} (76%) rename components/Behaviors/samples/{NavigateToUriBehaviorSample.xaml.cs => NavigateToUriActionSample.xaml.cs} (54%) rename components/Behaviors/src/{NavigateToUriBehavior.cs => NavigateToUriAction.cs} (69%) diff --git a/components/Behaviors/samples/Behaviors.md b/components/Behaviors/samples/Behaviors.md index 8157deee..f410cb55 100644 --- a/components/Behaviors/samples/Behaviors.md +++ b/components/Behaviors/samples/Behaviors.md @@ -43,7 +43,7 @@ A control only receives focus if it is enabled and loaded into the visual tree: Empty lists do not receive focus: > [!Sample FocusBehaviorListSample] -## NavigateToUriBehavior -This behavior allows you to define a Uri in XAML, similiar to a `HyperlinkButton`. +## NavigateToUriAction +This behavior allows you to define a Uri in XAML, similiar to a `Hyperlink` or `HyperlinkButton`. This allows you to use a `Button` and still define the Uri in XAML without wiring up the `Click` event in code-behind, or restyling a `HyperlinkButton`. -> [!Sample NavigateToUriBehaviorSample] +> [!Sample NavigateToUriActionSample] diff --git a/components/Behaviors/samples/NavigateToUriBehaviorSample.xaml b/components/Behaviors/samples/NavigateToUriActionSample.xaml similarity index 76% rename from components/Behaviors/samples/NavigateToUriBehaviorSample.xaml rename to components/Behaviors/samples/NavigateToUriActionSample.xaml index 0cdc8226..980a9d94 100644 --- a/components/Behaviors/samples/NavigateToUriBehaviorSample.xaml +++ b/components/Behaviors/samples/NavigateToUriActionSample.xaml @@ -1,4 +1,4 @@ - - + diff --git a/components/Behaviors/samples/NavigateToUriBehaviorSample.xaml.cs b/components/Behaviors/samples/NavigateToUriActionSample.xaml.cs similarity index 54% rename from components/Behaviors/samples/NavigateToUriBehaviorSample.xaml.cs rename to components/Behaviors/samples/NavigateToUriActionSample.xaml.cs index 04466889..5114d071 100644 --- a/components/Behaviors/samples/NavigateToUriBehaviorSample.xaml.cs +++ b/components/Behaviors/samples/NavigateToUriActionSample.xaml.cs @@ -6,10 +6,10 @@ namespace BehaviorsExperiment.Samples; -[ToolkitSample(id: nameof(NavigateToUriBehaviorSample), nameof(NavigateToUriBehavior), description: $"A sample demonstrating how to use {nameof(NavigateToUriBehavior)}.")] -public sealed partial class NavigateToUriBehaviorSample : Page +[ToolkitSample(id: nameof(NavigateToUriActionSample), nameof(NavigateToUriAction), description: $"A sample demonstrating how to use {nameof(NavigateToUriAction)}.")] +public sealed partial class NavigateToUriActionSample : Page { - public NavigateToUriBehaviorSample() + public NavigateToUriActionSample() { this.InitializeComponent(); } diff --git a/components/Behaviors/src/NavigateToUriBehavior.cs b/components/Behaviors/src/NavigateToUriAction.cs similarity index 69% rename from components/Behaviors/src/NavigateToUriBehavior.cs rename to components/Behaviors/src/NavigateToUriAction.cs index b501bc06..2c14c4b4 100644 --- a/components/Behaviors/src/NavigateToUriBehavior.cs +++ b/components/Behaviors/src/NavigateToUriAction.cs @@ -6,10 +6,13 @@ namespace CommunityToolkit.WinUI.Behaviors; -public sealed partial class NavigateToUriBehavior : DependencyObject, IAction +/// +/// NavigateToUriAction represents an action that allows navigate to a specified URL defined in XAML, similiar to a and . Not action will be invoked if the Uri cannot be navigated to. +/// +public sealed partial class NavigateToUriAction : DependencyObject, IAction { /// - /// Gets or sets the linked instance to invoke. + /// Gets or sets the Uniform Resource Identifier (URI) to navigate to when the object is clicked. /// public Uri NavigateUri { @@ -23,7 +26,7 @@ public Uri NavigateUri public static readonly DependencyProperty NavigateUriProperty = DependencyProperty.Register( nameof(NavigateUri), typeof(Uri), - typeof(NavigateToUriBehavior), + typeof(NavigateToUriAction), new PropertyMetadata(null)); /// From 80e23d25f4d8676c5413dfd9e3b7d78f96397136 Mon Sep 17 00:00:00 2001 From: Niels Laute Date: Tue, 22 Aug 2023 14:09:05 +0200 Subject: [PATCH 3/3] Comment tweak --- components/Behaviors/src/NavigateToUriAction.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/Behaviors/src/NavigateToUriAction.cs b/components/Behaviors/src/NavigateToUriAction.cs index 2c14c4b4..5659ff67 100644 --- a/components/Behaviors/src/NavigateToUriAction.cs +++ b/components/Behaviors/src/NavigateToUriAction.cs @@ -7,7 +7,7 @@ namespace CommunityToolkit.WinUI.Behaviors; /// -/// NavigateToUriAction represents an action that allows navigate to a specified URL defined in XAML, similiar to a and . Not action will be invoked if the Uri cannot be navigated to. +/// NavigateToUriAction represents an action that allows navigate to a specified URL defined in XAML, similiar to a Hyperlink and HyperlinkButton. No action will be invoked if the Uri cannot be navigated to. /// public sealed partial class NavigateToUriAction : DependencyObject, IAction {