我正在使用一个.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";
}
}
希望这里有人能帮助我。
2条答案
按热度按时间xwmevbvl1#
ContentView
类定义了一个View类型的Content属性,它表示ContentView
的内容。此属性由BindableProperty
对象支持,这意味着它可以成为数据绑定的目标,并具有样式。ContentView类本身提供的功能很少,但可用于创建自定义控件。创建自定义控件的过程是:
有关更多信息,您可以查看官方文档:创建自定义控件。
注:
您也可以在这里查看示例ContentView Demos,它将帮助您了解如何使用
ContentView
创建自定义控件。虽然此示例是Xamarin表单的应用程序,但它也适用于MAUI。dkqlctbz2#
我做了这样的事情,它的工作,但它是最好的做法,使用我不确定。
这里是页面:
下面是自定义控件: