我试图让用户选择一个文件,然后在 XamlIslands 上显示文件的图标GridView
。用户选择文件后(文件的文件路径为myfile),文件的图标随后保存到 C:\LauncherX\Temp\Icons。该图标保存得很好,因为我可以打开它并查看它,但是,当我尝试在 XamlIslandImage
控件中显示它时,它只显示某些图像。例如,这是我要显示的两个图标:
这个图标叫做Github Desktop.png
和
这个图标叫做TextIcon.png
Xaml Islands Image
Control完美地显示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>
你能帮助我吗?
谢谢