英文:
How to make event or callback in WinUI3?
问题
MainPage.xaml包含两个框架:Toolbar和Content。还有一些不同的页面用于这些框架。在Toolbar框架中使用了CommandBar。如何通过AppBarButton的点击事件从MainPage.xaml中的Frame(例如TaskerToolbar)更改Frame?或者如何从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>
英文:
I have main XAML page including two frames: Toolbar and Content. Also I have a few different pages for this frames. I'm using CommandBar into Toolbar frame. How to change Frame by AppBarButton click event from Frame (for ex. TaskerToolbar) in Main page? Or how to send click event from TaskerToolbar to MainPage.xaml. Thanks for help.
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 (and another toolbars):
<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>
答案1
得分: 0
以下是您要求的代码部分的中文翻译:
TaskerToobar.xaml
您可以将事件添加到您的 ``Page`` 类中:
<AppBarButton
Click="RecorderButton_Click"
Label="录制器" />
<AppBarButton
Click="ConfigurationButton_Click"
Label="配置" />
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" 是仅用于此回答的页面。
this.contentFrame.Navigate(typeof(SingleBView), null);
}
}
英文:
You can add events to your Page
class:
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);
}
}
and subscribe/unsubscribe like this:
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);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论