1

我试图将一个项目添加到WindowsXamlHost设置InitialTypeNameWindows.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’的定义。”

谁能帮我?

谢谢

4

1 回答 1

1

在尝试访问其属性之前,您应该将 的Child属性WindowsXamlHost转换为 a Windows.UI.Xaml.Controls.ListView(或任何设置为) :InitialTypeNameItems

var uwpListView = listView.Child as Windows.UI.Xaml.Controls.ListView;
uwpListView.Items.Add(...);

您可能还应该将其重命名为WindowsXamlHost比“listView”更好的名称。毕竟,它只是 UWP 控件的宿主

于 2020-04-01T09:43:22.457 回答