diff --git a/components/Behaviors/samples/Behaviors.md b/components/Behaviors/samples/Behaviors.md index 7188a217..f410cb55 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] + +## 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 NavigateToUriActionSample] diff --git a/components/Behaviors/samples/NavigateToUriActionSample.xaml b/components/Behaviors/samples/NavigateToUriActionSample.xaml new file mode 100644 index 00000000..980a9d94 --- /dev/null +++ b/components/Behaviors/samples/NavigateToUriActionSample.xaml @@ -0,0 +1,15 @@ + + + + diff --git a/components/Behaviors/samples/NavigateToUriActionSample.xaml.cs b/components/Behaviors/samples/NavigateToUriActionSample.xaml.cs new file mode 100644 index 00000000..5114d071 --- /dev/null +++ b/components/Behaviors/samples/NavigateToUriActionSample.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(NavigateToUriActionSample), nameof(NavigateToUriAction), description: $"A sample demonstrating how to use {nameof(NavigateToUriAction)}.")] +public sealed partial class NavigateToUriActionSample : Page +{ + public NavigateToUriActionSample() + { + this.InitializeComponent(); + } +} diff --git a/components/Behaviors/src/NavigateToUriAction.cs b/components/Behaviors/src/NavigateToUriAction.cs new file mode 100644 index 00000000..5659ff67 --- /dev/null +++ b/components/Behaviors/src/NavigateToUriAction.cs @@ -0,0 +1,46 @@ +// 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; + +/// +/// 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 +{ + /// + /// Gets or sets the Uniform Resource Identifier (URI) to navigate to when the object is clicked. + /// + 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(NavigateToUriAction), + 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; + } +}