处理资源
本文关键字:资源 处理 | 更新日期: 2023-09-27 18:07:01
我有以下XAML代码:
<Window xmlns:Controls="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Navigation"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:resx="clr-namespace:DM.Properties"
xmlns:ig="http://schemas.infragistics.com/xaml"
xmlns:igWindows="http://infragistics.com/Windows"
xmlns:igPrim="http://schemas.infragistics.com/xaml/primitives"
xmlns:UI="clr-namespace:DM.Properties"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
x:Class="DM.PageEightInThree"
WindowStartupLocation="CenterScreen"
FontFamily="B Mitra"
WindowStyle="None" ResizeMode="NoResize"
Title="Dummies Maker" Height="500" Width="1000" Background="#FF181818">
<Grid Margin="10">
<telerik:RadCarousel x:Name="carsoul" VerticalAlignment="Top" Background="Black" Height="422" />
<Button Width="173" FlowDirection="RightToLeft" VerticalAlignment="Bottom" HorizontalAlignment="Center" Click="Button_Click" TabIndex="0" Content="اضافه کردن آیکون جدید" Height="26" Margin="609,0,190,10" />
<Button Width="173" FlowDirection="RightToLeft" VerticalAlignment="Bottom" HorizontalAlignment="Center" Click="Button_Click_1" TabIndex="1" Content="حذف آیکون" Height="26" Margin="410,0,389,10" />
<Button Width="173" FlowDirection="RightToLeft" VerticalAlignment="Bottom" HorizontalAlignment="Center" Click="Button_Click_2" TabIndex="2" Content="بازگشت" Height="26" Margin="212,0,587,10" RenderTransformOrigin="-1.823,0.346" />
</Grid>
它背后的代码是:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
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.Shapes;
using System.Diagnostics;
using Telerik.Windows.Data;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.Navigation;
using DM.Properties;
namespace DM
{
/// <summary>
/// Interaction logic for PageEightInThree.xaml
/// </summary>
public partial class PageEightInThree : Window
{
internal static String pathOfIcons = @"Icons";
internal static String txtFile = "Location.txt";
public PageEightInThree()
{
InitializeComponent();
this.Init();
this.carsoul.ItemsSource = IconImagesForDummiesService.GetImages();
this.carsoul.Focus();
}
private void Init()
{
List<String> res = new List<string>(50);
String[] names = Directory.GetFiles(PageEightInThree.pathOfIcons, "*.*", SearchOption.AllDirectories);
var items = names.Where(a => !a.Contains(txtFile));
foreach (var inner in items)
{
res.Add(System.IO.Path.GetFileName(inner));
}
System.IO.File.WriteAllLines(pathOfIcons + @"/" + txtFile, res, Encoding.Unicode);
}
}
public class IconImagesForDummies
{
private String name;
private Image image;
public String Name
{
get
{
return this.name;
}
set
{
this.name = value;
}
}
public Image Image
{
get
{
return this.image;
}
set
{
this.image = value;
}
}
}
public class IconImagesForDummiesService
{
public static List<Image> GetImages()
{
String path = PageEightInThree.pathOfIcons + @"/" + PageEightInThree.txtFile;
String path2 = PageEightInThree.pathOfIcons;
List<Image> images = new List<Image>();
ObservableCollection<IconImagesForDummies> icons = new ObservableCollection<IconImagesForDummies>();
String[] lines = null;
if (File.Exists(path))
lines = File.ReadAllLines(path, Encoding.Unicode);
if (lines != null)
{
for (int i = 0; i < lines.Length; ++i)
{
String str = path2 + "/" + lines[i];
if (File.Exists(str))
{
Image im = new Image();
im.Source = new BitmapImage(new Uri(str, UriKind.RelativeOrAbsolute));
im.Height = 200;
im.Width = 200;
images.Add(im);
icons.Add(new IconImagesForDummies() { Name = str, Image = im });
}
}
}
else
throw new Exception("The Location.txt which stores the location of icons is missing");
if (icons.Count == 0)
throw new Exception("There is no icon image");
List<Image> res = new List<Image>();
for (int i = 0; i < icons.Count; ++i)
res.Add(icons[i].Image);
return res;
}
}
我使用一个名为Location
的文本文件,其中包含图标文件夹中所有图像的名称。我制作了一个图像列表,然后将其传递给WPF
控件以显示图像。有一个名为Icon的文件夹,它包含Build Action
设置为Copy Always
的图像。起初程序工作正常,图标文件夹中的所有图像都显示正常,但是重新运行程序两三次后,图像不显示,这是为什么?
经过一番努力,我发现Location应该有绝对值作为它的地址。我不知道原因,但我通过使用System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
实现了上述愿望。但是仍然不知道为什么Image类需要绝对地址。