C# Xamarin表单:如何(在应用程序中)从CRUD函数动态添加菜单项?

C# Xamarin表单:如何(在应用程序中)从CRUD函数动态添加菜单项?,c#,xamarin.forms,xamarin.android,crud,C#,Xamarin.forms,Xamarin.android,Crud,我有一个CRUD功能,工作非常完美,我需要做的是启用CRUD功能添加菜单项 使用CRUD创建的新项目应显示在此处: 我试图找到以下问题的解决方案:,以及其他许多问题。。但这些文章并没有回答我的问题。 [XamlCompilation(XamlCompilationOptions.Compile)] public partial class SettingsPage : ContentPage { public Settings Settings { get; set;

我有一个CRUD功能,工作非常完美,我需要做的是启用CRUD功能添加菜单项

使用CRUD创建的新项目应显示在此处:

我试图找到以下问题的解决方案:,以及其他许多问题。。但这些文章并没有回答我的问题。

[XamlCompilation(XamlCompilationOptions.Compile)]
  public partial class SettingsPage : ContentPage
     {
      public Settings Settings { get; set; }
         DbService  services;
         public SettingsPage ()
  {
  InitializeComponent();
             // Creates the Settings Model
      Settings = new Settings();
             // Sets the Settings Model as the BindingContext for "filling" the SettingsPage with the current AppSettings.
      BindingContext = Settings;
             services = new DbService();
    
         }
    
         protected async override void OnAppearing()
         {
             showUrl(); // Returns GetAllUrls
             base.OnAppearing();
         }
    
         private void showUrl()
         {
             var res = services.GetAllUrls();
             lstUrls.ItemsSource = res; // Binding with listview
         }
    
    
         //This method is used to insert data into db 
         private async void BtnAddRecord_Clicked(object sender, EventArgs e)
         {
             await this.Navigation.PushModalAsync(new AddUrl(), true);
    
         }
    
          
    
         // Display alert for update and delete
         private async void lstData_ItemSelected(object sender, SelectedItemChangedEventArgs e)
         {
             if (e.SelectedItem != null)
             {
                 Urls obj = (Urls) e.SelectedItem;
                 string res = await DisplayActionSheet("Operation", "Cancel", null, "Update", "Delete");
    
                 switch (res)
                 {
                     case "Update":
                         await this.Navigation.PushModalAsync(new AddUrl(obj), true);
                         break;
                     case "Delete":
                         services.DeleteUrl(obj);
                         showUrl();
                         break;
                            
                 }
                  
                 lstUrls.SelectedItem = null;
             }
    
         }
     }
如前所述,我理解您的逻辑,但我无法将其应用到我的项目中。。我在这方面是新手,所以我将分享一些代码,希望有人能帮助我

