完善主體資料,免費(fèi)贈(zèng)送VIP會(huì)員!
    * 主體類(lèi)型
    * 企業(yè)名稱(chēng)
    * 信用代碼
    * 所在行業(yè)
    * 企業(yè)規(guī)模
    * 所在職位
    * 姓名
    * 所在行業(yè)
    * 學(xué)歷
    * 工作性質(zhì)
    請(qǐng)先選擇行業(yè)
    您還可以選擇以下福利:
    行業(yè)福利,領(lǐng)完即止!

    下載app免費(fèi)領(lǐng)取會(huì)員

    NULL

    ad.jpg

    二次開(kāi)發(fā)教程:C# udp小程序

    發(fā)布于:2019-07-25 15:22:07

    網(wǎng)友投稿

    更多

    學(xué)習(xí)udp,寫(xiě)個(gè)小聊天程序,供以后查看


    使用了MVVMLight


        /// <summary>

        /// This class contains properties that the main View can data bind to.

        /// <para>

        /// See http://www.mvvmlight.net

        /// </para>

        /// </summary>

        public class MainViewModel : ViewModelBase

        {

            private int port1 = 8981;

            private int port2 = 8982;

            private UdpClient user1 = null;//new UdpClient(8981);

            private UdpClient user2 = null;//new UdpClient(port2);

            private string hostname = "127.0.0.1";

            private readonly IDataService _dataService;

            /// <summary>

            /// The <see cref="WelcomeTitle" /> property's name.

            /// </summary>

            public const string WelcomeTitlePropertyName = "WelcomeTitle";

            private string _welcomeTitle = string.Empty;

            /// <summary>

            /// Gets the WelcomeTitle property.

            /// Changes to that property's value raise the PropertyChanged event. 

            /// </summary>

            public string WelcomeTitle

            {

                get

                {

                    return _welcomeTitle;

                }

                set

                {

                    Set(ref _welcomeTitle, value);

                }

            }


            /// <summary>

            /// Initializes a new instance of the MainViewModel class.

            /// </summary>

            public MainViewModel(IDataService dataService)

            {

                _dataService = dataService;

                _dataService.GetData(

                    (item, error) =>

                    {

                        if (error != null)

                        {

                            // Report error here

                            return;

                        }


                        WelcomeTitle = item.Title;

                    });


                user1 = new UdpClient(port1);

                user2 = new UdpClient(port2);


                Task task1 = new Task(() =>

                {

                    while (true)

                    {

                        IPEndPoint remoteEP = null;

                        byte[] message = user1.Receive(ref remoteEP);

                        string msg = Encoding.UTF8.GetString(message);

                        msg = chatInfo + "\n" + msg;

                        Set(ref chatInfo, msg,false,nameof(ChatInfo));

                    }

                });

                task1.Start();


                Task task2 = new Task(() =>

                {

                    while (true)

                    {

                        IPEndPoint remoteEP = null;

                        byte[] message = user2.Receive(ref remoteEP);

                        string msg = Encoding.UTF8.GetString(message);

                        msg = chatInfo + "\n" + msg;

                        Set(ref chatInfo, msg, false, nameof(ChatInfo));

                    }

                });

                task2.Start();

            }



            private string chatInfo = string.Empty;

            public string ChatInfo

            {

                get

                {

                    return chatInfo;

                }

                set

                {

                    chatInfo = value;

                }

            }


            private string message1 = string.Empty;

            public string Message1

            {

                get

                {

                    return message1;

                }

                set

                {

                    // message1 = value;     

                    Set(ref message1, value);

                    RaisePropertyChanged(nameof(User1_Send));     

                }

            }


            private string message2 = string.Empty;

            public string Message2

            {

                get

                {

                    return message2;

                }

                set

                {

                    //message2 = value;

                    Set(ref message2, value);

                    RaisePropertyChanged(nameof(User2_Send));

                }

            }


            public ICommand User1_Send

            {

                get

                {

                    return new RelayCommand(() =>

                    {

                        message1 = "user1:" + message1;

                        byte[] msg = Encoding.UTF8.GetBytes(message1);

                        IPAddress id = IPAddress.Parse(hostname);

                        IPEndPoint ip = new IPEndPoint(id, port2);

                        user1.Send(msg, msg.Length, ip);

                        Set(ref message1, null,false,nameof(Message1));

                        RaisePropertyChanged(nameof(User1_Send));

                    }, ()=> {

                        return !string.IsNullOrWhiteSpace(message1);

                    });

                }

            }


            public ICommand User2_Send

            {

                get

                {

                    return new RelayCommand(() =>

                    {

                        message2 = "user2:" + message2;

                        byte[] msg = Encoding.UTF8.GetBytes(message2);

                        IPAddress id = IPAddress.Parse(hostname);

                        IPEndPoint ip = new IPEndPoint(id, port1);

                        user2.Send(msg, msg.Length, ip);

                        Set(ref message2, null, false, nameof(Message2));

                        RaisePropertyChanged(nameof(User2_Send));

                    }, () => {

                        return !string.IsNullOrWhiteSpace(message2);

                    });

                }

            }

            public override void Cleanup()

            {

                // Clean up if needed

                user1.Close();

                user2.Close();

                base.Cleanup();

            }

        }




    xmal:


    <Window x:Class="ChatDemo.MainWindow"

            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

            xmlns:ignore="http://www.galasoft.ch/ignore"

            mc:Ignorable="d ignore"

            SizeToContent="WidthAndHeight"

            Title="MVVM Light Application"

            DataContext="{Binding Main, Source={StaticResource Locator}}">

        

        <Window.Resources>

            <ResourceDictionary>

                <ResourceDictionary.MergedDictionaries>

                    <ResourceDictionary Source="Skins/MainSkin.xaml" />

                </ResourceDictionary.MergedDictionaries>

            </ResourceDictionary>

        </Window.Resources>



        <Grid x:Name="LayoutRoot">

            <Grid.ColumnDefinitions>

                <ColumnDefinition/>

                <ColumnDefinition/>

            </Grid.ColumnDefinitions>

            <Grid.RowDefinitions>

                <RowDefinition/>

                <RowDefinition/>

                <RowDefinition/>

            </Grid.RowDefinitions>

            <TextBox  Text="{Binding Path=ChatInfo,UpdateSourceTrigger=PropertyChanged}" Grid.Row="0" Grid.Column="0" IsReadOnly="True"/>

            <TextBox  Text="{Binding Path=Message1,UpdateSourceTrigger=PropertyChanged}" Grid.Row="1" Grid.Column="0" Height="100"/>

            <Button  Content="發(fā)送" Command="{Binding Path=User1_Send}" Grid.Row="2" Grid.Column="0"/>

            <TextBox   Text="{Binding Path=ChatInfo,UpdateSourceTrigger=PropertyChanged}" Grid.Row="0" Grid.Column="1" IsReadOnly="True"/>

            <TextBox   Text="{Binding Path=Message2,UpdateSourceTrigger=PropertyChanged}" Grid.Row="1" Grid.Column="1" Height="100"/>

            <Button Content="發(fā)送" Command="{Binding Path=User2_Send}" Grid.Row="2" Grid.Column="1"/>

        </Grid>

    </Window>

    本文版權(quán)歸腿腿教學(xué)網(wǎng)及原創(chuàng)作者所有,未經(jīng)授權(quán),謝絕轉(zhuǎn)載。

    未標(biāo)題-1.jpg

    上一篇:二次開(kāi)發(fā)教程:C# 通過(guò)MVVMLight探索IOC

    下一篇:二次開(kāi)發(fā)教程:WPF 設(shè)置快捷鍵

    主站蜘蛛池模板: 中文字幕一区二区三区日韩精品| 丝袜美腿一区二区三区| 日本在线视频一区二区| 精品国产精品久久一区免费式| 一区二区不卡视频在线观看| 国产精品视频一区二区三区经| 国产亚洲无线码一区二区| 无码精品人妻一区二区三区免费| 中文字幕日韩欧美一区二区三区| 在线观看亚洲一区二区| 日本一区二区三区高清| 波多野结衣高清一区二区三区| 精品久久综合一区二区| 四虎在线观看一区二区| 自拍日韩亚洲一区在线| 久久久久人妻一区二区三区| 色偷偷久久一区二区三区| 国产激情无码一区二区app| 国产一区二区精品久久凹凸| 日韩视频在线观看一区二区| 日韩国产一区二区| 午夜肉伦伦影院久久精品免费看国产一区二区三区 | 在线视频一区二区三区四区| 国产手机精品一区二区| 亚洲宅男精品一区在线观看| 精品国产乱码一区二区三区| 精品人妻中文av一区二区三区 | 国产一区二区在线观看| 海角国精产品一区一区三区糖心| 人妻视频一区二区三区免费| 无码中文人妻在线一区二区三区| 色窝窝免费一区二区三区| 国内精自品线一区91| 视频一区二区在线观看| 亚洲一区二区三区免费| 亚洲乱码一区二区三区在线观看| 久久人妻av一区二区软件| 中文字幕日韩一区二区三区不卡| 欲色aV无码一区二区人妻| 国产成人精品视频一区| 国产亚洲情侣一区二区无|