首页 > 解决方案 > Xamarin 形成猴子聊天

问题描述

我在我的移动应用程序中使用猴子聊天(基于 Xamrin 表单)。在 IOS 上发送消息时,在真实设备上,我必须单击发送按钮 2 次。第一次点击时,它会最小化键盘,第二次,它会发送消息。请建议。

如何一键发送消息?这是我的发送消息功能

命名空间 Budocode.ViewModels { public class MainChatViewModel : BaseViewModel { public ObservableRangeCollection Messages { get; } ITwilioMessenger twilioMessenger;

    string outgoingText = string.Empty;

    public string OutGoingText
    {
        get { return outgoingText; }
        set { SetProperty(ref outgoingText, value); }
    }

    public ICommand SendCommand { get; set; }


    public ICommand LocationCommand { get; set; }

    public MainChatViewModel()
    {
        // Initialize with default values
        twilioMessenger = DependencyService.Get<ITwilioMessenger>();

        Messages = new ObservableRangeCollection<ChatMessage>();

        SendCommand = new Command(() =>
        {
            var message = new ChatMessage
            {
                Text = OutGoingText,
                IsIncoming = false,
                ProfileId = "profile" + GlobalSettingsDataSource.Current.SelectedProfileImageId + ".png",
                MessageDateTime = DateTime.Now,
                FromUser = GlobalSettingsDataSource.Current.SelectedProfile
            };


            if (string.IsNullOrWhiteSpace(OutGoingText))
                return;

            Messages.Add(message);

            twilioMessenger?.SendMessage(message.Text, message.ProfileId, GlobalSettingsDataSource.Current.SelectedProfile);

            OutGoingText = string.Empty;

        });


        LocationCommand = new Command(async () =>
        {
            try
            {
                var local = await CrossGeolocator.Current.GetPositionAsync(TimeSpan.FromSeconds(15));
                var map = $"https://maps.googleapis.com/maps/api/staticmap?center={local.Latitude.ToString(CultureInfo.InvariantCulture)},{local.Longitude.ToString(CultureInfo.InvariantCulture)}&zoom=17&size=400x400&maptype=street&markers=color:red%7Clabel:%7C{local.Latitude.ToString(CultureInfo.InvariantCulture)},{local.Longitude.ToString(CultureInfo.InvariantCulture)}&key=";

                var message = new ChatMessage
                {
                    Text = "I am here",
                    AttachementUrl = map,
                    ProfileId = "profile" + GlobalSettingsDataSource.Current.SelectedProfileImageId + ".png",
                    IsIncoming = false,
                    MessageDateTime = DateTime.Now
                };

                Messages.Add(message);
                twilioMessenger?.SendMessage("attach:" + message.AttachementUrl, message.ProfileId, GlobalSettingsDataSource.Current.SelectedProfile);

            }
            catch (Exception ex)
            {

            }
        });


        if (twilioMessenger == null)
            return;

        twilioMessenger.MessageAdded = (message) =>
        {
            //if (message.ProfileId == "Icon.png")

            Device.BeginInvokeOnMainThread(() =>
            {
                if (message.FromUser != GlobalSettingsDataSource.Current.SelectedProfile)
                {
                    message.IsIncoming = true;
                }
                else
                {
                    message.IsIncoming = false;
                }
                Messages.Add(message);
            });


        };
    }


    public async void InitializeMock(string channelName)
    {

        try
        {
            var id = CrossDeviceInfo.Current.Id;
            var userId = PersistantData.Current.UserAccount.Properties["user_id"];

            HttpResponseMessage appResponse = CommonUtility.GetApiContent(
                String.Format(ClientConfiguration.TwilioServiceChatHistory, id, GlobalSettingsDataSource.Current.SelectedProfile, channelName));
            if (appResponse.IsSuccessStatusCode)
            {
                var chatContent = await appResponse.Content.ReadAsStringAsync();
                List<ChatMessage> chatMsgs = JsonConvert.DeserializeObject<List<ChatMessage>>(chatContent);
                Messages.ReplaceRange(chatMsgs);
            }
        }
        catch
        {

            // ignore if there are any issues with twilio
        }


    }

}

}

标签: xamarinxamarin.forms

解决方案


推荐阅读