Skip to content

Commit d3a1be7

Browse files
authored
Porting NavigateToUriBehavior (#179)
* Adding NavigateToUriBehavior * Address feedback * Comment tweak
1 parent 24d0559 commit d3a1be7

File tree

4 files changed

+82
-0
lines changed

4 files changed

+82
-0
lines changed

components/Behaviors/samples/Behaviors.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,8 @@ A control only receives focus if it is enabled and loaded into the visual tree:
4242
4343
Empty lists do not receive focus:
4444
> [!Sample FocusBehaviorListSample]
45+
46+
## NavigateToUriAction
47+
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`.
48+
49+
> [!Sample NavigateToUriActionSample]
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Page x:Class="BehaviorsExperiment.Samples.NavigateToUriActionSample"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:behaviors="using:CommunityToolkit.WinUI.Behaviors"
5+
xmlns:core="using:Microsoft.Xaml.Interactions.Core"
6+
xmlns:interactivity="using:Microsoft.Xaml.Interactivity">
7+
8+
<Button Content="Click to navigate">
9+
<interactivity:Interaction.Behaviors>
10+
<core:EventTriggerBehavior EventName="Click">
11+
<behaviors:NavigateToUriAction NavigateUri="https://aka.ms/toolkit/windows" />
12+
</core:EventTriggerBehavior>
13+
</interactivity:Interaction.Behaviors>
14+
</Button>
15+
</Page>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using CommunityToolkit.WinUI.Behaviors;
6+
7+
namespace BehaviorsExperiment.Samples;
8+
9+
[ToolkitSample(id: nameof(NavigateToUriActionSample), nameof(NavigateToUriAction), description: $"A sample demonstrating how to use {nameof(NavigateToUriAction)}.")]
10+
public sealed partial class NavigateToUriActionSample : Page
11+
{
12+
public NavigateToUriActionSample()
13+
{
14+
this.InitializeComponent();
15+
}
16+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using Microsoft.Xaml.Interactivity;
6+
7+
namespace CommunityToolkit.WinUI.Behaviors;
8+
9+
/// <summary>
10+
/// 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.
11+
/// </summary>
12+
public sealed partial class NavigateToUriAction : DependencyObject, IAction
13+
{
14+
/// <summary>
15+
/// Gets or sets the Uniform Resource Identifier (URI) to navigate to when the object is clicked.
16+
/// </summary>
17+
public Uri NavigateUri
18+
{
19+
get => (Uri)GetValue(NavigateUriProperty);
20+
set => SetValue(NavigateUriProperty, value);
21+
}
22+
23+
/// <summary>
24+
/// Identifies the <seealso cref="NavigateUri"/> dependency property.
25+
/// </summary>
26+
public static readonly DependencyProperty NavigateUriProperty = DependencyProperty.Register(
27+
nameof(NavigateUri),
28+
typeof(Uri),
29+
typeof(NavigateToUriAction),
30+
new PropertyMetadata(null));
31+
32+
/// <inheritdoc/>
33+
public object Execute(object sender, object parameter)
34+
{
35+
if (NavigateUri != null)
36+
{
37+
_ = Windows.System.Launcher.LaunchUriAsync(NavigateUri);
38+
}
39+
else
40+
{
41+
throw new ArgumentNullException(nameof(NavigateUri));
42+
}
43+
44+
return true;
45+
}
46+
}

0 commit comments

Comments
 (0)