XAML 在何处执行内容视图业务逻辑操作

h79rfbju  于 2023-09-28  发布在  其他
关注(0)|答案(2)|浏览(78)

我正在使用一个.net maui应用程序,我需要在自定义控件中获取和设置数据。
目标是:
Operation Template
页面:

<ContentPage
    x:Class="V7.UIS.MauiMobile.Views.AboutPage"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:controls="clr-namespace:V7.UIS.MauiMobile.Controls.Views"    
    xmlns:viewmodel="clr-namespace:V7.UIS.MauiMobile.ViewModels"
    Title="About"
    ios:Page.UseSafeArea="true"
    x:DataType="viewmodel:AboutViewModel"
    BackgroundColor="{StaticResource NormalBackgroundColor}">

    <VerticalStackLayout>

        <controls:ItemBarcodeSearchView />

        <Entry Text="Count" />
        <Button
            Grid.Row="1"
            Grid.Column="1"
            Command="{Binding AddCommand}"
            Text="Add" />
    </VerticalStackLayout>
</ContentPage>

自定义控件xaml:

<?xml version="1.0" encoding="utf-8" ?>
<ContentView
    x:Class="V7.UIS.MauiMobile.Controls.Views.ItemBarcodeSearchView"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:dxe="clr-namespace:DevExpress.Maui.Editors;assembly=DevExpress.Maui.Editors"
    x:Name="BarcodeSearchView">
    <ContentView.ControlTemplate>
        <ControlTemplate>
            <VerticalStackLayout>
                <Grid
                    x:Name="ButtonGrid"
                    Margin="5"
                    IsVisible="{TemplateBinding IsVisibleButtonGrid}">
                    <Button Clicked="ShowView" Text="Enter Barcode" />
                </Grid>
                <Grid
                    x:Name="DetailGrid"
                    Padding="15"
                    ColumnDefinitions="*"
                    ColumnSpacing="20"
                    IsVisible="{TemplateBinding IsVisibleDetailGrid}"
                    RowDefinitions="Auto,Auto"
                    VerticalOptions="Center">
                    <dxe:TextEdit
                        Grid.Row="0"
                        Margin="0"
                        ClearIconCommand="{TemplateBinding ClearFieldsCommand}"
                        ClearIconVisibility="Always"
                        LabelText="Barcode"
                        Text="{TemplateBinding Barcode}" />
                    <dxe:TextEdit
                        Grid.Row="1"
                        Margin="0"
                        ClearIconVisibility="Never"
                        LabelText="Item"
                        Text="{TemplateBinding ItemDescription}" />                    
                    <Button
                        Grid.Row="6"
                        Margin="0,10,0,0"
                        Command="{Binding Source={x:Reference BarcodeSearchView}, Path=Search}"
                        Text="Search" />
                    <Button
                        Grid.Row="7"
                        Margin="0,10,0,0"
                        Clicked="SendBarcodeDetails"
                        Text="Ok" />
                </Grid>
            </VerticalStackLayout>
        </ControlTemplate>
    </ContentView.ControlTemplate>
</ContentView>

当我按下搜索按钮时,我想执行此命令以设置itemdescription。这是内容视图的业务逻辑,我不想一遍又一遍地重复这段代码。

private readonly HttpClient _client;

public blabla(IHttpClientFactory factory)
{
_client = factory?.CreateClient(AppConstants.ApiName);
}

GetBarcodeDetailQuery query = new()
{
    Barcode = barcode
};
await client.GetDataFromApi<GetBarcodeDetailQuery, GetBarcodeDetailViewModel>("Cards/ItemBarcode/GetBarcodeDetail", query, FindBarcodeCallBack);

我应该把这个操作放在哪里,怎么做?我一个星期都搞不清楚。
自定义控件xaml.cs:

using CommunityToolkit.Mvvm.Input;
using V7.UIS.MauiMobile.Models;

namespace V7.UIS.MauiMobile.Controls.Views;

public partial class ItemBarcodeSearchView : ContentView
{
    public ItemBarcodeSearchView()
    {
        InitializeComponent();
        IsVisibleButtonGrid = "true";
        IsVisibleDetailGrid = "false";
    }

    public event EventHandler<ItemBarcodeDetail> BarcodeDetailsReady;

    public static readonly BindableProperty BarcodeProperty =
        BindableProperty.Create(nameof(Barcode), typeof(string), typeof(ItemBarcodeSearchView));
    public static readonly BindableProperty ItemDescriptionProperty =
        BindableProperty.Create(nameof(ItemDescription), typeof(string), typeof(ItemBarcodeSearchView));    
    public static readonly BindableProperty IsVisibleButtonGridProperty =
        BindableProperty.Create(nameof(IsVisibleButtonGrid), typeof(string), typeof(ItemBarcodeSearchView));
    public static readonly BindableProperty IsVisibleDetailGridProperty =
        BindableProperty.Create(nameof(IsVisibleDetailGrid), typeof(string), typeof(ItemBarcodeSearchView));
    public static readonly BindableProperty SearchProperty =
            BindableProperty.Create(nameof(Search), typeof(RelayCommand), typeof(ItemBarcodeSearchView));

    public RelayCommand Search
    {
        get => (RelayCommand)GetValue(SearchProperty);
        set => SetValue(SearchProperty, value);
    }
    public string IsVisibleButtonGrid
    {
        get => (string)GetValue(IsVisibleButtonGridProperty);
        set => SetValue(IsVisibleButtonGridProperty, value);
    }
    public string IsVisibleDetailGrid
    {
        get => (string)GetValue(IsVisibleDetailGridProperty);
        set => SetValue(IsVisibleDetailGridProperty, value);
    }
    public string Barcode
    {
        get => (string)GetValue(BarcodeProperty);
        set => SetValue(BarcodeProperty, value);
    }
    public string ItemDescription
    {
        get => (string)GetValue(ItemDescriptionProperty);
        set => SetValue(ItemDescriptionProperty, value);
    }
    
    [RelayCommand]
    public void ClearFields()
    {
        Barcode = string.Empty;
        ItemDescription = string.Empty;        
    }

    private void SendBarcodeDetails(object sender, EventArgs e)
    {
        ItemBarcodeDetail itemBarcodeDetail = new()
        {
            Barcode = Barcode,
            ItemDescription = ItemDescription            
        };
        BarcodeDetailsReady?.Invoke(sender, itemBarcodeDetail);
        IsVisibleButtonGrid = "true";
        IsVisibleDetailGrid = "false";
    }
    private void ShowView(object sender, EventArgs e)
    {
        IsVisibleButtonGrid = "false";
        IsVisibleDetailGrid = "true";
    }
}

希望这里有人能帮助我。

xwmevbvl

xwmevbvl1#

ContentView类定义了一个View类型的Content属性,它表示ContentView的内容。此属性由BindableProperty对象支持,这意味着它可以成为数据绑定的目标,并具有样式。
ContentView类本身提供的功能很少,但可用于创建自定义控件。创建自定义控件的过程是:

  • 创建从ContentView类派生的类。
  • 在自定义控件的代码隐藏文件中定义任何控件属性或事件。
  • 定义自定义控件的UI。

有关更多信息,您可以查看官方文档:创建自定义控件。

注:

您也可以在这里查看示例ContentView Demos,它将帮助您了解如何使用ContentView创建自定义控件。虽然此示例是Xamarin表单的应用程序,但它也适用于MAUI。

dkqlctbz

dkqlctbz2#

我做了这样的事情,它的工作,但它是最好的做法,使用我不确定。
这里是页面:

<ContentPage    
    xmlns:viewmodel="clr-namespace:V7.UIS.MauiMobile.ViewModels"
    x:DataType="viewmodel:AboutViewModel"
    Title="About">
    <VerticalStackLayout>

        <controls:ItemBarcodeSearchView HttpClient="{Binding HttpClient}" />

        <Entry Text="Count" />
        <Button
            Grid.Row="1"
            Grid.Column="1"
            Text="Add"/>
    </VerticalStackLayout>
</ContentPage>
using V7.UIS.MauiMobile.Constants;

namespace V7.UIS.MauiMobile.ViewModels
{
    public class AboutViewModel : BaseViewModel
    {
        private readonly HttpClient _client;

        public const string ViewName = "AboutPage";

        public AboutViewModel(IHttpClientFactory factory)
        {
            Title = "About";
            _client = factory?.CreateClient(AppConstants.ApiName);
        }
        public HttpClient HttpClient => _client;
    }
}

下面是自定义控件:

using CommunityToolkit.Mvvm.Input;
using V7.BaseApplication.AppModels.Erp.Cards.Item.ItemBarcode.Queries;
using V7.BaseApplication.AppModels.Erp.Cards.Item.ItemBarcode.ViewModel;
using V7.UIS.MauiMobile.Helpers;
using V7.UIS.MauiMobile.Models;

namespace V7.UIS.MauiMobile.Controls.Views;

public partial class ItemBarcodeSearchView : ContentView
{
    public ItemBarcodeSearchView()
    {
        InitializeComponent();
        IsVisibleButtonGrid = "true";
        IsVisibleDetailGrid = "false";
    }

    public event EventHandler<ItemBarcodeDetail> BarcodeDetailsReady;

    #region PropertyDefinition 

