winui3问题:点击事件不生效

2021-11-23

winui xaml zinotes

<pre class="EnlighterJSRAW" data-enlighter-group="" data-enlighter-highlight="" data-enlighter-language="csharp" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-theme="" data-enlighter-title=""><Window
    x:Class="App1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App1"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">
    <SplitView IsPaneOpen="True" x:Name="splitView" PaneBackground="{ThemeResource SystemControlBackgroundChromeMediumLowBrush}"
               OpenPaneLength="256" CompactPaneLength="48" DisplayMode="CompactInline">
        <SplitView.Pane>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="56" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>

                <StackPanel Orientation="Vertical" Grid.Column="0" Margin="0,20,0,0">
                    <AppBarButton Icon="GlobalNavigationButton" Click="OnOpenPane" Margin="-10,0,0,0"/>
                    <AppBarButton Icon="Add" Label="Add" Click="OnOpenPane" Margin="-10,0,0,0"/>
                    <AppBarButton Icon="Edit" Label="Add" Click="OnOpenPane" Margin="-10,0,0,0"/>
                    <AppBarButton Icon="Setting" Label="Add" Click="OnOpenPane" Margin="-10,0,0,0"/>
                </StackPanel>

                <TreeView x:Name="treeView1" 
                    Grid.Column="1"
                    SelectionMode="Single">
                </TreeView>
            </Grid>
        </SplitView.Pane>
        Grid>
            <TabView x:Name="Tabs" />
        </Grid>
    </SplitView>
</Window>

大概这样的XAML,整体是一个SplitView,左侧的Pane分两部分,左侧是一列按钮,右侧是一个TreeView,SplitView的右侧是一个TabView。不得不说,winui的新UI确实漂亮,而且XAML设计界面也很快捷,就是winui好像还不支持可视化设计,只能手写XAML,权当学习了吧。

![](https://www.zicode.com/wp-content/uploads/2021/11/image-1024x575.png)
基本上没费多大功夫就设计出这样的界面,问题是,左侧的那些按钮点击了没有反应。调试工具选择界面元素查看了一下,发现StackPanel其实是占用的右侧部分,而不是期望的左侧的56像素的部分,猜测Grid的列的位置搞错了(代码和截图都是修改后的)。将StackPanel的Grid.Column改成0,问题解决了。
 
阅读