Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从与代码隐藏不同的类启动xaml中的事件处理程序?_C#_Xaml_Xamarin_Eventhandler - Fatal编程技术网

C# 如何从与代码隐藏不同的类启动xaml中的事件处理程序?

C# 如何从与代码隐藏不同的类启动xaml中的事件处理程序?,c#,xaml,xamarin,eventhandler,C#,Xaml,Xamarin,Eventhandler,点击JournalWarning标签时,我想引发“重新连接”事件 我在一个类中有一个事件处理程序,它根据开关情况引发不同的事件。在我的一个视图中,我想在点击标签时触发“重新连接”事件。我很难弄清楚怎么做,尤其是因为事件不在代码隐藏中,而是在一个不同的类中 我还不擅长处理事件处理程序和数据绑定,但我希望在这方面做得更好之后,我可以更加专注于它 在后面的代码中,我有一些数据绑定正在进行,这会影响我的事件处理程序,但我还没有开始使用TapGestureRecognitzer one 我想这可能有点像J

点击JournalWarning标签时,我想引发“重新连接”事件

我在一个类中有一个事件处理程序,它根据开关情况引发不同的事件。在我的一个视图中,我想在点击标签时触发“重新连接”事件。我很难弄清楚怎么做,尤其是因为事件不在代码隐藏中,而是在一个不同的类中

我还不擅长处理事件处理程序和数据绑定,但我希望在这方面做得更好之后,我可以更加专注于它

在后面的代码中,我有一些数据绑定正在进行,这会影响我的事件处理程序,但我还没有开始使用TapGestureRecognitzer one

我想这可能有点像JournalWarning.SetBinding(TapGestureRecognitor,“重新连接”)

这是我的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>

非常感谢您的帮助

如果要将
点击
事件绑定到模型,应该使用来实现

使用模型-视图-模型(MVVM)模式的应用程序通常使用
ICommand
,而不是直接连接事件处理程序。通过在代码中设置绑定,可以轻松支持
ICommand

<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 Command="{Binding TapCommand}" CommandParameter="paramater"/>
      </Label.GestureRecognizers>
</Label>

如果要将
点击
事件绑定到模型,应该使用来实现

使用模型-视图-模型(MVVM)模式的应用程序通常使用
ICommand
,而不是直接连接事件处理程序。通过在代码中设置绑定,可以轻松支持
ICommand

<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 Command="{Binding TapCommand}" CommandParameter="paramater"/>
      </Label.GestureRecognizers>
</Label>
public class TapViewModel : INotifyPropertyChanged
{
    int taps = 0;
    ICommand tapCommand;
    public TapViewModel () {
        // configure the TapCommand with a method
        tapCommand = new Command (OnTapped);
    }
    public ICommand TapCommand {
        get { return tapCommand; }
    }
    void OnTapped (object s)  {
        taps++;
        Debug.WriteLine ("parameter: " + s);
    }
    //region INotifyPropertyChanged code omitted
}