【问题标题】:'ListViewItem' does not contain a contructor that takes 1 argument, Windows phone c#'ListViewItem' 不包含带 1 个参数的构造函数,Windows phone c#
【发布时间】:2016-04-19 12:44:05
【问题描述】:

您好,我正在尝试将项目添加到列表视图,但出现以下错误,“ListViewItem”不包含采用 1 个参数的构造函数。 我在这里收到错误 new ListViewItem(row); 我在 c# 中使用 Windows phone 8.1。

protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        int totalq = int.Parse(textBox.Text) + app.vanilla;
        textBox.Text = totalq.ToString();
        double totalvanilla = Convert.ToDouble(totalq)* 1.50;
        textBox_Copy.Text = totalvanilla.ToString();
        //listBox.Items.Add(totalq.ToString() + "Vanilla");

        string[] row = { totalq.ToString(),"vanilla" };
        var listViewItem = new ListViewItem(row); 

        listView.Items.Add(listViewItem);


    }

【问题讨论】:

  • 请在您想使用列表视图数据的地方添加您的列表视图 xaml 代码

标签: c# listview windows-phone


【解决方案1】:

我认为你是基于 xaml 的 System.Windows.Controls.ListViewItemSystem.Windows.Forms.ListViewItem(它有一个用于获取字符串数组的构造函数)。

你会以另一种方式使用后者:

XAML:

<ListView x:Name="listView">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Path=Name}"/>
            <GridViewColumn Header="Qunatity" DisplayMemberBinding="{Binding Path=Quantity}"/>
        </GridView>
    </ListView.View>
</ListView>

代码:

int totalq = 23;
var row = new { Quantity = totalq, Name = "vanilla" };

listView.Items.Add(row);

编辑:

如果 GridView 不适合您,并且您希望/必须自己将所有内容放置到位,您可以使用以下方法(基于 this answer):

<ListView x:Name="listView">
    <ListView.ItemTemplate>
        <DataTemplate>
            <Grid VerticalAlignment="Center">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="50" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>

                <TextBlock Grid.Column="0" Text="{Binding Quantity}" />
                <TextBlock Grid.Column="1" Text="{Binding Name}" />
            </Grid>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

【讨论】:

  • Gridview 列在 windows phone 8.1 中不存在。当我使用这个方法时它会显示一个错误。
【解决方案2】:

您正在将字符串作为参数发送给 listviewtem。ListViewItem 具有 content 属性,

这样做

listView.Items.Add(new ListViewItem{Content = whateveryourstring});

【讨论】:

  • 它给了我system.string,知道为什么吗?
  • 当我运行程序时。
【解决方案3】:

我认为您需要添加一个具有要在视图中显示的属性的类。 像这样。

public class ListModel {
    public string TotalQuantity { set; get; }
    public string Name { set; get; }
}

然后试试这个 XAML

xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"

<toolkit:ListView Name="listView">
    <toolkit:ListView.ItemTemplate>
       <DataTemplate>
           <Grid>
               <Grid.ColumnDefinitions>
                   <ColumnDefinition Width="100" />
                   <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>                       
                <TextBlock  Text="{Binding TotalQuantity}" />
                <TextBlock Grid.Column="1" Text="{Binding Name}" />
           </Grid>
       </DataTemplate>
   </toolkit:ListView.ItemTemplate>
</toolkit:ListView>

最后为列表视图添加itemsource

int totalq = int.Parse("3") + 2;
textBox.Text = totalq.ToString();
double totalvanilla = Convert.ToDouble(totalq) * 1.50;
textBox_Copy.Text = totalvanilla.ToString();
listBox.Items.Add(totalq.ToString() + "Vanilla");

ListViewItems = new ObservableCollection<ListModel>();
ListViewItems.Add(new ListModel()
{
    TotalQuantity = totalq.ToString(),
     Name = "vanilla"
});
listView.ItemsSource = ListViewItems;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 2013-02-17
    • 2012-08-21
    • 2012-08-13
    • 1970-01-01
    相关资源
    最近更新 更多