如何在 WindowsXamlHost ListView 中添加项目?
How do I add an item in a WindowsXamlHost ListView?
我试图将一个项目添加到 WindowsXamlHost
,其 InitialTypeName
设置为 Windows.UI.Xaml.Controls.ListView
。我正在尝试将项目添加到 WindowsXamlHost 列表视图。
这是我的代码:
List<string> listFiles = new List<string>();
OpenFileDialog openFileDialog = new OpenFileDialog() { DereferenceLinks = false };
private void AddItem_Click(object sender, RoutedEventArgs e)
{
if (openFileDialog.ShowDialog() == true)
{
foreach(String myfile in openFileDialog.FileNames)
{
//get filename
string filename = System.IO.Path.GetFileName(myfile);
if (Path.GetExtension(myfile) == ".lnk")
{
try
{
var iconmyfile = GetShortcutTargetFile(myfile);
Icon icon1 = System.Drawing.Icon.ExtractAssociatedIcon(iconmyfile);
Bitmap icon = icon1.ToBitmap();
StackPanel sp = new StackPanel();
listView.Items.Add(sp);
sp.Height = 35;
sp.Width = listView.Width;
sp.Orientation = Orientation.Horizontal;
sp.VerticalAlignment = VerticalAlignment.Center;
System.Windows.Controls.Image image = new System.Windows.Controls.Image();
image.Source = BitmapToImageSource(icon);
sp.Children.Add(image);
TextBlock label = new TextBlock();
label.VerticalAlignment = VerticalAlignment.Center;
label.Text = " " + filename.Remove(filename.Length - 4);
label.FontSize = 17;
sp.Children.Add(label);
}
catch
{
MessageBox.Show("It seems that the shortcut file that you have chosen has no target location. Please select another file.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
else
{
Icon icon1 = System.Drawing.Icon.ExtractAssociatedIcon(myfile);
Bitmap icon = icon1.ToBitmap();
StackPanel sp = new StackPanel();
listView.Items.Add(sp);
sp.Height = 35;
sp.Width = listView.Width;
sp.Orientation = Orientation.Horizontal;
sp.VerticalAlignment = VerticalAlignment.Center;
System.Windows.Controls.Image image = new System.Windows.Controls.Image();
image.Source = BitmapToImageSource(icon);
sp.Children.Add(image);
TextBlock label = new TextBlock();
label.VerticalAlignment = VerticalAlignment.Center;
label.Text = " " + filename.Remove(filename.Length - 4);
label.FontSize = 17;
sp.Children.Add(label);
}
//TODO: write text documents to directory and get the file name
}
}
}
此代码适用于常规 WPF ListView。但是,当我尝试将此代码与来自 XAML Islands 的 WindowsXamlHost ListView 一起使用时,它说 "WindowsXamlHost does not contain a definition for 'Items'."
谁能帮帮我?
谢谢
在尝试之前,您应该将 WindowsXamlHost
的 Child
属性 转换为 Windows.UI.Xaml.Controls.ListView
(或 InitialTypeName
设置的任何值)访问其 Items
属性:
var uwpListView = listView.Child as Windows.UI.Xaml.Controls.ListView;
uwpListView.Items.Add(...);
您或许还应该将 WindowsXamlHost
重命名为比 "listView" 更好的名称。毕竟,它只是一个 host 用于 UWP 控件。
我试图将一个项目添加到 WindowsXamlHost
,其 InitialTypeName
设置为 Windows.UI.Xaml.Controls.ListView
。我正在尝试将项目添加到 WindowsXamlHost 列表视图。
这是我的代码:
List<string> listFiles = new List<string>();
OpenFileDialog openFileDialog = new OpenFileDialog() { DereferenceLinks = false };
private void AddItem_Click(object sender, RoutedEventArgs e)
{
if (openFileDialog.ShowDialog() == true)
{
foreach(String myfile in openFileDialog.FileNames)
{
//get filename
string filename = System.IO.Path.GetFileName(myfile);
if (Path.GetExtension(myfile) == ".lnk")
{
try
{
var iconmyfile = GetShortcutTargetFile(myfile);
Icon icon1 = System.Drawing.Icon.ExtractAssociatedIcon(iconmyfile);
Bitmap icon = icon1.ToBitmap();
StackPanel sp = new StackPanel();
listView.Items.Add(sp);
sp.Height = 35;
sp.Width = listView.Width;
sp.Orientation = Orientation.Horizontal;
sp.VerticalAlignment = VerticalAlignment.Center;
System.Windows.Controls.Image image = new System.Windows.Controls.Image();
image.Source = BitmapToImageSource(icon);
sp.Children.Add(image);
TextBlock label = new TextBlock();
label.VerticalAlignment = VerticalAlignment.Center;
label.Text = " " + filename.Remove(filename.Length - 4);
label.FontSize = 17;
sp.Children.Add(label);
}
catch
{
MessageBox.Show("It seems that the shortcut file that you have chosen has no target location. Please select another file.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
else
{
Icon icon1 = System.Drawing.Icon.ExtractAssociatedIcon(myfile);
Bitmap icon = icon1.ToBitmap();
StackPanel sp = new StackPanel();
listView.Items.Add(sp);
sp.Height = 35;
sp.Width = listView.Width;
sp.Orientation = Orientation.Horizontal;
sp.VerticalAlignment = VerticalAlignment.Center;
System.Windows.Controls.Image image = new System.Windows.Controls.Image();
image.Source = BitmapToImageSource(icon);
sp.Children.Add(image);
TextBlock label = new TextBlock();
label.VerticalAlignment = VerticalAlignment.Center;
label.Text = " " + filename.Remove(filename.Length - 4);
label.FontSize = 17;
sp.Children.Add(label);
}
//TODO: write text documents to directory and get the file name
}
}
}
此代码适用于常规 WPF ListView。但是,当我尝试将此代码与来自 XAML Islands 的 WindowsXamlHost ListView 一起使用时,它说 "WindowsXamlHost does not contain a definition for 'Items'."
谁能帮帮我?
谢谢
在尝试之前,您应该将 WindowsXamlHost
的 Child
属性 转换为 Windows.UI.Xaml.Controls.ListView
(或 InitialTypeName
设置的任何值)访问其 Items
属性:
var uwpListView = listView.Child as Windows.UI.Xaml.Controls.ListView;
uwpListView.Items.Add(...);
您或许还应该将 WindowsXamlHost
重命名为比 "listView" 更好的名称。毕竟,它只是一个 host 用于 UWP 控件。