这是我的AppShell.xaml

    <?xml version="1.0" encoding="UTF-8"?>
 <Shell xmlns="http://xamarin.com/schemas/2014/forms" 
        xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
        xmlns:local="clr-namespace:MyTestApplication.Views"
        xmlns:viewModels="clr-namespace:MyTestApplication.ViewModels;assembly=MyTestApplication"
        Title="MyTestApplication"
        x:Class="MyTestApplication.AppShell">
        
     <Shell.Resources>
         <ResourceDictionary>
             <Style x:Key="BaseStyle" TargetType="Element">
                 <Setter Property="Shell.BackgroundColor" Value="{StaticResource Primary}" />
                 <Setter Property="Shell.ForegroundColor" Value="White" />
                 <Setter Property="Shell.TitleColor" Value="White" />
                 <Setter Property="Shell.DisabledColor" Value="#B4FFFFFF" />
                 <Setter Property="Shell.UnselectedColor" Value="#95FFFFFF" />
                 <Setter Property="Shell.TabBarBackgroundColor" Value="{StaticResource Primary}" />
                 <Setter Property="Shell.TabBarForegroundColor" Value="White"/>
                 <Setter Property="Shell.TabBarUnselectedColor" Value="#95FFFFFF"/>
                 <Setter Property="Shell.TabBarTitleColor" Value="White"/>
             </Style>
             <Style TargetType="TabBar" BasedOn="{StaticResource BaseStyle}" />
             <Style TargetType="FlyoutItem" BasedOn="{StaticResource BaseStyle}" />
    
             <Style Class="MenuItemLayoutStyle" TargetType="Layout" ApplyToDerivedTypes="True">
                 <Setter Property="VisualStateManager.VisualStateGroups">
                     <VisualStateGroupList>
                         <VisualStateGroup x:Name="CommonStates">
                             <VisualState x:Name="Normal">
                                 <VisualState.Setters>
                                     <Setter TargetName="FlyoutItemLabel" Property="Label.TextColor" Value="{StaticResource Primary}" />
                                 </VisualState.Setters>
                             </VisualState>
                         </VisualStateGroup>
                     </VisualStateGroupList>
                 </Setter>
             </Style>
         </ResourceDictionary>
     </Shell.Resources>
      
    
     <FlyoutItem Title="Home" Icon="home.png">
             <ShellContent Route="MainPage" ContentTemplate="{DataTemplate local:MainPage}" />
         </FlyoutItem>
         <FlyoutItem Title="Settings" Icon="setting.png">
             <ShellContent Route="SettingsPage" ContentTemplate="{DataTemplate local:SettingsPage}" />
         </FlyoutItem>
    
     <MenuItem Icon="logout.png" Text="Logout" StyleClass="MenuItemLayoutStyle" Clicked="OnMenuItemClicked">
     </MenuItem>
    
     <TabBar >
         <ShellContent Route="LoginPage" ContentTemplate="{DataTemplate local:LoginPage}" />
     </TabBar>
    
 </Shell>
  <ContentPage.Content>
         <StackLayout Margin="10" VerticalOptions="StartAndExpand" HorizontalOptions="FillAndExpand">
             <Entry x:Name="TxtUrlName" Placeholder="Enter Url Name" />
             <Entry x:Name="TxtUrl" Placeholder="Enter Url" />
             <Button x:Name="btnSaveUpdate" Text="Save" Clicked="btnSaveUpdate_Clicked" />
         </StackLayout>
     </ContentPage.Content>
