Commit 6404a122 by liulongfei

包装资源

parent 76fb25d9
using log4net;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using System.Windows.Media.Imaging;
namespace VIZ.Framework.Core
{
/// <summary>
/// 字符串转图片源转化器类型
/// </summary>
public enum String2ImageSourceConverterType
{
/// <summary>
/// 绝对路径
/// </summary>
Absolute,
/// <summary>
/// 相对路径
/// </summary>
Relative
}
/// <summary>
/// 字符串转图片源转化器
/// </summary>
public class String2ImageSourceConverter : IValueConverter
{
/// <summary>
/// 日志
/// </summary>
private readonly static ILog log = LogManager.GetLogger(typeof(String2ImageSourceConverter));
/// <summary>
/// 类型
/// </summary>
public String2ImageSourceConverterType Type { get; set; }
/// <summary>
/// 工作目录,相对于.exe的路径
/// </summary>
public string WorkPath { get; set; }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
try
{
if (value == null)
return null;
string path = value.ToString();
if (this.Type == String2ImageSourceConverterType.Relative)
{
path = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, this.WorkPath, path);
}
BitmapImage bmp = new BitmapImage(new Uri(path, UriKind.Absolute));
return bmp;
}
catch (Exception ex)
{
log.Error(ex);
return null;
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
...@@ -95,6 +95,7 @@ ...@@ -95,6 +95,7 @@
<Compile Include="Core\Converter\Bool2SolidColorBrushConverter.cs" /> <Compile Include="Core\Converter\Bool2SolidColorBrushConverter.cs" />
<Compile Include="Core\Converter\Color2SolidColorBrushConverter.cs" /> <Compile Include="Core\Converter\Color2SolidColorBrushConverter.cs" />
<Compile Include="Core\Converter\Enum2EnumDescriptionConverter.cs" /> <Compile Include="Core\Converter\Enum2EnumDescriptionConverter.cs" />
<Compile Include="Core\Converter\String2ImageSourceConverter.cs" />
<Compile Include="Core\Converter\StringAppendConverter.cs" /> <Compile Include="Core\Converter\StringAppendConverter.cs" />
<Compile Include="Core\Enum\EnumHelper.cs" /> <Compile Include="Core\Enum\EnumHelper.cs" />
<Compile Include="Core\Enum\EnumModel.cs" /> <Compile Include="Core\Enum\EnumModel.cs" />
......
<ResourceDictionary <ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:VIZ.Framework.TVP"> xmlns:local="clr-namespace:VIZ.Framework.TVP">
</ResourceDictionary> </ResourceDictionary>
...@@ -57,6 +57,10 @@ ...@@ -57,6 +57,10 @@
<Generator>MSBuild:Compile</Generator> <Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType> <SubType>Designer</SubType>
</Page> </Page>
<Page Include="Widgets\Resource\TvpResourcePicker.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs"> <Compile Include="Properties\AssemblyInfo.cs">
...@@ -72,6 +76,9 @@ ...@@ -72,6 +76,9 @@
<DependentUpon>Settings.settings</DependentUpon> <DependentUpon>Settings.settings</DependentUpon>
<DesignTimeSharedInput>True</DesignTimeSharedInput> <DesignTimeSharedInput>True</DesignTimeSharedInput>
</Compile> </Compile>
<Compile Include="Widgets\Resource\ITvpResource.cs" />
<Compile Include="Widgets\Resource\TvpResourcePicker.cs" />
<Compile Include="Widgets\Resource\TvpResourceType.cs" />
<EmbeddedResource Include="Properties\Resources.resx"> <EmbeddedResource Include="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator> <Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput> <LastGenOutput>Resources.Designer.cs</LastGenOutput>
......
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace VIZ.Framework.TVP
{
/// <summary>
/// 包装资源
/// </summary>
public interface ITvpResource
{
/// <summary>
/// 资源名称
/// </summary>
string ResourceName { get; }
/// <summary>
/// 资源类型
/// </summary>
TvpResourceType ResourceType { get; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace VIZ.Framework.TVP
{
/// <summary>
/// 包装资源选择器
/// </summary>
public class TvpResourcePicker : Button
{
static TvpResourcePicker()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(TvpResourcePicker), new FrameworkPropertyMetadata(typeof(TvpResourcePicker)));
}
#region ResourceName -- 资源名称
/// <summary>
/// 资源名称
/// </summary>
public string ResourceName
{
get { return (string)GetValue(ResourceNameProperty); }
set { SetValue(ResourceNameProperty, value); }
}
/// <summary>
/// Using a DependencyProperty as the backing store for ResourceName. This enables animation, styling, binding, etc...
/// </summary>
public static readonly DependencyProperty ResourceNameProperty =
DependencyProperty.Register("ResourceName", typeof(string), typeof(TvpResourcePicker), new PropertyMetadata(null));
#endregion
#region ResourceSource -- 资源源
/// <summary>
/// 资源源
/// </summary>
public string ResourceSource
{
get { return (string)GetValue(ResourceSourceProperty); }
set { SetValue(ResourceSourceProperty, value); }
}
/// <summary>
/// Using a DependencyProperty as the backing store for ResourceSource. This enables animation, styling, binding, etc...
/// </summary>
public static readonly DependencyProperty ResourceSourceProperty =
DependencyProperty.Register("ResourceSource", typeof(string), typeof(TvpResourcePicker), new PropertyMetadata(null));
#endregion
#region ResourceType -- 资源类型
/// <summary>
/// 资源类型
/// </summary>
public TvpResourceType ResourceType
{
get { return (TvpResourceType)GetValue(ResourceTypeProperty); }
set { SetValue(ResourceTypeProperty, value); }
}
/// <summary>
/// Using a DependencyProperty as the backing store for ResourceType. This enables animation, styling, binding, etc...
/// </summary>
public static readonly DependencyProperty ResourceTypeProperty =
DependencyProperty.Register("ResourceType", typeof(TvpResourceType), typeof(TvpResourcePicker), new PropertyMetadata(TvpResourceType.Image));
#endregion
}
}
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:VIZ.Framework.TVP">
<Style TargetType="local:TvpResourcePicker">
<Setter Property="FocusVisualStyle" Value="{x:Null}"></Setter>
<Setter Property="Background" Value="Transparent"></Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:TvpResourcePicker">
<Border Background="{TemplateBinding Background}">
<ContentPresenter Margin="{TemplateBinding Padding}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"></ContentPresenter>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
\ No newline at end of file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace VIZ.Framework.TVP
{
/// <summary>
/// 包装资源类型
/// </summary>
public enum TvpResourceType
{
/// <summary>
/// 图片
/// </summary>
Image
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment