.net Maui SQL搜索页调用网格页

cbeh67ev  于 2023-03-04  发布在  .NET
关注(0)|答案(2)|浏览(144)

我是毛伊岛的新手,最初是从WinForms环境中来的,所以非常注重事件,在毛伊岛有很多大问题,特别是DI和MVVM。
所以我试着让一个页面接受一堆参数,然后用一个网格显示另一个页面,网格显示从API返回的所有记录。我已经编写并发布了API,我已经让每个页面单独工作得很好,我只是不能让它们一起工作。
问题是我无法将参数传递给网格页面。网格页面有一个BindingContext,它可以获取数据来填充网格,但我找不到任何可以访问调用页面参数的地方。在下面的代码中,我从button_click传递对象中的paramter类。我知道这在MVVM中是不好的,但我很熟悉,我会在工作后整理它。我已经得到了一个MVVM版本,按钮绑定到MV中的一个方法,但恢复到这种方式来尝试并首先解决这个问题。
我已经创建了一个参数类,并使用调用屏幕中的字段填充了它,但在执行API调用之前似乎无法访问它。

public partial class AnimalSearchNew : ContentPage
    {
        public AnimalSearchNew()
        {
            InitializeComponent();
        }
        async void OnSubmitClicked(Object sender, EventArgs e)
        {
            AnimalParam animalParm = new AnimalParam();
            animalParm.AnimalName = "fred";
            await Navigation.PushModalAsync(new grdAnimal());
        }
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:dxg="clr-namespace:DevExpress.Maui.DataGrid;assembly=DevExpress.Maui.DataGrid"
             xmlns:dxe="clr-namespace:DevExpress.Maui.Editors;assembly=DevExpress.Maui.Editors"
             xmlns:dxc="clr-namespace:DevExpress.Maui.Controls;assembly=DevExpress.Maui.Controls"
             xmlns:ios="clr-namespace:Microsoft.Maui.Controls.PlatformConfiguration.iOSSpecific;assembly=Microsoft.Maui.Controls"
             ios:Page.UseSafeArea="True"
             xmlns:local="clr-namespace:MauiAppPagesTest.AnimalViewModel"
             x:Class="MauiAppPagesTest.grdAnimal">
    <ContentPage.ToolbarItems>
        <ToolbarItem Text="Grid" Order="Secondary" Priority="0" IconImageSource="dog_64_w.png" Clicked="GridClicked" />
        <!--IconImageSource=""-->
        <ToolbarItem Text="Grid Options" Order="Secondary" Priority="0"  IconImageSource="findperson_50_w.png" Clicked="GridOptionsClicked"/>
        <ToolbarItem Text="Map" Order="Secondary" Priority="0"  IconImageSource="mapicon.png" Clicked="MapClicked"/>
    </ContentPage.ToolbarItems>
    
    <ContentPage.BindingContext >
        <local:AnimalDataViewModel />
    </ContentPage.BindingContext>
    
    <dxg:DataGridView ItemsSource="{Binding Animals}"
                  EditorShowMode="DoubleTap"
                  AllowDragDropSortedRows ="True">
        <dxg:DataGridView.Columns>
            <dxg:TemplateColumn FieldName="Owner" Caption="Person" MinWidth="200"/>
            <dxg:TextColumn FieldName="Phone" MinWidth="150" VerticalContentAlignment="Center" />
            <dxg:TextColumn FieldName="Address" MinWidth="300" VerticalContentAlignment="Center" />
            <dxg:TextColumn FieldName="Tag" Caption="Tag" MinWidth="100" VerticalContentAlignment="Center"/>
            <dxg:TextColumn FieldName="Tag_Expiry" Caption="Tag Expiry" MinWidth="150" DisplayFormat="d" VerticalContentAlignment="Center"/>
            <dxg:TextColumn FieldName="Microchip_Number" Caption="Microchip" MinWidth="120" VerticalContentAlignment="Center"/>
            <dxg:TextColumn FieldName="Name"  MinWidth="150" VerticalContentAlignment="Center"/>
            <dxg:TextColumn FieldName="Breed"  MinWidth="150" VerticalContentAlignment="Center"/>
            <dxg:TextColumn FieldName="Colour"  MinWidth="150" VerticalContentAlignment="Center"/>
            <dxg:TextColumn FieldName="Gender"  MinWidth="100" VerticalContentAlignment="Center"/>
            <dxg:CheckBoxColumn FieldName="Neutered" Caption="Neutered" MinWidth="100" VerticalContentAlignment="Center"/>
            <dxg:CheckBoxColumn FieldName="Dangerous" Caption="Dangerous" MinWidth="110" VerticalContentAlignment="Center"/>
            <dxg:TextColumn FieldName="BirthDate" Caption="BirthDate" MinWidth="150" DisplayFormat="d" VerticalContentAlignment="Center"/>
            <dxg:TextColumn FieldName="DeathDate" Caption="DeathDate" MinWidth="150" DisplayFormat="d" VerticalContentAlignment="Center"/>
            <dxg:TextColumn FieldName="DepartedDate" Caption="DepartedDate" MinWidth="150" DisplayFormat="d" VerticalContentAlignment="Center"/>
            <dxg:CheckBoxColumn FieldName="CRM" Caption="CRM" MinWidth="100"  VerticalContentAlignment="Center"/>
            <dxg:CheckBoxColumn FieldName="Memo" Caption="Memo" MinWidth="100"  VerticalContentAlignment="Center"/>
        </dxg:DataGridView.Columns>
    </dxg:DataGridView>
</ContentPage>
public class AnimalData
    {
        public ObservableCollection<Animal> Animals { get; private set; }
        public AnimalData()
        {
            GenerateAnimal();
        }
        void GenerateAnimal()
        {
            string Url = "https://{ValidAPI which returns data removed for security}/";
            IRequestHandler restSharpRequestHandler = new RestSharpRequestHandler();
            List<Animal> _animals = restSharpRequestHandler.GetAnimalByName(Url + "FindAnimalByName", "kito"); // AnimalParam.AnimalName);
            Animals = new ObservableCollection<Animal>(_animals);
        }
    }

我已经搜索了很多,以找到正确的方法来做到这一点,但不能找到一个例子,有相同的页面结构排序给我。
我只是希望能够告诉一个被调用的页面从DB中读取什么。

agyaoht7

agyaoht71#

你想在触发OnSubmitClicked事件后,从AnimalSearchNew传递参数到grdAnimal页,对吗?然后你可以尝试在grdAnimal页的构造函数中传递参数。
请参考以下代码:

private async void OnSubmitClicked(object sender, EventArgs e) 
{
    AnimalParam animalParm = new AnimalParam();
    animalParm.AnimalName = "fred";
    await Navigation.PushModalAsync(new grdAnimal(animalParm));
}

在第grdAnimal.xaml.cs页中,您可以获得如下参数:

public partial class grdAnimal : ContentPage 
{

      public  AnimalParam myParam { get; set; }

    public grdAnimal(AnimalParam param)
      {
            InitializeComponent();

            this.myParam = param;
      }
}
nx7onnlm

nx7onnlm2#

我最终从xaml中取出了ContentPage.BindingContext,并在initialiseComponent之后添加了参数。这似乎是一种迂回的方法,但似乎有效。现在要使其成为适当的MVVM:-(

<!--<ContentPage.BindingContext>
    <local:AnimalDataViewModel/>
</ContentPage.BindingContext>-->

        public grdAnimal(AnimalSearch _animalSearch)
    {
        animalSearch = _animalSearch;
        InitializeComponent();
        this.BindingContext = new AnimalDataViewModel(animalSearch);
    }

相关问题