【问题标题】:How to fire off event handler in xaml from a different class than the code behind?如何从与后面的代码不同的类中触发 xaml 中的事件处理程序?
【发布时间】: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


    【解决方案1】:

    如果你想将Tap事件绑定到Model,你应该使用ICommand来实现。

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

    可以在示例中找到此视图模型的完整代码。相关的Command实现细节如下:

    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
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-30
      • 2013-09-02
      • 2014-09-30
      • 1970-01-01
      相关资源
      最近更新 更多