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 2 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]

## NavigateToUriBehavior
This behavior allows you to define a Uri in XAML, similiar to a `HyperlinkButton`.

> [!Sample NavigateToUriBehaviorSample]
15 changes: 15 additions & 0 deletions components/Behaviors/samples/NavigateToUriBehaviorSample.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Page x:Class="BehaviorsExperiment.Samples.NavigateToUriBehaviorSample"
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:NavigateToUriBehavior NavigateUri="https://aka.ms/toolkit/windows" />
</core:EventTriggerBehavior>
</interactivity:Interaction.Behaviors>
</Button>
</Page>
16 changes: 16 additions & 0 deletions components/Behaviors/samples/NavigateToUriBehaviorSample.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(NavigateToUriBehaviorSample), nameof(NavigateToUriBehavior), description: $"A sample demonstrating how to use {nameof(NavigateToUriBehavior)}.")]
public sealed partial class NavigateToUriBehaviorSample : Page
{
public NavigateToUriBehaviorSample()
{
this.InitializeComponent();
}
}
43 changes: 43 additions & 0 deletions components/Behaviors/src/NavigateToUriBehavior.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Gets or sets the linked <see cref="NavigateUri"/> instance to invoke.
/// </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(NavigateToUriBehavior),
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;
}
}