Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用列表<;T>;在xaml内?_C#_Wpf_Xaml_Objectdataprovider - Fatal编程技术网

C# 如何使用列表<;T>;在xaml内?

C# 如何使用列表<;T>;在xaml内?,c#,wpf,xaml,objectdataprovider,C#,Wpf,Xaml,Objectdataprovider,因此,我非常确定,在定义部分,我需要包括以下内容: xmlns:s="clr-namespace:System.Collections.Generic;assembly=?????" 但我只是不知道该用什么来代替 我想对代码做的是: <UserControl.DataContext> <ObjectDataProvider MethodName="CreateNodes" ObjectType="{x:Type local:

因此,我非常确定,在定义部分,我需要包括以下内容:

xmlns:s="clr-namespace:System.Collections.Generic;assembly=?????" 
但我只是不知道该用什么来代替

我想对代码做的是:

<UserControl.DataContext>
    <ObjectDataProvider 
          MethodName="CreateNodes"
          ObjectType="{x:Type local:TreeViewModel}" >
        <ObjectDataProvider.MethodParameters>
            <s:List<T>>
                  {Binding Nodes}
            </s:List<T>>
        </ObjectDataProvider.MethodParameters>
    </ObjectDataProvider>
</UserControl.DataContext>

{绑定节点}
因此,当我调用objectDataProvider时,我可以将列表传递给它正在调用的方法(CreateNodes)

我该怎么做呢

谢谢

编辑-可能是修复方法?

我只是把它放在方法中,而不是传递到列表中,它只是一个应用程序变量…我不知道应用程序变量是否坏

  List<TNode> existingNodes;

  if (Application.Current.Properties.Contains("ExistingNodes")) existingNodes = Application.Current.Properties["ExistingNodes"] as List<TNode>;
  else existingNodes = new List<TNode>();
列出现有节点;
如果(Application.Current.Properties.Contains(“ExistingNodes”))ExistingNodes=Application.Current.Properties[“ExistingNodes”]作为列表;
else existingNodes=新列表();

XML命名空间声明的
程序集
部分将是
mscorlib

但无论如何,XAML不支持泛型(*),所以您不能这样做。相反,您可以创建一个继承
List
的类,并在XAML中使用它:

class ListOfFoo : List<Foo>
{
}
xmlns:s="clr-namespace:System.Collections.Generic;assembly=netstandard"
...
<View>
    <View.TestList>
        <s:List x:TypeArguments="x:String" >
           <x:String>abc</x:String>
           <x:String>123</x:String>
           <x:String>456</x:String>
        </s:List>
    </View.TestList>
</View>
...
class-listofoo:List
{
}

(1) 实际上,XAML2009中支持泛型,但编译的XAML中不支持大部分XAML2009。有关更多信息,请参阅。

在我的情况下,这是可以的。 如果您这样定义列表属性:

    internal static readonly BindableProperty TestListProperty = BindableProperty.Create("TestList", typeof(List<string>), typeof(View), null, propertyChanged: (bindable, oldValue, newValue) =>
    {
        var view = (View)bindable;
        List<string> v = (List<string>)newValue;
        view.testList = v;
    },
    defaultValueCreator: (bindable) =>
    {
        var view = (View)bindable;
        return view.testList;
    });
    private List<string> testList;
    public List<string> TestList
    {
        get
        {
            return (List<string>)GetValue(TestListProperty);
        }
        set
        {
            SetValue(TestListProperty, value);
        }
    }
内部静态只读BindableProperty TestListProperty=BindableProperty.Create(“TestList”、typeof(List)、typeof(View)、null、propertyChanged:(bindable、oldValue、newValue)=>
{
变量视图=(视图)可绑定;
List v=(List)newValue;
view.testList=v;
},
defaultValueCreator:(可绑定)=>
{
变量视图=(视图)可绑定;
返回view.testList;
});
私有列表测试列表;
公共列表测试列表
{
得到
{
返回(列表)GetValue(TestListProperty);
}
设置
{
SetValue(TestListProperty,值);
}
}
然后在XAML中使用它:

class ListOfFoo : List<Foo>
{
}
xmlns:s="clr-namespace:System.Collections.Generic;assembly=netstandard"
...
<View>
    <View.TestList>
        <s:List x:TypeArguments="x:String" >
           <x:String>abc</x:String>
           <x:String>123</x:String>
           <x:String>456</x:String>
        </s:List>
    </View.TestList>
</View>
...
xmlns:s=“clr命名空间:System.Collections.Generic;assembly=netstandard”
...
abc
123
456
...

我不认为XAML在这一点上支持泛型。相关的程序集是
mscorlib.dll
,但我将此作为注释,因为我从未在XAML中这样做过,我不想留下部分答案。我知道XAML不支持泛型。所以我不能做我想做的事(或者我必须使用应用程序变量来传递列表?XAML中不支持泛型。但是您可以创建一个使用适当泛型类型从列表继承的类,并在XAML中使用该类。@dowhilefor创建该类更好,还是像我在上面的编辑中那样使用应用程序变量更好?我在上面进行了编辑。您能告诉我吗告诉我是按照你建议的方式(创建一个类)还是按照我使用应用程序变量的方式做更好?:@toadams,这可能不是最干净的方式,但它应该可以工作,是的,在编写了这样一个类之后,它就可以了。(只是花了一个下午的时间试图理解为什么这不起作用。)问题被标记了。你的答案在这里是可行的,但在这里是完全错误的。