这是AddUrl.xaml

    <?xml version="1.0" encoding="UTF-8"?>
 <Shell xmlns="http://xamarin.com/schemas/2014/forms" 
        xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
        xmlns:local="clr-namespace:MyTestApplication.Views"
        xmlns:viewModels="clr-namespace:MyTestApplication.ViewModels;assembly=MyTestApplication"
        Title="MyTestApplication"
        x:Class="MyTestApplication.AppShell">
        
     <Shell.Resources>
         <ResourceDictionary>
             <Style x:Key="BaseStyle" TargetType="Element">
                 <Setter Property="Shell.BackgroundColor" Value="{StaticResource Primary}" />
                 <Setter Property="Shell.ForegroundColor" Value="White" />
                 <Setter Property="Shell.TitleColor" Value="White" />
                 <Setter Property="Shell.DisabledColor" Value="#B4FFFFFF" />
                 <Setter Property="Shell.UnselectedColor" Value="#95FFFFFF" />
                 <Setter Property="Shell.TabBarBackgroundColor" Value="{StaticResource Primary}" />
                 <Setter Property="Shell.TabBarForegroundColor" Value="White"/>
                 <Setter Property="Shell.TabBarUnselectedColor" Value="#95FFFFFF"/>
                 <Setter Property="Shell.TabBarTitleColor" Value="White"/>
             </Style>
             <Style TargetType="TabBar" BasedOn="{StaticResource BaseStyle}" />
             <Style TargetType="FlyoutItem" BasedOn="{StaticResource BaseStyle}" />
    
             <Style Class="MenuItemLayoutStyle" TargetType="Layout" ApplyToDerivedTypes="True">
                 <Setter Property="VisualStateManager.VisualStateGroups">
                     <VisualStateGroupList>
                         <VisualStateGroup x:Name="CommonStates">
                             <VisualState x:Name="Normal">
                                 <VisualState.Setters>
                                     <Setter TargetName="FlyoutItemLabel" Property="Label.TextColor" Value="{StaticResource Primary}" />
                                 </VisualState.Setters>
                             </VisualState>
                         </VisualStateGroup>
                     </VisualStateGroupList>
                 </Setter>
             </Style>
         </ResourceDictionary>
     </Shell.Resources>
      
    
     <FlyoutItem Title="Home" Icon="home.png">
             <ShellContent Route="MainPage" ContentTemplate="{DataTemplate local:MainPage}" />
         </FlyoutItem>
         <FlyoutItem Title="Settings" Icon="setting.png">
             <ShellContent Route="SettingsPage" ContentTemplate="{DataTemplate local:SettingsPage}" />
         </FlyoutItem>
    
     <MenuItem Icon="logout.png" Text="Logout" StyleClass="MenuItemLayoutStyle" Clicked="OnMenuItemClicked">
     </MenuItem>
    
     <TabBar >
         <ShellContent Route="LoginPage" ContentTemplate="{DataTemplate local:LoginPage}" />
     </TabBar>
    
 </Shell>
  <ContentPage.Content>
         <StackLayout Margin="10" VerticalOptions="StartAndExpand" HorizontalOptions="FillAndExpand">
             <Entry x:Name="TxtUrlName" Placeholder="Enter Url Name" />
             <Entry x:Name="TxtUrl" Placeholder="Enter Url" />
             <Button x:Name="btnSaveUpdate" Text="Save" Clicked="btnSaveUpdate_Clicked" />
         </StackLayout>
     </ContentPage.Content>
再说一遍:我不知道如何在我的代码中实现逻辑。。我有一个CRUD函数,它可以完美地工作,但我需要使用它来动态地在菜单中添加项目。希望有人能给我指路


谢谢

关于CRUD操作,您可以在操作数据库之前调用
Items.Add()
方法和
Items.Remove()
方法

与添加AddUrl.xaml.cs中的菜单项类似:

private async void btnSaveUpdate_Clicked(object sender, EventArgs e)
     {
         Urls obj = new Urls(); // Use of url object

         //Text box value
         obj.Name = TxtUrlName.Text;
         obj.Url = TxtUrl.Text;

         // If _isUpdate is true, call UpdateUrls method
         if (_isUpdate)
         {
             obj.Id = urlId;
             await _services.UpdateUrls(obj);
         }
         // Else insert record
         else
         {
             _services.InsertUrls(obj);
         }
         AppShell.Current.Items.Add(your menuitem);//add the menuitems
         await this.Navigation.PopModalAsync();
     }
删除设置spage.cs中的菜单项:

private async void lstData_ItemSelected(object sender, SelectedItemChangedEventArgs e)
     {
         if (e.SelectedItem != null)
         {
             Urls obj = (Urls) e.SelectedItem;
             string res = await DisplayActionSheet("Operation", "Cancel", null, "Update", "Delete");

             switch (res)
             {
                 case "Update":
                     await this.Navigation.PushModalAsync(new AddUrl(obj), true);
                     break;
                 case "Delete":
                     services.DeleteUrl(obj);
                     AppShell.Current.Items.Remove(your menuitem); //remove
                     showUrl();
                     break;
                        
             }
              
             lstUrls.SelectedItem = null;
         }

     }

“AppShell.Current.Items.Remove(你的菜单项);”,你说的“你的菜单项”是什么意思?我找不到菜单项@Leo Zhu-MSFT@Lidprogsky这是您要在菜单中显示的新菜单项。