Просмотр исходного кода

mvvm异步处理逻辑,处理完成后调度到ui线程设置ui

tanghai 14 лет назад
Родитель
Сommit
c7f03c2d10

+ 1 - 0
CSharp/Editor/App.xaml.cs

@@ -1,5 +1,6 @@
 using System.Windows;
 using GalaSoft.MvvmLight.Threading;
+using System.ComponentModel;
 
 namespace Editor
 {

+ 0 - 5
CSharp/Editor/Editor.csproj

@@ -70,7 +70,6 @@
       <Generator>MSBuild:Compile</Generator>
       <SubType>Designer</SubType>
     </ApplicationDefinition>
-    <Compile Include="Model\DataItem.cs" />
     <Compile Include="Model\DataService.cs" />
     <Compile Include="Model\IDataService.cs" />
     <Compile Include="ViewModel\MainViewModel.cs" />
@@ -90,10 +89,6 @@
       <DependentUpon>MainWindow.xaml</DependentUpon>
       <SubType>Code</SubType>
     </Compile>
-    <Page Include="Skins\MainSkin.xaml">
-      <Generator>MSBuild:Compile</Generator>
-      <SubType>Designer</SubType>
-    </Page>
     <Page Include="View\MainView.xaml">
       <SubType>Designer</SubType>
       <Generator>MSBuild:Compile</Generator>

+ 0 - 9
CSharp/Editor/MainWindow.xaml

@@ -6,15 +6,6 @@
 		xmlns:v="clr-namespace:Editor.View"
 		mc:Ignorable="d" Height="600" Width="800" 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">
 		<TabControl HorizontalAlignment="Stretch" VerticalAlignment="Stretch" >
 			<TabItem Header="Main" Name="mainTabItem">

+ 0 - 21
CSharp/Editor/Model/DataItem.cs

@@ -1,21 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-
-namespace Editor.Model
-{
-	public class DataItem
-	{
-		public DataItem(string title)
-		{
-			Title = title;
-		}
-
-		public string Title
-		{
-			get;
-			private set;
-		}
-	}
-}

+ 0 - 7
CSharp/Editor/Model/DataService.cs

@@ -4,12 +4,5 @@ namespace Editor.Model
 {
 	public class DataService : IDataService
 	{
-		public void GetData(Action<DataItem, Exception> callback)
-		{
-			// Use this to connect to the actual data service
-
-			var item = new DataItem("Welcome to MVVM Light");
-			callback(item, null);
-		}
 	}
 }

+ 0 - 1
CSharp/Editor/Model/IDataService.cs

@@ -7,6 +7,5 @@ namespace Editor.Model
 {
 	public interface IDataService
 	{
-		void GetData(Action<DataItem, Exception> callback);
 	}
 }

+ 0 - 4
CSharp/Editor/Skins/MainSkin.xaml

@@ -1,4 +0,0 @@
-<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
-					xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
-	
-</ResourceDictionary>

+ 4 - 1
CSharp/Editor/View/MainView.xaml

@@ -5,5 +5,8 @@
 		xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
 		mc:Ignorable="d" d:DesignHeight="600" d:DesignWidth="800"
 		DataContext="{Binding Main, Source={StaticResource Locator}}" >
-	<Grid></Grid>
+	<StackPanel>
+		<Label HorizontalAlignment="Center" VerticalAlignment="Center" Content="{Binding LoginResult}" />
+		<Button Content="登录" HorizontalAlignment="Center" VerticalAlignment="Center" Command="{Binding LoginCmd}" />
+	</StackPanel>
 </UserControl>

+ 37 - 38
CSharp/Editor/ViewModel/MainViewModel.cs

@@ -1,64 +1,63 @@
-using GalaSoft.MvvmLight;
+using System;
+using System.Windows.Threading;
 using Editor.Model;
+using GalaSoft.MvvmLight;
+using GalaSoft.MvvmLight.Command;
+using GalaSoft.MvvmLight.Threading;
 
 namespace Editor.ViewModel
 {
-	/// <summary>
-	/// This class contains properties that the main View can data bind to.
-	/// <para>
-	/// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
-	/// </para>
-	/// <para>
-	/// See http://www.galasoft.ch/mvvm/getstarted
-	/// </para>
-	/// </summary>
 	public class MainViewModel : ViewModelBase
 	{
 		private readonly IDataService dataService;
+		private string loginResult = "";
 
-		/// <summary>
-		/// The <see cref="WelcomeTitle" /> property's name.
-		/// </summary>
-		public const string WelcomeTitlePropertyName = "WelcomeTitle";
-
-		private string welcomeTitle = string.Empty;
+		public MainViewModel(IDataService dataService)
+		{
+			this.dataService = dataService;
+			LoginCmd = new RelayCommand(AsyncLogin);
+		}
 
-		/// <summary>
-		/// Gets the WelcomeTitle property.
-		/// Changes to that property's value raise the PropertyChanged event. 
-		/// </summary>
-		public string WelcomeTitle
+		public string LoginResult
 		{
 			get
 			{
-				return welcomeTitle;
+				return loginResult;
 			}
 			set
 			{
-				if (welcomeTitle == value)
+				if (loginResult == value)
 				{
 					return;
 				}
-				welcomeTitle = value;
-				RaisePropertyChanged(WelcomeTitlePropertyName);
+				loginResult = value;
+				RaisePropertyChanged("LoginResult");
 			}
 		}
 
-		/// <summary>
-		/// Initializes a new instance of the MainViewModel class.
-		/// </summary>
-		public MainViewModel(IDataService dataService)
+		public RelayCommand LoginCmd
 		{
-			this.dataService = dataService;
-			dataService.GetData((item, error) =>
+			get;
+			private set;
+		}
+
+		private void AsyncLogin()
+		{
+			Action showLoginResult = () =>
+				{
+					LoginResult = "Login OK!";
+				};
+			AsyncCallback callback = (obj) =>
+				{
+					DispatcherHelper.UIDispatcher.BeginInvoke(
+						DispatcherPriority.Normal, showLoginResult);
+				};
+
+			Action asynLogin = () =>
 				{
-					if (error != null)
-					{
-						// Report error here
-						return;
-					}
-					WelcomeTitle = item.Title;
-				});
+					
+				};
+			asynLogin.BeginInvoke(callback, null);
 		}
 
 		public override void Cleanup()

+ 1 - 29
CSharp/Editor/ViewModel/ViewModelLocator.cs

@@ -1,32 +1,10 @@
-/*
-  In App.xaml:
-  <Application.Resources>
-	  <vm:ViewModelLocatorTemplate xmlns:vm="clr-namespace:Editor.ViewModel"
-								   x:Key="Locator" />
-  </Application.Resources>
-  
-  In the View:
-  DataContext="{Binding Source={StaticResource Locator}, Path=ViewModelName}"
-*/
-
-using GalaSoft.MvvmLight;
+using GalaSoft.MvvmLight;
 using GalaSoft.MvvmLight.Ioc;
 using Microsoft.Practices.ServiceLocation;
 using Editor.Model;
 
 namespace Editor.ViewModel
 {
-	/// <summary>
-	/// This class contains static references to all the view models in the
-	/// application and provides an entry point for the bindings.
-	/// <para>
-	/// Use the <strong>mvvmlocatorproperty</strong> snippet to add ViewModels
-	/// to this locator.
-	/// </para>
-	/// <para>
-	/// See http://www.galasoft.ch/mvvm/getstarted
-	/// </para>
-	/// </summary>
 	public class ViewModelLocator
 	{
 		static ViewModelLocator()
@@ -38,9 +16,6 @@ namespace Editor.ViewModel
 			SimpleIoc.Default.Register<MainViewModel>();
 		}
 
-		/// <summary>
-		/// Gets the Main property.
-		/// </summary>
 		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
 			"CA1822:MarkMembersAsStatic",
 			Justification = "This non-static member is needed for data binding purposes.")]
@@ -52,9 +27,6 @@ namespace Editor.ViewModel
 			}
 		}
 
-		/// <summary>
-		/// Cleans up all the resources.
-		/// </summary>
 		public static void Cleanup()
 		{
 		}