gpt4 book ai didi

c# - 在代码隐藏中使用数据绑定(bind)

转载 作者:行者123 更新时间:2023-12-03 11:02:51 25 4
gpt4 key购买 nike

我有一个页面,我将命令绑定(bind)到按钮。当我单击它时,我会调用一个方法,从 API 中获取我想要的数据。如果我不想只在 View 中绑定(bind)数据,还想在后面的代码中使用这些数据怎么办?!
可以说这是我的观点:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="HelloWorld.Pages.JohnDoePage"
xmlns:local="clr-namespace:Xamarin.Forms.Maps;assembly=Xamarin.Forms.Pages"
xmlns:vm="clr-namespace:HelloWorld.ViewModel;assembly=HelloWorld">
<StackLayout Padding="20, 10" HorizontalOptions="Center">
<Button Command="{Binding JohnDoe}"
Text="Data about John Doe" />
</StackLayout>
</ContentPage>

后面的代码:
    Models.Info info;
public JohnDoePage(Models.Info info)
{
InitializeComponent();
BindingContext = new InfoDetailsViewModel(info);
this.info= info;

// i want to use the data here
//using the data
}

View 模型:
     Data _data;
public Data Data
{
get { return _data; }
set
{
if (value == _data) return;
_data = value;
OnPropertyChanged();
}
}
public ICommand JohnDoe
{
get
{
return new Command(async () =>
{
var Dataa = await _apiServices.InfoAboutJohnDOe();
});
}
}

我获得所需数据的服务还可以。我使用相同的 View 模型来绑定(bind)不同的命令,顺便说一句我不知道这是否可行,所以我被卡住了。知道如何使用在 View 代码隐藏中获得的数据吗?提前致谢!!

最佳答案

为什么不只维护对 VM 的类级别引用?

Models.Info info;
InfoDetailsViewModel vm;

public JohnDoePage(Models.Info info)
{
InitializeComponent();
vm = new InfoDetailsViewModel(info);
BindingContext = vm;
this.info= info;
}

关于c# - 在代码隐藏中使用数据绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48141434/

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