Condividi tramite


SettingsCard

SettingsCard è un controllo che può essere usato per visualizzare le impostazioni nell'esperienza. Usa lo stile predefinito trovato in Windows 11 ed è facile da usare, soddisfa tutti gli standard di accessibilità e renderà la pagina delle impostazioni ideale! È possibile impostare le Headerproprietà , HeaderIcon Descriptione Content per creare un'esperienza facile da usare, come illustrato di seguito:

<!--  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.  -->
<Page x:Class="SettingsControlsExperiment.Samples.SettingsCardSample"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:controls="using:CommunityToolkit.WinUI.Controls"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      xmlns:ui="using:CommunityToolkit.WinUI"
      mc:Ignorable="d">
    <StackPanel Spacing="4">

        <controls:SettingsCard x:Name="settingsCard"
                               Description="This is a default card, with the Header, HeaderIcon, Description and Content set."
                               Header="This is the Header"
                               HeaderIcon="{ui:FontIcon Glyph=}"
                               IsEnabled="{x:Bind IsCardEnabled, Mode=OneWay}">
            <ComboBox SelectedIndex="0">
                <ComboBoxItem>Option 1</ComboBoxItem>
                <ComboBoxItem>Option 2</ComboBoxItem>
                <ComboBoxItem>Option 3</ComboBoxItem>
            </ComboBox>
        </controls:SettingsCard>

        <controls:SettingsCard Description="You can use a FontIcon, SymbolIcon or BitmapIcon to set the cards HeaderIcon."
                               Header="Icon options"
                               HeaderIcon="{ui:BitmapIcon Source=ms-appx:///Assets/AppTitleBar.scale-200.png}"
                               IsEnabled="{x:Bind IsCardEnabled, Mode=OneWay}">
            <ToggleSwitch />
        </controls:SettingsCard>

        <controls:SettingsCard Header="A card with custom objects as its Description"
                               IsEnabled="{x:Bind IsCardEnabled, Mode=OneWay}">
            <controls:SettingsCard.Description>
                <HyperlinkButton Content="Learn more about Phone Link" />
            </controls:SettingsCard.Description>
            <Button Content="Open Phone Link"
                    Style="{StaticResource AccentButtonStyle}" />
        </controls:SettingsCard>

        <controls:SettingsCard Description="When resizing a SettingsCard, the Content will wrap vertically. You can override this breakpoint by setting the SettingsCardWrapThreshold resource. For edge cases, you can also hide the icon by setting SettingsCardWrapNoIconThreshold."
                               Header="Adaptive layouts"
                               HeaderIcon="{ui:FontIcon Glyph=}"
                               IsEnabled="{x:Bind IsCardEnabled, Mode=OneWay}">
            <controls:SettingsCard.Resources>
                <x:Double x:Key="SettingsCardWrapThreshold">800</x:Double>
                <x:Double x:Key="SettingsCardWrapNoIconThreshold">600</x:Double>
            </controls:SettingsCard.Resources>
            <Button Content="This control will wrap vertically!"
                    Style="{StaticResource AccentButtonStyle}" />
        </controls:SettingsCard>

        <controls:SettingsCard Header="This is a card with a Header only" />
    </StackPanel>
</Page>
// 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.

namespace SettingsControlsExperiment.Samples;

[ToolkitSampleBoolOption("IsCardEnabled", true, Title = "Is Enabled")]

[ToolkitSample(id: nameof(SettingsCardSample), "SettingsCard", description: "A sample for showing how SettingsCard can be static or clickable.")]
public sealed partial class SettingsCardSample : Page
{
    public SettingsCardSample()
    {
        this.InitializeComponent();
    }
}

SettingsCard può anche essere trasformato in un pulsante impostando la IsClickEnabled proprietà . Ciò può essere utile ogni volta che si desidera che il componente delle impostazioni passi a una pagina di dettaglio o a un collegamento esterno. È possibile impostare un'icona personalizzata impostando o ActionIconnascondendola completamente impostando su falseIsActionIconVisible .

<!--  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.  -->
<Page x:Class="SettingsControlsExperiment.Samples.ClickableSettingsCardSample"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:controls="using:CommunityToolkit.WinUI.Controls"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      xmlns:ui="using:CommunityToolkit.WinUI"
      mc:Ignorable="d">
    <StackPanel Spacing="4">
        <controls:SettingsCard x:Name="settingsCard"
                               Click="OnCardClicked"
                               Description="A SettingsCard can be made clickable and you can leverage the Command property or Click event."
                               Header="A clickable SettingsCard"
                               HeaderIcon="{ui:FontIcon Glyph=}"
                               IsClickEnabled="True"
                               IsEnabled="{x:Bind IsCardEnabled, Mode=OneWay}">
            <TextBlock Foreground="{ThemeResource TextFillColorSecondaryBrush}"
                       Text="This is content" />
        </controls:SettingsCard>

        <controls:SettingsCard ActionIcon="{ui:FontIcon Glyph=}"
                               ActionIconToolTip="Open in new window"
                               Click="OnCardClicked"
                               Description="You can customize the ActionIcon and ActionIconToolTip."
                               Header="Customizing the ActionIcon"
                               HeaderIcon="{ui:FontIcon Glyph=}"
                               IsClickEnabled="True"
                               IsEnabled="{x:Bind IsCardEnabled, Mode=OneWay}" />

        <controls:SettingsCard Click="OnCardClicked"
                               Header="Hiding the ActionIcon"
                               HeaderIcon="{ui:FontIcon Glyph=}"
                               IsActionIconVisible="False"
                               IsClickEnabled="True"
                               IsEnabled="{x:Bind IsCardEnabled, Mode=OneWay}" />
    </StackPanel>
</Page>
// 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 System.ComponentModel;

namespace SettingsControlsExperiment.Samples;

[ToolkitSampleBoolOption("IsCardEnabled", true, Title = "Is Enabled")]

[ToolkitSample(id: nameof(ClickableSettingsCardSample), "ClickableSettingsCardSample", description: "A sample for showing how SettingsCard can be static or clickable.")]
public sealed partial class ClickableSettingsCardSample : Page
{
    public ClickableSettingsCardSample()
    {
        this.InitializeComponent();
    }

    private async void OnCardClicked(object sender, RoutedEventArgs e)
    {
        await Windows.System.Launcher.LaunchUriAsync(new Uri("https://www.microsoft.com"));
    }
}