【发布时间】:2019-09-21 00:42:42
【问题描述】:
点击 JournalWarning 标签时,我想引发“重新连接”事件
我在一个类中有一个事件处理程序,它根据开关情况引发不同的事件。在我的一个观点中,我想在点击标签时触发“重新连接”事件。我很难弄清楚如何做到这一点,特别是因为事件不在代码后面,而是在一般的不同类中。
我不擅长处理事件处理程序和数据绑定,但希望在我让它更好地工作之后,我可以更多地思考它。
在后面的代码中,我有一些数据绑定正在处理我的事件处理程序,但我还没有开始使用 TapGestureRecognizer 。
我认为它可能类似于 JournalWarning.SetBinding(TapGestureRecognizer, "Reconnecting");
这是在我的 JournalSocket.cs 中
private void Sock_DabSocketEvent(object sender, DabSocketEventHandler e)
{
//An event has been fired by the socket. Respond accordingly
//Log the event to the debugger
Debug.WriteLine($"{e.eventName} was fired with {e.data}");
//Take action on the event
switch (e.eventName.ToLower())
{
case "disconnected": //Socket disconnected
Sock_Disconnected(e.data);
break;
case "connected": //Socket connected
Sock_Connected(e.data);
break;
case "reconnecting": //Socket reconnecting
//do nothing for now
break;
case "reconnected": //Socket reconnected
Sock_Connected(e.data);
break;
case "room_error": //Error with a room
Sock_ErrorOccured(e.eventName, e.data);
break;
case "join_error": //Error joining
Sock_ErrorOccured(e.eventName, e.data);
break;
case "auth_error": //Error with authentication
Sock_ErrorOccured(e.eventName, e.data);
break;
case "update": //update happened externally
Sock_ExternalUpdateOccured(e.eventName, e.data);
break;
default:
break;
}
}
这是我的 PlayerPage.xaml
<Label x:Name="JournalWarning"
Text="Your device has lost its connection to the journal server. Journals cannot be viewed or edited at this time. Tap here to try and reconnect."
Style="{StaticResource warningLabelStyle}"
FontSize="Medium"
VerticalOptions="EndAndExpand"
AutomationProperties.IsInAccessibleTree="true">
<Label.GestureRecognizers>
<TapGestureRecognizer Tapped="OnReconnect"/>
</Label.GestureRecognizers>
</Label>
这是我在 PlayerPage.cs 后面的代码,我在其中进行了一些其他成功的数据绑定,并认为我也可以在这里完成我想要的。不过可能完全错了。
void BindControls(bool BindToEpisode, bool BindToPlayer)
{
if (BindToEpisode)
{
//Journal
JournalTitle.BindingContext = Episode;
JournalTitle.SetBinding(Label.TextProperty, "title");
JournalContent.BindingContext = journal;
JournalContent.SetBinding(Editor.TextProperty, "Content");
JournalContent.SetBinding(Editor.IsEnabledProperty, "IsConnected");
JournalWarning.BindingContext = journal;
JournalWarning.SetBinding(IsVisibleProperty, "IsDisconnected");
}
非常感谢任何帮助!
【问题讨论】:
标签: c# xaml xamarin eventhandler