我试图将项目添加到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不包含'Items'的定义。”
谁能帮我?
谢谢
在尝试访问其属性之前,应将的Child
属性WindowsXamlHost
转换为Windows.UI.Xaml.Controls.ListView
(或InitialTypeName
设置为)Items
。
var uwpListView = listView.Child as Windows.UI.Xaml.Controls.ListView;
uwpListView.Items.Add(...);
您可能还应该将其重命名为WindowsXamlHost
比“ listView”更好的名称。毕竟,它只是UWP控件的宿主。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句