Преглед на файлове

mvvm并非是view中不写一行代码。与逻辑无关,纯界面的代码需要放到view中处理比较好,
例如本次提交的,textbox滚动条自动向下滚动

tanghai преди 14 години
родител
ревизия
53f9efa98b

+ 1 - 1
CSharp/Editor/Shell.xaml

@@ -5,6 +5,6 @@
 	xmlns:prism="http://www.codeplex.com/prism"
 	Title="Editor" Height="600" Width="800" WindowStartupLocation="CenterScreen">
 	<Grid>
-		<ItemsControl Name="mainRegion" prism:RegionManager.RegionName="MainRegion"/>
+		<ItemsControl Name="mainRegion" prism:RegionManager.RegionName="MainRegion" />
 	</Grid>
 </Window>

+ 4 - 1
CSharp/Modules/LoginModule/Login.csproj

@@ -34,6 +34,9 @@
     <WarningLevel>4</WarningLevel>
   </PropertyGroup>
   <ItemGroup>
+    <Reference Include="AsyncCtpLibrary">
+      <HintPath>..\..\Packages\AsynCtp\AsyncCtpLibrary.dll</HintPath>
+    </Reference>
     <Reference Include="Microsoft.Practices.Prism">
       <HintPath>..\..\Packages\Prism.4.0.0.0\lib\NET40\Microsoft.Practices.Prism.dll</HintPath>
     </Reference>
@@ -47,7 +50,7 @@
       <HintPath>..\..\Packages\CommonServiceLocator.1.0\lib\NET35\Microsoft.Practices.ServiceLocation.dll</HintPath>
     </Reference>
     <Reference Include="NLog">
-      <HintPath>..\..\Packages\NLog.2.0.0.2000\lib\net40\NLog.dll</HintPath>
+      <HintPath>..\..\Packages\NLog.2.0.0.0\lib\NET40\NLog.dll</HintPath>
     </Reference>
     <Reference Include="System" />
     <Reference Include="System.ComponentModel.Composition" />

+ 18 - 7
CSharp/Modules/LoginModule/LoginView.xaml

@@ -1,11 +1,22 @@
 <UserControl x:Class="Module.Login.LoginView"
-			 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
-			 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
-			 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
-			 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
-			 mc:Ignorable="d" 
-			 d:DesignHeight="600" d:DesignWidth="800">
+		xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
+		xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+		xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
+		xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
+		mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="360">
 	<Grid>
-		<TextBox Width="50" Height="20"></TextBox>
+		<Grid.RowDefinitions>
+			<RowDefinition Height="*"/>
+			<RowDefinition Height="*"/>
+		</Grid.RowDefinitions>
+		<StackPanel Grid.Row="0"  Orientation="Horizontal">
+			<Label Content="用户名: " Height="25"></Label>
+			<TextBox Name="tbUsername"  Text="{Binding Username}"  Width="100" Height="25"></TextBox>
+			<Label Content="密码: " Height="25"></Label>
+			<PasswordBox Name="pbPassword"  Width="100" Height="25"></PasswordBox>
+			<Button Name="btnLogin"  Content="登录" Width="50" Height="25" Click="btnLogin_Click"></Button>
+		</StackPanel>
+		<TextBox Grid.Row="1" Height="100"  Name="tbLogInfo" Text="{Binding LogInfo}" 
+				VerticalScrollBarVisibility="Visible" TextChanged="tbLogInfo_TextChanged"></TextBox>
 	</Grid>
 </UserControl>

+ 15 - 0
CSharp/Modules/LoginModule/LoginView.xaml.cs

@@ -31,10 +31,25 @@ namespace Module.Login
 		[Import]
 		LoginViewModel ViewModel
 		{
+			get
+			{
+				return this.DataContext as LoginViewModel;
+			}
 			set
 			{
 				this.DataContext = value;
 			}
 		}
+
+		private void btnLogin_Click(object sender, RoutedEventArgs e)
+		{
+			this.ViewModel.Password = pbPassword.Password;
+			this.ViewModel.Login();
+		}
+
+		private void tbLogInfo_TextChanged(object sender, TextChangedEventArgs e)
+		{
+			tbLogInfo.ScrollToEnd();
+		}
 	}
 }

+ 108 - 5
CSharp/Modules/LoginModule/LoginViewModel.cs

@@ -1,18 +1,121 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.ComponentModel.Composition;
+using System.ComponentModel.Composition;
 using Microsoft.Practices.Prism.ViewModel;
+using Microsoft.Practices.Prism.Commands;
+using System.Threading.Tasks;
+using System.Net.Sockets;
+using System.Security.Cryptography;
+using System;
 
 namespace Module.Login
 {
+	enum RealmLoginState : byte
+	{
+		RealmAuthLoginChallenge = 0,
+		RealmAuthLonginProof = 1,
+		AuthReconnectChallenge = 2,
+		AuthReconnectProof = 3,
+		AuthLoginPermit = 4,
+		RealmList = 10,
+		Surver = 48,
+	}
+
 	[Export(typeof(LoginViewModel))]
 	[PartCreationPolicy(CreationPolicy.NonShared)]
 	public class LoginViewModel : NotificationObject
 	{
+		private string username = "";
+		private string password = "";
+		private string logInfo = "";
+
 		public LoginViewModel()
 		{
 		}
+
+		public string Username
+		{
+			get
+			{
+				return username;
+			}
+			set
+			{
+				if (username == value)
+				{
+					return;
+				}
+				username = value;
+				RaisePropertyChanged("Username");
+			}
+		}
+
+		public string Password
+		{
+			get
+			{
+				return password;
+			}
+			set
+			{
+				if (password == value)
+				{
+					return;
+				}
+				password = value;
+				RaisePropertyChanged("Password");
+			}
+		}
+
+		public string LogInfo
+		{
+			get
+			{
+				return logInfo;
+			}
+			set
+			{
+				if (logInfo == value)
+				{
+					return;
+				}
+				logInfo = value;
+				RaisePropertyChanged("LogInfo");
+			}
+		}
+
+		public async void Login()
+		{
+			using (TcpClient tcpClient = new TcpClient())
+			{
+				// 异步连接
+				await tcpClient.ConnectAsync("192.168.10.246", 19000);
+				var stream = tcpClient.GetStream();
+
+				// 验证通行证
+				await LoginPermit(stream);
+			}
+		}
+
+		public async Task LoginPermit(NetworkStream stream)
+		{
+			byte[] opcodeBuffer = new byte[1] { (byte)RealmLoginState.AuthLoginPermit };
+			await stream.WriteAsync(opcodeBuffer, 0, opcodeBuffer.Length);
+
+			string username = Username + new string(' ', 128 - Username.Length);
+			byte[] usernameBuffer = System.Text.Encoding.Default.GetBytes(username);
+			await stream.WriteAsync(usernameBuffer, 0, usernameBuffer.Length);
+
+			MD5 md5 = new MD5CryptoServiceProvider();
+			byte[] password = System.Text.Encoding.Default.GetBytes(Password);
+			byte[] passMD5Buffer = md5.ComputeHash(password);
+
+			string passMD5 = BitConverter.ToString(passMD5Buffer);
+			passMD5 = passMD5.Replace("-", "");
+			passMD5 = passMD5.ToLower();
+
+			passMD5Buffer = System.Text.Encoding.Default.GetBytes(passMD5);
+			await stream.WriteAsync(passMD5Buffer, 0, passMD5Buffer.Length);
+
+			LogInfo += "username: " + username.Trim() + " password md5: " + passMD5 + Environment.NewLine;
+		}
 	}
 }