gpt4 book ai didi

Where to do Content View Bussiness Logic Operations(在哪里执行内容视图业务逻辑操作)

转载 作者:bug小助手 更新时间:2023-10-22 14:29:34 32 4
gpt4 key购买 nike



I'm working on a .net maui app and i need to get and set data in my custom control.

我正在开发一个.net maui应用程序,我需要在我的自定义控件中获取和设置数据。


The goal is:

目标是:


Operation Template

操作模板


The page:

页面:


    <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>

Custom control xaml:

自定义控件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>

When i press search button i want to execute this command for set itemdescription. This is the bussiness logic of that content view and i dont want to repeat this code over and over again.:

当我按下搜索按钮时,我想执行这个命令来设置项目描述。这是该内容视图的商业逻辑,我不想一遍又一遍地重复此代码。:


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);

Where should i place this operation and how? i cant figure it out for a week.

我应该把这个手术放在哪里,怎么做?我一个星期都想不通。


Custom control xaml.cs :

自定义控件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";
}
}

Hope someone here can help me.

希望这里有人能帮助我。


更多回答

Does this answer your question? Calling a parameterized constructor from XAML

这能回答你的问题吗?从XAML调用参数化构造函数

@H.A.H. thank you for your answer but also i asked this: should contentview has bussiness logic in it or should i have to bind it to viewmodel or something else i cant find the answer

@H.A.H.谢谢你的回答,但我也问了这个问题:contentview应该有商业逻辑吗?还是我必须将它绑定到viewmodel或其他什么?我找不到答案

@Okanalagas All business logic belongs to a ViewModel. That ViewModel can hold reference to other classes, that contain reusable code, sometimes named "Services". Avoid putting business logic in your ContentView (Page, Custom control or something else) at all cost. I am using CommunityToolkit.MVVM. Highly recommending it for MAUI.

@Okanalagas所有业务逻辑都属于ViewModel。该ViewModel可以保存对其他类的引用,这些类包含可重用代码,有时名为“服务”。避免不惜一切代价将业务逻辑放在ContentView(页面、自定义控件或其他控件)中。我正在使用CommunityToolkit.MVVM。强烈推荐用于MAUI。

优秀答案推荐

The ContentView class defines a Content property, of type View, which represents the content of the ContentView. This property is backed by a BindableProperty object, which means that it can be the target of data bindings, and styled.

ContentView类定义了一个View类型的Content属性,该属性表示ContentView的内容。此属性由BindableProperty对象支持,这意味着它可以成为数据绑定的目标,并设置样式。


The ContentView class offers little functionality by itself but can be used to create a custom control. The process for creating a custom control is to:

ContentView类本身提供的功能很少,但可以用于创建自定义控件。创建自定义控件的过程是:



  • Create a class that derives from the ContentView class.

  • Define any control properties or events in the code-behind file for
    the custom control.

  • Define the UI for the custom control.


For more information, you can check official document: Create a custom control.

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


Note:

注:


You can also check the sample here ContentView Demos which will help you understand how to create a custom control with ContentView. Although this sample is an app for Xamarin form, it also applies to MAUI.

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



I did something like this it works but is it best practice to use im not sure.

我做了这样的事情,它有效,但它是最好的做法使用我不确定。


Here is the page:

这是页面:


<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;
}
}

Here is custom control :

以下是自定义控件:


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";
}
}

更多回答

Can i use services like httpclientfactory in custom controls ? Is it best practice to do it for repetitive operations

我可以在自定义控件中使用httpclientfactory之类的服务吗?重复性操作的最佳做法是这样做吗

ContentView is a control used to present various UIs and is reusable. For code such as service like httpclient, we generally do not recommend adding it to the ContentView. We often use MVVM implementations, often putting code such as service requests in the ViewModel. For more information about MVVM, you can check document The Model-View-ViewModel Pattern.

ContentView是一个用于呈现各种UI的控件,并且是可重用的。对于像httpclient这样的服务代码,我们通常不建议将其添加到ContentView中。我们经常使用MVVM实现,经常将服务请求等代码放在ViewModel中。有关MVVM的更多信息,您可以查看文档The Model View ViewModel Pattern。

Hi @Okanalagas, we usually set the BindContextt for our pages not the customview(e.g. ItemBarcodeSearchView ). You can add the HttpClient to the Page's ViewModel and request data in the Page's ViewModel. And there are some similar threads about this, you can check them here: thread 1 and thread 2.

嗨@Okanalagas,我们通常为页面设置BindContextt,而不是自定义视图(例如ItemBarcodeSearchView)。您可以将HttpClient添加到页面的ViewModel中,并在页面的ViewModel中请求数据。还有一些类似的线程,你可以在这里检查它们:线程1和线程2。

Thanks for answer but still i want to ask this. I want to use this component in multiple places. Wouldn't such an approach lead to code repetition?

谢谢你的回答,但我还是想问这个。我想在多个地方使用这个组件。这样的方法不会导致代码重复吗?

Yes, a custom control itself can be reused. We just need to pass necessary parameters to it.

是的,自定义控件本身可以重复使用。我们只需要将必要的参数传递给它。

You need to provide an explanation for the content you've shared in your response.

你需要对你在回复中分享的内容做出解释。

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com