如何在 Xamarin Forms 中更新标签
How to update label in Xamarin Forms
这里是初学者。
我正在尝试获取准确的时间,此操作成功执行的时间并将其打印在标签上。问题是当我点击按钮时,标签没有更新文本。
namespace HGB.Droid.Helpers
{
public class CallServiceHelper : ICallServiceHelper
{
IContactsHelper contactsHelper = DependencyService.Get<IContactsHelper>();
List<Repository> ObjContactList = new List<Repository>();
LabelModel labelModel = new LabelModel();
Context context = Android.App.Application.Context;
HttpClient client = new HttpClient();
public async Task UpdatePhonebook()
{
if (NetworkCheck.IsInternet())
{
var response = await client.GetAsync("http://mmmmmmmmmmm.aspx");
if (response.IsSuccessStatusCode)
{
string contactsJson = await response.Content.ReadAsStringAsync();
var list = JsonConvert.DeserializeObject<List<Repository>>(contactsJson);
contactsHelper.DeleteContact();
ObjContactList = list;
foreach (Repository obj in ObjContactList)
{
contactsHelper.CreateContacts(obj.name, obj.phone);
}
Device.BeginInvokeOnMainThread(() =>
{
labelModel.UpdateLabelValue.Execute(DateTime.Now.ToString());
});
}
}
else
{
Device.BeginInvokeOnMainThread(() =>
{
Toast.MakeText(context, "Error", ToastLength.Long).Show();
});
}
}
我在 UI 按钮上调用此函数
public partial class MainPage : ContentPage
{
ICallServiceHelper callServiceHelper = DependencyService.Get<ICallServiceHelper>();
public MainPage()
{
InitializeComponent();
}
private async void updateContactsBtn_Clicked(object sender, EventArgs e)
{
await callServiceHelper.UpdatePhonebook();
}
}
这是我的 ViewModel
public class LabelModel : BindableObject
{
string dateValue = "Date Value";
public LabelModel()
{
UpdateLabelValue = new Command<string>(UpdateLabel);
}
public ICommand UpdateLabelValue { get; }
public string DateDisplay
{
get => dateValue;
set
{
dateValue = value;
OnPropertyChanged(nameof(DateDisplay));
}
}
void UpdateLabel(string newLabel)
{
DateDisplay = newLabel;
}
}
这是我的 Xaml 文件
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:HGB.ViewModel"
x:Class="HGB.MainPage">
<ContentPage.BindingContext>
<local:LabelModel/>
</ContentPage.BindingContext>
<StackLayout HorizontalOptions="Center" VerticalOptions="Center" Spacing="10">
<Button
Text="Update Phonebook"
x:Name="updateContactsBtn"
Clicked="updateContactsBtn_Clicked"
/>
<Label
VerticalOptions="Center"
HorizontalOptions="Center"
Text="{Binding DateDisplay}"
/>
</StackLayout>
I'm using the Helper method in my Foreground Service class, where it
gets called every 24 hours. What I'm trying to achieve is print the
exact time, when the phonebook was successfully updated and print that
date to my label.
对于你的问题,一个简单的方法就是使用 MessagingCenter 正如 Jason 提到的那样。
您可以在 CallServiceHelper
中发送消息并在 ViewModel(LabelModel.cs
) 中订阅此消息。
请参考以下代码:
1.In你的ViewMode(LabelModel.cs
)的构造函数,订阅这条消息:
public LabelModel()
{
MessagingCenter.Subscribe<object, string>(this, "time", (sender, args) =>
{
System.Diagnostics.Debug.WriteLine("received time is: "+ args);
DateDisplay = args;
});
}
2.In你的CallServiceHelper
,public你的留言:
MessagingCenter.Send<object, string>(this, "time", "2022-4-8");
这里是初学者。
我正在尝试获取准确的时间,此操作成功执行的时间并将其打印在标签上。问题是当我点击按钮时,标签没有更新文本。
namespace HGB.Droid.Helpers
{
public class CallServiceHelper : ICallServiceHelper
{
IContactsHelper contactsHelper = DependencyService.Get<IContactsHelper>();
List<Repository> ObjContactList = new List<Repository>();
LabelModel labelModel = new LabelModel();
Context context = Android.App.Application.Context;
HttpClient client = new HttpClient();
public async Task UpdatePhonebook()
{
if (NetworkCheck.IsInternet())
{
var response = await client.GetAsync("http://mmmmmmmmmmm.aspx");
if (response.IsSuccessStatusCode)
{
string contactsJson = await response.Content.ReadAsStringAsync();
var list = JsonConvert.DeserializeObject<List<Repository>>(contactsJson);
contactsHelper.DeleteContact();
ObjContactList = list;
foreach (Repository obj in ObjContactList)
{
contactsHelper.CreateContacts(obj.name, obj.phone);
}
Device.BeginInvokeOnMainThread(() =>
{
labelModel.UpdateLabelValue.Execute(DateTime.Now.ToString());
});
}
}
else
{
Device.BeginInvokeOnMainThread(() =>
{
Toast.MakeText(context, "Error", ToastLength.Long).Show();
});
}
}
我在 UI 按钮上调用此函数
public partial class MainPage : ContentPage
{
ICallServiceHelper callServiceHelper = DependencyService.Get<ICallServiceHelper>();
public MainPage()
{
InitializeComponent();
}
private async void updateContactsBtn_Clicked(object sender, EventArgs e)
{
await callServiceHelper.UpdatePhonebook();
}
}
这是我的 ViewModel
public class LabelModel : BindableObject
{
string dateValue = "Date Value";
public LabelModel()
{
UpdateLabelValue = new Command<string>(UpdateLabel);
}
public ICommand UpdateLabelValue { get; }
public string DateDisplay
{
get => dateValue;
set
{
dateValue = value;
OnPropertyChanged(nameof(DateDisplay));
}
}
void UpdateLabel(string newLabel)
{
DateDisplay = newLabel;
}
}
这是我的 Xaml 文件
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:HGB.ViewModel"
x:Class="HGB.MainPage">
<ContentPage.BindingContext>
<local:LabelModel/>
</ContentPage.BindingContext>
<StackLayout HorizontalOptions="Center" VerticalOptions="Center" Spacing="10">
<Button
Text="Update Phonebook"
x:Name="updateContactsBtn"
Clicked="updateContactsBtn_Clicked"
/>
<Label
VerticalOptions="Center"
HorizontalOptions="Center"
Text="{Binding DateDisplay}"
/>
</StackLayout>
I'm using the Helper method in my Foreground Service class, where it gets called every 24 hours. What I'm trying to achieve is print the exact time, when the phonebook was successfully updated and print that date to my label.
对于你的问题,一个简单的方法就是使用 MessagingCenter 正如 Jason 提到的那样。
您可以在 CallServiceHelper
中发送消息并在 ViewModel(LabelModel.cs
) 中订阅此消息。
请参考以下代码:
1.In你的ViewMode(LabelModel.cs
)的构造函数,订阅这条消息:
public LabelModel()
{
MessagingCenter.Subscribe<object, string>(this, "time", (sender, args) =>
{
System.Diagnostics.Debug.WriteLine("received time is: "+ args);
DateDisplay = args;
});
}
2.In你的CallServiceHelper
,public你的留言:
MessagingCenter.Send<object, string>(this, "time", "2022-4-8");