C# 如何从其他页面访问列表视图?(Xamarin表单)
我想从主页修改列表视图的内容。列表视图位于另一个名为LoadResultsPage的页面中。以下是主页中调用函数的按钮:C# 如何从其他页面访问列表视图?(Xamarin表单),c#,xamarin,xamarin.forms,xamarin.android,xamarin.ios,C#,Xamarin,Xamarin.forms,Xamarin.android,Xamarin.ios,我想从主页修改列表视图的内容。列表视图位于另一个名为LoadResultsPage的页面中。以下是主页中调用函数的按钮: <Button Text="Load Results" Clicked="FetchData"></Button> 以下是调用的函数: public async void FetchData(object sender, System.EventArgs e) { /* string
<Button Text="Load Results" Clicked="FetchData"></Button>
以下是调用的函数:
public async void FetchData(object sender, System.EventArgs e)
{
/* string apiUrl = null;
if (Device.RuntimePlatform == Device.Android)
apiUrl = "https://10.0.2.2:5001/api";
else if (Device.RuntimePlatform == Device.iOS)
apiUrl = "https://localhost:5001/api";
else
throw new Exception();*/
await Navigation.PushAsync(new LoadResultsPage());
var httpClient = new HttpClient();
var response = await httpClient.GetStringAsync("http://localhost:5001/api/Calcs");
var login = JsonConvert.DeserializeObject<List<Calc>>(response);
Lista.ItemsSource = login;
}
public async void FetchData(对象发送方,System.EventArgs e)
{
/*字符串apirl=null;
if(Device.RuntimePlatform==Device.Android)
apiUrl=”https://10.0.2.2:5001/api";
else if(Device.RuntimePlatform==Device.iOS)
apiUrl=”https://localhost:5001/api";
其他的
抛出新异常()*/
等待Navigation.PushAsync(新的LoadResultsPage());
var httpClient=新的httpClient();
var response=wait httpClient.GetStringAsync(“http://localhost:5001/api/Calcs");
var login=JsonConvert.DeserializeObject(响应);
Lista.ItemsSource=登录名;
}
此处无法识别Lista(位于函数底部),错误为:“名称'Lista'在当前上下文中不存在”
最后是页面LoadResultsPage的内容,我要修改的列表视图是:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Calculator.Views.LoadResultsPage">
<Grid VerticalOptions="FillAndExpand">
<ListView x:Name="Lista" >
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell >
<StackLayout >
<Label Text="{Binding Id}" TextColor="Black"></Label>
<Label Text="{Binding Value}"></Label>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
如何使用变量“login”中的信息填充这两个标签?名称字段不是公共属性,因此默认情况下无法在页面外访问它 有两个选项可供您提问:
请阅读有关MVVM的信息,我的建议将在全球范围内改变您的观念。它被认为不是在页面之间访问ui元素的最佳样式 无论如何,直接回答您的问题使用一些静态模型在应用程序内全局交换数据
public class Exchange
{
private static Exchange _instance;
public static Exchange Data
{
get
{
if (_instance == null)
{
_instance = new Exchange();
}
return _instance;
}
}
public string Buffer { get; set; }
public ListView MyListView { get; set; }
}
示例:Exchange.Data.Buffer
可以从任何地方访问
在InitializeComponent()之后的页面构造函数中为您的ListView创建一个代码>设置
Exchange.Data.MyListView = Lista;
现在最重要的一点是,如果您从另一个页面访问ListView,您必须在UI线程上进行访问。例如:
Device.BeginInvokeOnMainThread(() =>
{
// Update the UI
Exchange.Data.MyListView.ItemsSource = whatever;
});
此处建议为LoadResultsPage传递登录数据,然后在出现LoadResultsPage
时显示数据
例如,修改FetchData
方法如下:
public async void FetchData(object sender, System.EventArgs e)
{
var httpClient = new HttpClient();
var response = await httpClient.GetStringAsync("http://localhost:5001/api/Calcs");
var login = JsonConvert.DeserializeObject<List<Calc>>(response);
//Lista.ItemsSource = login;
await Navigation.PushAsync(new LoadResultsPage(login));
}
为什么不通过构造函数将调用结果传递给LoadResultsPage
?或者为什么不能调用API本身?
public partial class DetailPage : ContentPage
{
public DetailPage(Model login)
{
InitializeComponent();
Lista.ItemsSource = login;
}
}