个人资料

跳过导航链接首页 > 博客列表 > 博客正文

利用partial关键字写出更优雅的c#代码

分类:

使用c#中的partial将事件,方法,属性,继承等分离,比region更方便

示例:


	// 事件
	public sealed partial class MainPage : Page
	{
		public MainPage()
		{
			this.InitializeComponent();					
             
                 }

		protected async override void OnNavigatedTo(NavigationEventArgs e)
		{
			Debug.WriteLine("OnNavigatedTo");
			if (myfiles == null)
			{
				folder = KnownFolders.SavedPictures;
				myfiles = await folder.GetFilesAsync();
			}
			await ReadImageFiles(myfiles);
		}

		private void SplitViewToggle_Click(object sender, RoutedEventArgs e)
		{
			...
		}

		private async void SelectFolderButton_Click(object sender, RoutedEventArgs e)
		{
			...
		}

		private void GridViewItem_Click(object sender, ItemClickEventArgs e)
		{
                     ...
		}
	}

	// 属性
	public sealed partial class MainPage :  INotifyPropertyChanged
	{
		private bool _IsBusy = false;
		public bool IsBusy
		{
			get { return _IsBusy; }
			set { SetProperty(ref _IsBusy, value); }
		}
		// --------
		public static ObservableCollection<ImageBrushAndFileInfo> _ImageObjects = new ObservableCollection<ImageBrushAndFileInfo>();
		public ObservableCollection<ImageBrushAndFileInfo> ImageObjects
		{
			get { return _ImageObjects; }
			set { SetProperty(ref _ImageObjects, value); }
		}


		private double _DesiredColumnWidth = 250;
		public double DesiredColumnWidth
		{
			get { return _DesiredColumnWidth; }
			set { SetProperty(ref _DesiredColumnWidth, value); }
		}

		// --------
		//实现INotify接口
		public event PropertyChangedEventHandler PropertyChanged;
		private bool SetProperty<T>(ref T storage, T value, [CallerMemberName] String propertyName = null)
		{
			if (object.Equals(storage, value)) return false;
			storage = value;
			this.OnPropertyChanged(propertyName);
			return true;
		}
		private void OnPropertyChanged([CallerMemberName] string propertyName = "")
		{
			this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
		}

		//

		public static StorageFolder folder;
		public static IReadOnlyList<StorageFile> myfiles;
	}

	// 方法
	public sealed partial class MainPage
	{

		void BeautyTitle()
		{
			...
		}

		private async Task ReadImageFiles(IReadOnlyList<StorageFile> myfiles)
		{
                      ...
               }
	}

	// 辅助类
	public class ImageBrushAndFileInfo : INotifyPropertyChanged
	{

		private ImageBrush _ImageBrush;
		public ImageBrush ImageBrush
		{
			get { return _ImageBrush; }
			set { SetProperty(ref _ImageBrush, value); }
		}


		private StorageFile _file;
		public StorageFile file
		{
			get { return _file; }
			set { SetProperty(ref _file, value); }
		}

		// --------
		private double _ImageSize = 400;
		public double ImageSize
		{
			get { return _ImageSize; }
			set { SetProperty(ref _ImageSize, value); }
		}
		// --------
		//实现INotify接口
		public event PropertyChangedEventHandler PropertyChanged;
		private bool SetProperty<T>(ref T storage, T value, [CallerMemberName] String propertyName = null)
		{
			if (object.Equals(storage, value)) return false;
			storage = value;
			this.OnPropertyChanged(propertyName);
			return true;
		}
		private void OnPropertyChanged([CallerMemberName] string propertyName = "")
		{
			this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
		}
	}
	// 辅助类
	public class MyConverters : IValueConverter
	{
		public async static Task<ImageBrush> ConvertFile2ImgBrush(IStorageFile file)
		{
			BitmapImage bi = new BitmapImage();
			ImageBrush SkinImageBrush = new ImageBrush();
			try
			{
				
				using (IRandomAccessStream ir = await file.OpenAsync(FileAccessMode.Read))
				{
					await bi.SetSourceAsync(ir);
					SkinImageBrush.ImageSource = bi;
				}
			}
			catch (Exception ex)
			{
				Debug.WriteLine(ex.Message);
				return null;
			}

			return SkinImageBrush;
		}
	}


admin
最初发表2019/10/17 11:00:05 最近更新2019/10/17 11:00:39 1224
为此篇作品打分
10
   评论