Xamarin构成猴子聊天

时间:2019-02-01 15:37:42

标签: xamarin xamarin.forms

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

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

命名空间Budocode.ViewModels {     公共类MainChatViewModel:BaseViewModel     {         公开的ObservableRangeCollection消息{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
        }


    }

}

}

0 个答案:

没有答案