如何从 Xamarin.forms 中的事件更改 TextView 中的文本
How to change text in TextView from event in Xamarin.forms
我在本机 PageRenderer (Android) 中使用 Xamarin.Forms,我实现了 Geolocator (https://github.com/XLabs/Xamarin-Forms-Labs/wiki/Geolocator) 以在位置更改时使用 OnPositionChanged 事件。但是在他们使用的原始代码中:
private void OnPositionChanged(object sender, PositionEventArgs e)
{
BeginInvokeOnMainThread (() => {
ListenStatus.Text = e.Position.Timestamp.ToString("G");
ListenLatitude.Text = "La: " + e.Position.Latitude.ToString("N4");
ListenLongitude.Text = "Lo: " + e.Position.Longitude.ToString("N4");
});
}
它在我的应用程序上不起作用,现在我实现了类似的东西:
private void OnPositionChanged(object sender, PositionEventArgs e)
{
_activity.RunOnUiThread(() => {
_listenStatus.Text = "Estado:" + e.Position.Timestamp.ToString("G");
_listenLatitude.Text = "Latitud: " + e.Position.Latitude.ToString("N4");
_listenLongitude.Text = "Longitud: " + e.Position.Longitude.ToString("N4");
});
}
当我的 OnElementChanged 是这样的:
protected override void OnElementChanged(ElementChangedEventArgs<Page> e)
{
base.OnElementChanged (e);
_activity = this.Context as Activity;
_activity.SetContentView (Resource.Layout.RequestService);
_view = _activity.LayoutInflater.Inflate(Resource.Layout.RequestService, this, false);
AddView (_view);
SetupElements ();
SetupGeolocator ();
}
还有我的 SetupElements:
void SetupElements ()
{
_listenStatus = _view.FindViewById<TextView> (Resource.Id.ListenStatus);
_listenLatitude = _view.FindViewById<TextView> (Resource.Id.ListenLatitude);
_listenLongitude = _view.FindViewById<TextView> (Resource.Id.ListenLongitude);
}
但它不起作用,数据检索成功,TextViews 加载成功,当 RunOnUiThread 运行 视图没有任何变化。
您可以在自定义呈现器中使用静态页面来更改视图。
假设您有这样一个页面:;
public class SomePage : ContentPage
{
public static SomePage ThisPage {get; set;}
Label label;
public SomePage()
{
ThisPage = this;
label = new Label();
}
}
然后在您的页面渲染器中使用它:
SomePage.ThisPage.label.Text = "SomeText";
希望对您有所帮助。
我在本机 PageRenderer (Android) 中使用 Xamarin.Forms,我实现了 Geolocator (https://github.com/XLabs/Xamarin-Forms-Labs/wiki/Geolocator) 以在位置更改时使用 OnPositionChanged 事件。但是在他们使用的原始代码中:
private void OnPositionChanged(object sender, PositionEventArgs e)
{
BeginInvokeOnMainThread (() => {
ListenStatus.Text = e.Position.Timestamp.ToString("G");
ListenLatitude.Text = "La: " + e.Position.Latitude.ToString("N4");
ListenLongitude.Text = "Lo: " + e.Position.Longitude.ToString("N4");
});
}
它在我的应用程序上不起作用,现在我实现了类似的东西:
private void OnPositionChanged(object sender, PositionEventArgs e)
{
_activity.RunOnUiThread(() => {
_listenStatus.Text = "Estado:" + e.Position.Timestamp.ToString("G");
_listenLatitude.Text = "Latitud: " + e.Position.Latitude.ToString("N4");
_listenLongitude.Text = "Longitud: " + e.Position.Longitude.ToString("N4");
});
}
当我的 OnElementChanged 是这样的:
protected override void OnElementChanged(ElementChangedEventArgs<Page> e)
{
base.OnElementChanged (e);
_activity = this.Context as Activity;
_activity.SetContentView (Resource.Layout.RequestService);
_view = _activity.LayoutInflater.Inflate(Resource.Layout.RequestService, this, false);
AddView (_view);
SetupElements ();
SetupGeolocator ();
}
还有我的 SetupElements:
void SetupElements ()
{
_listenStatus = _view.FindViewById<TextView> (Resource.Id.ListenStatus);
_listenLatitude = _view.FindViewById<TextView> (Resource.Id.ListenLatitude);
_listenLongitude = _view.FindViewById<TextView> (Resource.Id.ListenLongitude);
}
但它不起作用,数据检索成功,TextViews 加载成功,当 RunOnUiThread 运行 视图没有任何变化。
您可以在自定义呈现器中使用静态页面来更改视图。
假设您有这样一个页面:;
public class SomePage : ContentPage
{
public static SomePage ThisPage {get; set;}
Label label;
public SomePage()
{
ThisPage = this;
label = new Label();
}
}
然后在您的页面渲染器中使用它:
SomePage.ThisPage.label.Text = "SomeText";
希望对您有所帮助。