XAML 如何在WinUI3中创建事件或回调?

ewm0tg9j  于 2023-05-11  发布在  其他
关注(0)|答案(1)|浏览(142)

我有主要的XAML页面包括两个框架:工具栏和内容。我也有几个不同的页面为这个框架。我使用命令栏到工具栏框架。如何通过AppBarButton单击事件从Frame更改Frame(例如在主页面中的TaskerToolbar)?或者如何将单击事件从TaskerToolbar发送到MainPage.xaml。谢谢你的帮助。
MainPage.xaml:

<Grid>
    <Grid.RowDefinitions>
        <!-- toolbar-->
        <RowDefinition Height="110"/>
        <RowDefinition/>
    </Grid.RowDefinitions>

    <Border CornerRadius="0,0,0,10" BorderThickness="0,0,0,5">
        <Border.BorderBrush>
            <LinearGradientBrush StartPoint=".5,0" EndPoint=".5,1" Opacity="0.02">
                <GradientStop Color="Gray" Offset="0"/>
                <GradientStop Color="Black" Offset=".03"/>
                <GradientStop Color="Black" Offset=".98"/>
                <GradientStop Color="Gray" Offset="1"/>
            </LinearGradientBrush>
        </Border.BorderBrush>
        <Border CornerRadius="0,0,0,10" BorderBrush="#D4D4D4" BorderThickness="1,0,0,1">
            <!-- toolbar frame -->
            <Grid Grid.Row="0" Background="#F3F3F3">
                <Frame x:Name="toolbarFrame"/>
            </Grid>
        </Border>
    </Border>
    <!-- content frame -->
    <Grid Grid.Row="1">
        <Frame x:Name="contentFrame"/>
    </Grid>
</Grid>

MainPage.xaml.cs:

public MainChromeWindowView()
{
    this.InitializeComponent();

    toolbarFrame.Navigate(typeof(TaskerToolbar), null);
    contentFrame.Navigate(typeof(SingleAView), null);
}

TaskerToolbar(和其他工具栏):

<Grid>
    <CommandBar Style="{StaticResource OpenDownCommandBar}" HorizontalAlignment="Left" IsOpen="False" DefaultLabelPosition="Collapsed" Margin="0,15,0,0">
        <AppBarButton Style="{StaticResource ImageAppBarButtonStyle}" LabelPosition="Default" Label="Recorder">
            <AppBarButton.Content>
                <Image>
                    <Image.Source>
                        <SvgImageSource UriSource="/Assets/icons/icon1.svg"/>
                    </Image.Source>
                </Image>
            </AppBarButton.Content>
        </AppBarButton>
        <AppBarButton Style="{StaticResource ImageAppBarButtonStyle}" LabelPosition="Default" Label="Configuration">
            <AppBarButton.Content>
                <Image>
                    <Image.Source>
                        <SvgImageSource UriSource="/Assets/icons/icon2.svg"/>
                    </Image.Source>
                </Image>
            </AppBarButton.Content>
        </AppBarButton>
    </CommandBar>
</Grid>
kninwzqo

kninwzqo1#

您可以将事件添加到Page类:

TaskerToobar.xaml

<AppBarButton
    Click="RecorderButton_Click"
    Label="Recorder" />
<AppBarButton
    Click="ConfigurationButton_Click"
    Label="Configuration" />

TaskerToolbar.xaml.cs

public sealed partial class TaskerToolbar : Page
{
    public TaskerToolbar()
    {
        this.InitializeComponent();
    }

    public event EventHandler? RecorderButtonClicked;

    public event EventHandler? ConfigurationButtonClicked;

    private void RecorderButton_Click(object sender, RoutedEventArgs e)
    {
        RecorderButtonClicked?.Invoke(sender, EventArgs.Empty);
    }

    private void ConfigurationButton_Click(object sender, RoutedEventArgs e)
    {
        ConfigurationButtonClicked?.Invoke(sender, EventArgs.Empty);
    }
}

订阅/取消订阅如下:

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
        
        this.toolbarFrame.Navigating += ToolbarFrame_Navigating;
        this.toolbarFrame.Navigated += ToolbarFrame_Navigated;
        
        this.toolbarFrame.Navigate(typeof(TaskerToolbar), null);
        this.contentFrame.Navigate(typeof(SingleAView), null);
    }

    private void ToolbarFrame_Navigating(object sender, NavigatingCancelEventArgs e)
    {
        if (sender is Frame { Content: TaskerToolbar taskerToolbar })
        {
            taskerToolbar.RecorderButtonClicked -= TaskerToolbar_RecorderButtonClicked;
            taskerToolbar.ConfigurationButtonClicked -= TaskerToolbar_ConfigurationButtonClicked;
        }
    }

    private void ToolbarFrame_Navigated(object sender, Microsoft.UI.Xaml.Navigation.NavigationEventArgs e)
    {
        if (sender is Frame { Content: TaskerToolbar taskerToolbar })
        {
            taskerToolbar.RecorderButtonClicked += TaskerToolbar_RecorderButtonClicked;
            taskerToolbar.ConfigurationButtonClicked += TaskerToolbar_ConfigurationButtonClicked;
        }
    }

    private void TaskerToolbar_RecorderButtonClicked(object? sender, System.EventArgs e)
    {
        this.contentFrame.Navigate(typeof(SingleAView), null);
    }

    private void TaskerToolbar_ConfigurationButtonClicked(object? sender, EventArgs e)
    {
        // "SingleBView" is a page just for this answer.
        this.contentFrame.Navigate(typeof(SingleBView), null);
    }
}

相关问题