繁体   English   中英

Xamarin Forms 单击列表视图上的菜单时如何打开弹出窗口

[英]Xamarin Forms how to open a popup when you clicked a menu on the listview

我有一个 XAML 页面,其中包含菜单的 ListView,我正在尝试将注销链接为按钮以显示 RadPopup。 这个 Popup 没有 MVVM
我是菜鸟所以不要怪我那么难...

这是XAML上的ListView

        <ListView x:Name="listview" x:FieldModifier="public" 
                  BackgroundColor="{StaticResource BlueDark}"
                  SeparatorVisibility="Default" 
                  SeparatorColor="White" 
                  SelectionMode="Single"
                  HasUnevenRows="False"
                  ios:ListView.SeparatorStyle="FullWidth">

            <ListView.ItemsSource>
                <x:Array Type="{x:Type local:MenuItem}">

                    <local:MenuItem  Title="{x:Static lang:AppResources.MenuHome}"           TargetPage="{x:Type local:AlarmsListPage}"/>
                    <local:MenuItem  Title="{x:Static lang:AppResources.MenuSitesAndAssets}" TargetPage="{x:Type local:AlarmsListPage}"/>
                    <local:MenuItem  Title="{x:Static lang:AppResources.MenuUserProfile}"    TargetPage="{x:Type local:AlarmsListPage}"/>
                    <local:MenuItem  Title="{x:Static lang:AppResources.MenuFeedback}"       TargetPage="{x:Type local:AlarmsListPage}"/>
                    <local:MenuItem  Title="{x:Static lang:AppResources.MenuLogout}"        TargetPage="{x:Type local:AlarmsListPage} "/>
                </x:Array>
            </ListView.ItemsSource>
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="33"/>
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>
                            <Label Grid.Column="0" Grid.ColumnSpan="3"
                                   Text="{Binding Title}" TextColor="white" Padding="15,0,0,0"
                                   VerticalTextAlignment="Center"/>
                        </Grid>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
        

我为 PopupView XAML 创建了一个 ContenPage

    <ContentPage.Content>
        <Button Clicked="LogoutPopup">
            <telerikPrimitives:RadPopup.Popup>
                <telerikPrimitives:RadPopup x:Name="Popup" Placement="Center" OutsideBackgroundColor="WhiteSmoke"  IsModal="False">
                    <telerikPrimitives:RadBorder CornerRadius="10" BackgroundColor="{StaticResource BlueDark}">
                        <Grid Padding="25">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="*"/>
                                <RowDefinition Height="*"/>
                            </Grid.RowDefinitions>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="100"/>
                                <ColumnDefinition Width="100"/>
                            </Grid.ColumnDefinitions>
                            <StackLayout Margin="12" Padding="24" Spacing="24" BackgroundColor="{StaticResource BlueDark}" HorizontalOptions="Center" VerticalOptions="Center">
                                <StackLayout>
                                    <Label Grid.Column="0"  Grid.Row="0" Grid.ColumnSpan="2" TextColor="{StaticResource White}" HorizontalTextAlignment="Center" 
                                     Text="Are you sure you want to log out? You will no longer receive alarm notifications." />
                                </StackLayout>
                                <Button Grid.Column="0" Grid.Row="1" BackgroundColor="Transparent" TextColor="{StaticResource White}" Text="Yes"/>
                                <Button Grid.Column="1" Grid.Row="1" BackgroundColor="Transparent" TextColor="{StaticResource White}" Text="No"/>
                            </StackLayout>
                        </Grid>
                    </telerikPrimitives:RadBorder>
                </telerikPrimitives:RadPopup>
            </telerikPrimitives:RadPopup.Popup>
        </Button>
    </ContentPage.Content>
</ContentPage>

我正在尝试将 PopupView 绑定到注销 ListView。 有人可以帮我弄这个吗?

从我的角度来看,没有必要使用 Button。 您可以使用列表视图的 ItemSelected 事件来实现它。

在 xaml 中,只需添加 listview_ItemTapped 事件处理程序:

<StackLayout>
<ListView x:Name="listview" x:FieldModifier="public" 
              ItemTapped="listview_ItemTapped"  // add this line
              BackgroundColor="AliceBlue"
              SeparatorVisibility="Default" 
              SeparatorColor="White" 
              SelectionMode="Single"
              HasUnevenRows="False"
              >
...
...

</ListView>
// you could just put your popup after listview
<telerikPrimitives:RadPopup.Popup>
    <telerikPrimitives:RadPopup x:Name="Popup" ...


...

</telerikPrimitives:RadPopup.Popup>
</StackLayout>

在.cs文件中实现它:

void listview_ItemTapped(System.Object sender, Xamarin.Forms.ItemTappedEventArgs e)
    {
        // popup page here
        Popup.IsOpen = true;
    }

希望对你有效。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM