为什么 XamlIslands Image 控件不显示图像?
Why does the XamlIslands Image control not display the image?
我试图让用户选择一个文件,然后在 Xaml 岛屿 GridView
上显示该文件的图标。用户选择一个文件后(该文件的文件路径为myfile),该文件的图标将保存到C:\LauncherX\Temp\Icons。该图标保存良好,因为我可以打开它并查看它,但是,当我尝试在 XamlIsland Image
控件中显示它时,它只显示某些图像。
例如,这是我要显示的两个图标:
这个图标叫做GithubDesktop.png
和
这个图标叫做TextIcon.png
Xaml群岛Image
控件显示GithubDesktop.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>
你能帮帮我吗?
谢谢
好的,将扩展名设置为 .tiff
似乎有效。
我试图让用户选择一个文件,然后在 Xaml 岛屿 GridView
上显示该文件的图标。用户选择一个文件后(该文件的文件路径为myfile),该文件的图标将保存到C:\LauncherX\Temp\Icons。该图标保存良好,因为我可以打开它并查看它,但是,当我尝试在 XamlIsland Image
控件中显示它时,它只显示某些图像。
例如,这是我要显示的两个图标:
这个图标叫做GithubDesktop.png
和
这个图标叫做TextIcon.png
Xaml群岛Image
控件显示GithubDesktop.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>
你能帮帮我吗?
谢谢
好的,将扩展名设置为 .tiff
似乎有效。