    public static readonly BindableProperty BarcodeProperty =
        BindableProperty.Create(nameof(Barcode), typeof(string), typeof(ItemBarcodeSearchView));
    public static readonly BindableProperty ItemDescriptionProperty =
        BindableProperty.Create(nameof(ItemDescription), typeof(string), typeof(ItemBarcodeSearchView));
    public static readonly BindableProperty ItemUnitProperty =
        BindableProperty.Create(nameof(ItemUnit), typeof(string), typeof(ItemBarcodeSearchView));
    public static readonly BindableProperty ItemLotProperty =
        BindableProperty.Create(nameof(ItemLot), typeof(string), typeof(ItemBarcodeSearchView));
    public static readonly BindableProperty ItemSerialNumberProperty =
        BindableProperty.Create(nameof(ItemSerialNumber), typeof(string), typeof(ItemBarcodeSearchView));
    public static readonly BindableProperty QuantityProperty =
        BindableProperty.Create(nameof(Quantity), typeof(string), typeof(ItemBarcodeSearchView));
    public static readonly BindableProperty IsVisibleButtonGridProperty =
        BindableProperty.Create(nameof(IsVisibleButtonGrid), typeof(string), typeof(ItemBarcodeSearchView));
    public static readonly BindableProperty IsVisibleDetailGridProperty =
        BindableProperty.Create(nameof(IsVisibleDetailGrid), typeof(string), typeof(ItemBarcodeSearchView));
    public static readonly BindableProperty HttpClientProperty =
            BindableProperty.Create(nameof(HttpClient), typeof(HttpClient), typeof(ItemBarcodeSearchView));
    public HttpClient HttpClient
    {
        get => (HttpClient)GetValue(HttpClientProperty);
        set => SetValue(HttpClientProperty, value);
    }
    public string IsVisibleButtonGrid
    {
        get => (string)GetValue(IsVisibleButtonGridProperty);
        set => SetValue(IsVisibleButtonGridProperty, value);
    }
    public string IsVisibleDetailGrid
    {
        get => (string)GetValue(IsVisibleDetailGridProperty);
        set => SetValue(IsVisibleDetailGridProperty, value);
    }
    public string Barcode
    {
        get => (string)GetValue(BarcodeProperty);
        set => SetValue(BarcodeProperty, value);
    }
    public string ItemDescription
    {
        get => (string)GetValue(ItemDescriptionProperty);
        set => SetValue(ItemDescriptionProperty, value);
    }
    public string ItemUnit
    {
        get => (string)GetValue(ItemUnitProperty);
        set => SetValue(ItemUnitProperty, value);
    }
    public string ItemLot
    {
        get => (string)GetValue(ItemLotProperty);
        set => SetValue(ItemLotProperty, value);
    }
    public string ItemSerialNumber
    {
        get => (string)GetValue(ItemSerialNumberProperty);
        set => SetValue(ItemSerialNumberProperty, value);
    }
    public string Quantity
    {
        get => (string)GetValue(QuantityProperty);
        set => SetValue(QuantityProperty, value);
    }
    #endregion

    private void ShowView(object sender, EventArgs e)
    {
        IsVisibleButtonGrid = "false";
        IsVisibleDetailGrid = "true";
    }

    [RelayCommand]
    public void ClearFields()
    {
        Barcode = string.Empty;
        ItemDescription = string.Empty;
        ItemUnit = string.Empty;
        ItemLot = string.Empty;
        ItemSerialNumber = string.Empty;
        Quantity = string.Empty;
    }
    [RelayCommand]
    public async Task Search()
    {
        await FindItemDetails(Barcode, HttpClient);
    }

    public async Task FindItemDetails(string barcode, HttpClient client)
    {
        if (!string.IsNullOrEmpty(barcode))
        {
            GetBarcodeDetailQuery query = new()
            {
                Barcode = barcode
            };
            await client.GetDataFromApi<GetBarcodeDetailQuery, GetBarcodeDetailViewModel>("Cards/ItemBarcode/GetBarcodeDetail", query, FindBarcodeCallBack);
        }
        else
        {
            await Shell.Current.DisplayAlert("Hata", "Lütfen bir barkod giriniz.", "Ok");
        }
    }

    void FindBarcodeCallBack(GetBarcodeDetailViewModel vm)
    {
        ItemDescription = vm.Item.Description;
        ItemUnit = vm.ItemUnits.FirstOrDefault().ToString();
        ItemLot = vm.Lot.ToString();
        ItemSerialNumber = vm.SerialNumber.ToString();
        Quantity = "0";
    }

    private void SendBarcodeDetails(object sender, EventArgs e)
    {
        ItemBarcodeDetail itemBarcodeDetail = new()
        {
            Barcode = Barcode,
            ItemDescription = ItemDescription,
            ItemUnit = ItemUnit,
            ItemLot = ItemLot,
            ItemSerialNumber = ItemSerialNumber,
            Quantity = Quantity,
        };
        BarcodeDetailsReady?.Invoke(sender, itemBarcodeDetail);
        IsVisibleButtonGrid = "true";
        IsVisibleDetailGrid = "false";
    }
}

相关问题