我想在C#中的win form应用程序中使用botsharp库 [英] I want to use botsharp library in win form application in C#

查看:79
本文介绍了我想在C#中的win form应用程序中使用botsharp库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在C#中的win form应用程序中使用botsharp库来使用电报机器人

但我要面对这个错误

('消息'是一个模糊的参考在'System.Windows.Forms.Message'和'TelegramBotSharp.Types.Message'之间)









foreach(结果中的消息m)



private static void ControlMessages1(消息m)



我尝试了什么:



i want to use botsharp library in win form application in C# for use telegram bot
but i To face whith this error
( 'Message' is an ambiguous refrence between 'System.Windows.Forms.Message' and 'TelegramBotSharp.Types.Message')




foreach (Message m in result)
and
private static void ControlMessages1(Message m)

What I have tried:

using System;
using System.Threading.Tasks;
using System.Windows.Forms;
using TelegramBotSharp;
using TelegramBotSharp.Types;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public static TelegramBot bot;
        public Form1()
        {
            InitializeComponent();


        }

        private void button1_Click(object sender, EventArgs e)
        {
            bot = new TelegramBot("146900***********************-FyoSPg");
             new Task(PollMessages1).Start();
        }

        static async void PollMessages1()
        {
            while (true)
            {
                var result = await bot.GetMessages();
                foreach (Message m in result)
                {
                    if (m.Chat != null)
                    {
                        Console.WriteLine("[{0}] {1} : {2}", m.Chat.Title, m.From.Username, m.Text);
                    }
                    else
                    {
                        Console.WriteLine("{0}: {1}", m.From.Username, m.Text);
                    }
                    ControlMessages1(m);
                }

            }
        }

        private static void ControlMessages1(Message m)
        {
            if (m.Text == null) return;
            MessageTarget target = (MessageTarget)m.Chat ?? m.From;

                bot.SendMessage(target, "hello");
         }
    }
}

推荐答案

你无法理解命名空间等基本和简单的东西,所以我不能建议修复这个基本的bug,继续使用Telegram Bots或任何先进的东西。您应该更好地学习编程和.NET的基础知识,这对于在最简单的仅限控制台的应用程序上学习更有效率。



但是,修复方法是:1 )使用完整类型名称, System.Windows.Forms.Message TelegramBotSharp.Types.Message ,2)或者,修改用于重命名两种类型之一;例如使用TelegramBotSharpMessage = TelegramBotSharp.Types.Message ,4)想想你是否需要 System.Windows.Forms 时间:初学者中最丑陋的纯粹有条不紊的错误之一是使用过多,特别是没有清理一些自动生成的代码,5)如果确实需要,你可以使用更好的替代方案:隔离来自Bot的UI东西,所以Forms和 TelegramBotSharp 命名空间永远不会发生冲突(但它不会让你免于学习在其他一些情况下需要使用的名称解析技术无论如何)。



学习。不要保持原样。本主题是.NET中最简单的主题之一。如果您在没有完全理解的情况下离开问题,很快就会遇到大量累积的问题。



-SA
You fail to understand such basic and simple thing as namespaces, so I cannot advise to fix this elementary bug and go ahead with Telegram Bots or anything advanced at all. You should better learn the very basics of programming and .NET, which is much more productive to learn on simplest console-only applications.

However, the fix is: 1) use full type names, System.Windows.Forms.Message or TelegramBotSharp.Types.Message, 2) alternatively, modify using to rename one of both of the type in question; for example using TelegramBotSharpMessage = TelegramBotSharp.Types.Message, 4) think if you need System.Windows.Forms in this time at all: one of the ugliest purely methodical mistakes of the beginners is having too much "using", in particular, not cleaning up some auto-generated code, 5) if it is really needed, you can use even better alternative: segregate UI stuff from Bot stuff, so the Forms and TelegramBotSharp namespace never clash (but it won't free you from learning the name resolution techniques which you need to use in some other cases anyway).

Learn. Don't leave it as is. This topic is one of the simplest in .NET. If you ever leave issues without full understanding, you will soon get lost in the tons of accumulated problems.

—SA


这篇关于我想在C#中的win form应用程序中使用botsharp库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