为啥 XamlIslands Image 控件不显示图像?
Posted
技术标签:
【中文标题】为啥 XamlIslands Image 控件不显示图像?【英文标题】:Why does the XamlIslands Image control not display the image?为什么 XamlIslands Image 控件不显示图像? 【发布时间】:2020-07-24 01:41:12 【问题描述】:我试图让用户选择一个文件,然后在 XamlIslands GridView
上显示该文件的图标。用户选择一个文件后(文件的文件路径为myfile),该文件的图标随后保存到C:\LauncherX\Temp\Icons。该图标保存得很好,因为我可以打开它并查看它,但是,当我尝试在 XamlIsland Image
控件中显示它时,它只显示某些图像。
例如,这是我要显示的两个图标:
这个图标叫做Github Desktop.png
和
这个图标叫做TextIcon.png
Xaml IslandsImage
控件显示 Github Desktop.png 非常好:
但是,无论出于何种原因,它都不会显示 TextIcon.png
这是 C# 代码:
private void Button_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
//init a gridview
var gridView = gridviewhost.Child as Windows.UI.Xaml.Controls.GridView;
//init open file dialog
OpenFileDialog openFileDialog = new OpenFileDialog() DereferenceLinks = false ;
//check if openfile dialog is ok
if (openFileDialog.ShowDialog() == true)
//and then get the icon and put it in into the grid view
foreach (string myfile in openFileDialog.FileNames)
//init filename
var filename = System.IO.Path.GetFileName(myfile);
filename = filename.Remove(filename.Length - 4);
filename = filename + ".png";
//save file icon
if(File.Exists(Path.Combine("C:\\LauncherX\\Temp\\Icons", filename)))
filename = count.ToString() + filename;
FileStream stream = new FileStream(System.IO.Path.Combine("C:\\LauncherX\\Temp\\Icons\\", filename), FileMode.Create);
Bitmap icon1 = new Bitmap(System.Drawing.Icon.ExtractAssociatedIcon(myfile).ToBitmap());
icon1.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
count += 1;
else
FileStream stream = new FileStream(System.IO.Path.Combine("C:\\LauncherX\\Temp\\Icons\\", filename), FileMode.Create);
Bitmap icon1 = new Bitmap(System.Drawing.Icon.ExtractAssociatedIcon(myfile).ToBitmap());
icon1.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
//create a stackpanel
Windows.UI.Xaml.Controls.StackPanel stackpanel = new Windows.UI.Xaml.Controls.StackPanel();
stackpanel.Width = 85;
stackpanel.Height = 85;
//load file icon into uwp image control
Windows.UI.Xaml.Controls.Image image = new Windows.UI.Xaml.Controls.Image();
image.Width = 50;
image.Height = 50;
string path = Path.Combine(@"C:\LauncherX\Temp\Icons\" + filename);
Uri fileuri = new Uri(path);
image.Source = new Windows.UI.Xaml.Media.Imaging.BitmapImage(fileuri);
image.VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Top;
image.HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Center;
//create a textblock
//TODO: fix the text
Windows.UI.Xaml.Controls.TextBlock textblock = new Windows.UI.Xaml.Controls.TextBlock();
textblock.FontSize = 11;
textblock.TextWrapping = Windows.UI.Xaml.TextWrapping.Wrap;
textblock.Text = filename.Remove(filename.Length - 4);
textblock.HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Stretch;
textblock.VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Bottom;
textblock.TextAlignment = Windows.UI.Xaml.TextAlignment.Center;
//add the controls
stackpanel.Children.Add(image);
stackpanel.Children.Add(textblock);
gridView.Items.Add(stackpanel);
//TODO: Save the item using text documents
这里是 Xaml 代码:
<Grid>
<xamlHost:WindowsXamlHost x:Name="OpenFileHost" InitialTypeName="Windows.UI.Xaml.Controls.Button" Margin="0,10,10,0" HorizontalAlignment="Right" VerticalAlignment="Top" Height="32" RenderTransformOrigin="0.5,0.5" Width="105" ChildChanged="OpenFileHost_ChildChanged"/>
<TextBlock x:Name="textBlock" HorizontalAlignment="Left" Margin="10,10,0,0" TextWrapping="Wrap" Text="My applications" VerticalAlignment="Top" FontWeight="Bold" FontSize="16"/>
<xamlHost:WindowsXamlHost Margin="10,47,10,10.5" x:Name="gridviewhost" InitialTypeName="Windows.UI.Xaml.Controls.GridView"/>
</Grid>
你能帮帮我吗?
谢谢
【问题讨论】:
确保所有图标的扩展名是.png
并尝试注释掉这一行filename = filename + ".png";
然后检查。
@RaoHammasHussain 如果我从文件(如文本文件或快捷方式文件)中提取图标,如何确保所有图标的扩展名都是 .png?我该怎么做?
文件名应以 .png string.endswith() 结尾。或 path.getextension()。
@Andy 当用户选择一个文件(例如 Text.txt)时,文件名给了我“Text.txt”的值所以,我需要在末尾添加 .png 以便当我保存此文件的图标(通过 System.Drawing.Icon.ExtractAssosiatedIcon(myfile) 完成)时,它会将其保存为 .png 文件。查看代码了解更多详情。
但是你为什么要保存一个带有.png
扩展名的.txt
文件呢?这就是为什么在使用 path.getextension()
或只是 Yourstring.endsWith()
或我猜是 Yourstring.Contains()
之前检查扩展名
【参考方案1】:
好的,结果将扩展名设置为.tiff
似乎有效。
【讨论】:
以上是关于为啥 XamlIslands Image 控件不显示图像?的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 XamlIslands 和桌面桥打包 WPF (.NET Core) 应用程序?
为啥电脑好多应用程序(firefox、delphi)背景全都跟窗口背景一样了,个别字体变大,关闭窗口的x也不显
用JAVA的表读取数据库的内容时,where语句后的条件例如sname(数据库表的列)='"+f1.getText()+"'为啥不显