如何使用List在 xaml 中?

发布于 2024-12-01 00:07:38 字数 1088 浏览 7 评论 0原文

所以我很确定在定义部分我需要包含以下内容:

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>();

So I am pretty sure that up in the definition part I need to include something along the lines of:

xmlns:s="clr-namespace:System.Collections.Generic;assembly=?????" 

but I just do not know what to put in place of the ???'s.

What I want to do with the code is this:

<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>

So that when I make the objectDataProvider call, I can pass the list in to the method that it is calling (CreateNodes)...

How do I go about doing this?

thanks!

Edit - could be a fix?

I just put this in the method, instead of passing in the list, it is just an app variable...I dont know if app variables are bad though

  List<TNode> existingNodes;

  if (Application.Current.Properties.Contains("ExistingNodes")) existingNodes = Application.Current.Properties["ExistingNodes"] as List<TNode>;
  else existingNodes = new List<TNode>();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

紧拥背影 2024-12-08 00:07:38

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

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

class ListOfFoo : List<Foo>
{
}

(1) 实际上,XAML 2009 中支持泛型,但大多数已编译的 XAML 不支持 XAML 2009。有关详细信息,请参阅此问题

The assembly part of the XML namespace declaration would be mscorlib.

But anyway, XAML doesn't support generics (*), so you can't do it. Instead, you could create a class that inherits List<T> and use it in XAML:

class ListOfFoo : List<Foo>
{
}

(1) Actually generics are supported in XAML 2009, but most of XAML 2009 is not supported in compiled XAML. See this question for more information.

瀞厅☆埖开 2024-12-08 00:07:38

就我而言,这没问题。
如果您像这样定义 List 属性:

    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);
        }
    }

那么您可以在 XAML 中使用它:

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>
...

In my case, this is ok.
If you define List property like this:

    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);
        }
    }

Then you use it in XAML:

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>
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文