XAML 如何将选取器作为MenuItem包含在.NET MAUI中?

lndjwyie  于 2022-12-07  发布在  .NET
关注(0)|答案(1)|浏览(180)

是否可以将下拉选取器添加到MAUI Flyout中?我需要使全局选择对所有页面都可用。我知道我可以创建一个设置页面,但用户可以在菜单中更容易地进行此操作。
我已将Picker示例代码插入到MenuItem中,但收到以下错误:
XLS0502类型'MenuItem'不支持直接内容。
是否有其他方法可以做到这一点,或者是跨平台兼容性在某种程度上限制了我们的能力?

<?xml version="1.0" encoding="UTF-8" ?>
<Shell
    x:Class="SAE.AppShell"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:views="clr-namespace:SAE.MVVM.Views"
    Shell.FlyoutBehavior="Locked"
    Shell.FlyoutBackground="DarkGray">

    <FlyoutItem Title="Items" Icon="building_regular.png">
        <Tab>
            <ShellContent ContentTemplate="{DataTemplate views:Items}" />
        </Tab>
    </FlyoutItem>

    <MenuItem Text="Color">
            <Picker Title="Color"
                VerticalOptions="CenterAndExpand"
                Margin="10, 0"
                SelectedIndexChanged="OnPickerSelectedIndexChanged">
                <Picker.Items>
                    <x:String>Aqua</x:String>
                    <x:String>Black</x:String>
                    <x:String>Blue</x:String>
                    <x:String>Fuchsia</x:String>
                    <x:String>Gray</x:String>
                    <x:String>Green</x:String>
                    <x:String>Lime</x:String>
                    <x:String>Maroon</x:String>
                    <x:String>Navy</x:String>
                    <x:String>Olive</x:String>
                    <x:String>Purple</x:String>
                    <x:String>Red</x:String>
                    <x:String>Silver</x:String>
                    <x:String>Teal</x:String>
                    <x:String>White</x:String>
                    <x:String>Yellow</x:String>
                </Picker.Items>
            </Picker>
    </MenuItem>

</Shell>
4bbkushb

4bbkushb1#

您可以创建一个ColorPage来设置颜色,并创建一个全局变量来存储所选颜色,然后您可以从应用中的其他页面访问该全局变量。
可以参考下面的代码:
1.在AppShell.xaml中定义ColorPage
2.ColorPage.xaml.cs

public partial class ColorPage : ContentPage 
{
    //create a global variable here

    public static string CHOOSEDCOLOR = "";
      public ColorPage()
      {
            InitializeComponent();
      }

      private void Picker_SelectedIndexChanged(object sender, EventArgs e)
      {
        var picker = (Picker)sender;
        int selectedIndex = picker.SelectedIndex;

        if (selectedIndex != -1)
        {
            CHOOSEDCOLOR = (string)picker.ItemsSource[selectedIndex];
            mColorLabel.Text = CHOOSEDCOLOR;
        }
    }
}

3.代码ColorPage.xaml

<?xml version="1.0" encoding="utf-8" ?> 
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MauiApp925.ColorPage"
             Title="ColorPage">
    <VerticalStackLayout>
        <Label 
            Text=""
            x:Name="mColorLabel"
            VerticalOptions="Start" 
            HorizontalOptions="Center" />

        <Picker Title="Please choose color"
                VerticalOptions="CenterAndExpand"
                Margin="20"
                SelectedIndexChanged="Picker_SelectedIndexChanged">

            <Picker.ItemsSource>
                <x:Array Type="{x:Type x:String}">
                    <x:String>Aqua</x:String>
                    <x:String>Black</x:String>
                    <x:String>Blue</x:String>
                    <x:String>Fuchsia</x:String>
                    <x:String>Gray</x:String>
                    <x:String>Green</x:String>
                    <x:String>Lime</x:String>
                    <x:String>Maroon</x:String>
                    <x:String>Navy</x:String>
                    <x:String>Olive</x:String>
                    <x:String>Purple</x:String>
                    <x:String>Red</x:String>
                    <x:String>Silver</x:String>
                    <x:String>Teal</x:String>
                    <x:String>White</x:String>
                    <x:String>Yellow</x:String>
                </x:Array>
            </Picker.ItemsSource>
           
        </Picker>
    </VerticalStackLayout>
</ContentPage>

4.然后,您可以在其他页面中存取全局变量CHOOSEDCOLOR,如下所示:

string color = ColorPage.CHOOSEDCOLOR;

相关问题