gpt4 book ai didi

xamarin - 是否可以在 View 模型中包含方法,或者应该将它们放置在代码的另一部分中?

转载 作者:行者123 更新时间:2023-12-02 17:42:14 26 4
gpt4 key购买 nike

我的 Xamarin Forms ViewModel 如下所示:

public class CFSPageViewModel : BaseViewModel
{
#region Constructor

public CFSPageViewModel()
{
PTBtnCmd = new Command<string>(PTBtn);
OnTappedCmd = new Command<string>(OnTapped);
}

#endregion

# region Commands

public ICommand PTBtnCmd { get; set; }
public ICommand OnTappedCmd { get; }

#endregion

#region Methods

private void OnTapped(string btnText)
{
Utils.SetState(btnText, CFS, SET.Cfs);
CFSMessage = Settings.cfs.TextLongDescription();
}

private void PTBtn(string btnText)
{
Utils.SetState(btnText, PT);
SetLangVisible(btnText);
SetLangSelected(btnText);
CFSMessage = Settings.cfs.TextLongDescription();
}


}

我之前使用 MessageCenter 向我的 C# 后端代码发送消息,但现在已删除 MessageCenter,因此这些方法是 ViewModel 的一部分。

这样做安全吗?我听说在 ViewModel 之间传递 MessageCenter 消息并不是最好的解决方案。

请注意,这是我之前的做法:

MyPageViewModel.cs

PTBtnCmd = new Command<Templates.WideButton>((btn) =>             
MessagingCenter.Send<CFSPageViewModel, Templates.WideButton>(
this, "PTBtn", btn));

MyPage.xaml.cs

MessagingCenter.Subscribe<CFSPageViewModel, Templates.WideButton>(
this, "PTBtn", (s, btn) =>
{
Utils.SetState(btn.Text, vm.PT);
SetLangVisible(btn.Text);
SetLangSelected(btn.Text);
vm.CFSMessage = Settings.cfs.TextLongDescription();
});

请注意,SetLangVisible 等方法也在 MyPage.xaml.cs 中

最佳答案

要向您的Button添加事件处理程序,只需:

<ContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MyProject.Views.MyPage">
<ContentPage.Content>
<StackLayout>
<Button Text="PTBtn" Clicked="Handle_Clicked" />
</StackLayout>
</ContentPage.Content>
</ContentPage>

在代码后面:

namespace MyProject.Views
{
public partial class MyPage : ContentPage
{
public MyPage()
{
InitializeComponent();
}
void Handle_Clicked(object sender, EventArgs eventArgs)
{
((Button)sender).BackgroundColor = Color.Blue; // sender is the control the event occured on

// Here call your methods depending on what they do/if they are view related
/*
Utils.SetState(btn.Text, vm.PT);
SetLangVisible(btn.Text);
SetLangSelected(btn.Text);
vm.CFSMessage = Settings.cfs.TextLongDescription();
*/
}
}
}

所有可以分配事件处理程序的事件都以黄色列出,并带有 E: enter image description here

<小时/>

Command 首先触发,您可以在构造函数中添加 CanExecute 作为第二个参数 - 这也将停止执行命令和事件处理程序。

我还将 Command 重命名为 SelectLanguageCommand 之类的名称 - 以将其与 ui 操作区分开来。这样,您可以断开按钮与命令的连接并将命令连接到其他 ui - 如果您决定将来要更改 View 。单元测试时也更容易理解。

<小时/>

Is this a safe thing to do? I heard that MessageCenter messages passing around between ViewModels for everything was not the best of solutions.

您可以使用DependencyService注册所有 View 模型

public App()
{
InitializeComponent();

DependencyService.Register<AboutViewModel>();
DependencyService.Register<CFSPageViewModel>();
DependencyService.Register<MyPageViewModel>();
MainPage = new AppShell();
}

将 View 的BindingContext设置为注册的实例:

public AboutPage()
{
InitializeComponent();
BindingContext = DependencyService.Get<AboutViewModel>();
}

并在您需要的任何地方获取 ViewModel 实例。这样,您就不必像使用 MessagingCenter 时那样处理订阅。

它是否安全 - 我不确定。

关于xamarin - 是否可以在 View 模型中包含方法,或者应该将它们放置在代码的另一部分中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56722493/

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