Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Porting NavigateToUriBehavior #179

Merged
merged 5 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions components/Behaviors/samples/Behaviors.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]
15 changes: 15 additions & 0 deletions components/Behaviors/samples/NavigateToUriActionSample.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Page x:Class="BehaviorsExperiment.Samples.NavigateToUriActionSample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:behaviors="using:CommunityToolkit.WinUI.Behaviors"
xmlns:core="using:Microsoft.Xaml.Interactions.Core"
xmlns:interactivity="using:Microsoft.Xaml.Interactivity">

<Button Content="Click to navigate">
<interactivity:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="Click">
<behaviors:NavigateToUriAction NavigateUri="https://aka.ms/toolkit/windows" />
</core:EventTriggerBehavior>
</interactivity:Interaction.Behaviors>
</Button>
</Page>
16 changes: 16 additions & 0 deletions components/Behaviors/samples/NavigateToUriActionSample.xaml.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
46 changes: 46 additions & 0 deletions components/Behaviors/src/NavigateToUriAction.cs
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// 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.
/// </summary>
public sealed partial class NavigateToUriAction : DependencyObject, IAction
{
/// <summary>
/// Gets or sets the Uniform Resource Identifier (URI) to navigate to when the object is clicked.
/// </summary>
public Uri NavigateUri
{
get => (Uri)GetValue(NavigateUriProperty);
set => SetValue(NavigateUriProperty, value);
}

/// <summary>
/// Identifies the <seealso cref="NavigateUri"/> dependency property.
/// </summary>
public static readonly DependencyProperty NavigateUriProperty = DependencyProperty.Register(
nameof(NavigateUri),
typeof(Uri),
typeof(NavigateToUriAction),
new PropertyMetadata(null));

/// <inheritdoc/>
public object Execute(object sender, object parameter)
{
if (NavigateUri != null)
{
_ = Windows.System.Launcher.LaunchUriAsync(NavigateUri);
}
else
{
throw new ArgumentNullException(nameof(NavigateUri));
}

return true;
}
}