个人资料

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

UWP常用Converter收藏

分类:

Converters

***bool转visible枚举

public class VisibilityConverter : IValueConverter
	{

		object IValueConverter.Convert(object value, Type targetType, object parameter, string language)
		{
			if ((bool)value)
			{
				//return "0xEA8C"; &#x;              
				return Visibility.Visible;
			}

			else
			{
				return Visibility.Collapsed;
			}
		}

		object IValueConverter.ConvertBack(object value, Type targetType, object parameter, string language)
		{
			var s = (Visibility)value;
			bool result = false;
			switch (s)
			{
				case Visibility.Visible:
					result=true;
					break;
				case Visibility.Collapsed:
					result = false;
					break;
				default:
					break;
			}
			return result;
		}
	}


*** 0,1整数转换bool


	public class Int2BoolConverter:IValueConverter
	{
		public object Convert(object value, Type targetType, object parameter, string language)
		{

			//return true;

			var dv = (int)value;

			return (dv==0) ? false : true;
		}

		public object ConvertBack(object value, Type targetType, object parameter, string language)
		{
		
			if ((bool)value)
			{
				return 1;
			}
			else
			{
				return 0;
			}

		}
	}


***图片文件转imagebrush


	public class File2ImageBrushConverter:IValueConverter
	{
		public  object Convert(object value, Type targetType, object parameter, string language)
		{
			var SkinImageBrush = new ImageBrush();
			StorageFile file = value as StorageFile;

			using (IRandomAccessStream ir = file.OpenAsync(FileAccessMode.Read).GetResults())
			{
				BitmapImage bi = new BitmapImage();
				bi.SetSourceAsync(ir).GetResults();
				SkinImageBrush.ImageSource = bi;
				return SkinImageBrush;
			}
		}

		public object ConvertBack(object value, Type targetType, object parameter, string language)
		{
			throw new NotImplementedException();
		}

	}


***DateTime转字符转

    public class DateTimeConverter : IValueConverter
    {
      

        public object Convert(object value, Type targetType, object parameter, string language)
        {
            var time = (DateTime)value;
            return time.ToString("yyyy年MM月dd日");
           
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            return null;
        }
    }



*** bool/主题明暗 转换

	public class AppThemeConverter : IValueConverter
	{

		object IValueConverter.Convert(object value, Type targetType, object parameter, string language)
		{
			if ((bool)value)
			{             
				return ApplicationTheme.Dark;
			}
			else
			{
				return ApplicationTheme.Light;
			}
		}

		object IValueConverter.ConvertBack(object value, Type targetType, object parameter, string language)
		{
			bool result = false;
			switch (value)
			{
				case ApplicationTheme.Dark:
					result = true;
					break;
				case ApplicationTheme.Light:
					result = false;

					break;
			}

			return result;
		}
	}

	public class ElementThemeConverter : IValueConverter
	{

		object IValueConverter.Convert(object value, Type targetType, object parameter, string language)
		{
			if ((bool)value)
			{
				return ElementTheme.Dark;
				
			}
			else
			{
				return ElementTheme.Light;
			}
		}

		object IValueConverter.ConvertBack(object value, Type targetType, object parameter, string language)
		{
			bool result = false;
			switch (value)
			{
				case ElementTheme.Dark:
					result = true;
					break;
				case ElementTheme.Light:
					result = false;

					break;
			}

			return result;
		}
	}

***颜色Color/画刷Brush

	public class ColorBrushConverter : IValueConverter
	{

		object IValueConverter.Convert(object value, Type targetType, object parameter, string language)
		{
			return new SolidColorBrush((Windows.UI.Color)value);

		}

		object IValueConverter.ConvertBack(object value, Type targetType, object parameter, string language)
		{
			SolidColorBrush scb = value as SolidColorBrush;
			return scb.Color;
		}
	}

***

admin
最初发表2019/4/11 6:01:08 最近更新2019/4/11 6:14:21 1722
为此篇作品打分
10
   评论