如何向网格WP8.1 MVVM灯添加按钮?

如何向网格WP8.1 MVVM灯添加按钮?,mvvm,windows-phone-8.1,Mvvm,Windows Phone 8.1,我在WP8.1中构建仪表板时遇到问题。 我正在使用MVVM Light工具包 我正在关注这一结果: 我目前可以构建仪表板,但每行只能使用一个对象 我搜索了很多,但没有找到想要的效果 以下是按编程方式完成的仪表板代码: int i = 0; Grid grid = new Grid(); grid.RowDefinitions.Add(new RowDefinition()); for (int ct = 0; ct < 3; ct++) grid.ColumnDefinitio

我在WP8.1中构建仪表板时遇到问题。 我正在使用MVVM Light工具包

我正在关注这一结果:

我目前可以构建仪表板,但每行只能使用一个对象

我搜索了很多,但没有找到想要的效果

以下是按编程方式完成的仪表板代码:

int i = 0;

Grid grid = new Grid();
grid.RowDefinitions.Add(new RowDefinition());
for (int ct = 0; ct < 3; ct++)
    grid.ColumnDefinitions.Add(new ColumnDefinition());
StackPanel stackPanel = new StackPanel();
stackPanel.Name = "stackPanel";
stackPanel.Background = new SolidColorBrush(new Windows.UI.Color());

foreach (var flow in dashboard.DashboardFlows)
{
    if (flow.Flow.IsInstalled)
    {
        //if next object should change line
        if (i == 3)
        {
            stackPanel.Children.Add(grid);

            grid = new Grid();
            grid.RowDefinitions.Add(new RowDefinition());
            for (int ct = 0; ct < 3; ct++)
                grid.ColumnDefinitions.Add(new ColumnDefinition());

            i = 0;
        }

        Button button = new Button();
        if (flow.Flow.Type.Equals("COMPNEWS"))
        {
            button.Click += delegate { Frame.Navigate(typeof(NewsPage), flow); };
        }

        //button stackpanel
        StackPanel stack = new StackPanel();

        //Button image
        Image buttonImage = new Image();
        if (!String.IsNullOrEmpty(flow.Flow.IconUrl) || !String.IsNullOrWhiteSpace(flow.Flow.IconUrl))
            buttonImage.Source = new BitmapImage(new Uri(flow.Flow.IconUrl));
        buttonImage.Height = 45;
        buttonImage.VerticalAlignment = VerticalAlignment.Top;
        stack.Children.Add(buttonImage);

        //Button name
        TextBlock buttonName = new TextBlock();
        buttonName.Text = flow.Flow.Name;
        buttonName.FontSize = 12;
        buttonName.TextWrapping = TextWrapping.Wrap;
        stack.Children.Add(buttonName);

        button.Content = stack;
        button.Margin = new Thickness(5, 0, 5, 0);
        button.SetValue(Grid.ColumnProperty, i);
        button.SetValue(Grid.RowProperty, 0);
        button.HorizontalAlignment = HorizontalAlignment.Stretch;
        button.VerticalAlignment = VerticalAlignment.Stretch;
        button.Background = new SolidColorBrush(Color.FromArgb(130, 30, 30, 30));
        grid.Children.Add(button);

        i++;

    }
}
scroll.Content = stackPanel;
inti=0;
网格=新网格();
添加(新的RowDefinition());
对于(int-ct=0;ct<3;ct++)
grid.ColumnDefinitions.Add(newColumnDefinition());
StackPanel StackPanel=新的StackPanel();
stackPanel.Name=“stackPanel”;
stackPanel.Background=newsolidcolorbush(newwindows.UI.Color());
foreach(仪表板中的变量流。仪表板流)
{
if(flow.flow.IsInstalled)
{
//如果下一个对象应该更改行
如果(i==3)
{
stackPanel.Children.Add(网格);
网格=新网格();
添加(新的RowDefinition());
对于(int-ct=0;ct<3;ct++)
grid.ColumnDefinitions.Add(newColumnDefinition());
i=0;
}
按钮按钮=新按钮();
if(flow.flow.Type.Equals(“COMPNEWS”))
{
点击+=委托{Frame.Navigate(typeof(NewsPage),flow);};
}
//按钮堆叠面板
StackPanel stack=新的StackPanel();
//按钮图像
图像按钮图像=新图像();
如果(!String.IsNullOrEmpty(flow.flow.IconUrl)| |!String.IsNullOrWhiteSpace(flow.flow.IconUrl))
buttonImage.Source=新的位图图像(新的Uri(flow.flow.IconUrl));
按钮尺寸。高度=45;
buttonImage.VerticalAlignment=VerticalAlignment.Top;
stack.Children.Add(buttonImage);
//按钮名称
TextBlock buttonName=新建TextBlock();
buttonName.Text=flow.flow.Name;
buttonName.FontSize=12;
buttonName.TextWrapping=TextWrapping.Wrap;
stack.Children.Add(buttonName);
内容=堆栈;
边距=新厚度(5,0,5,0);
button.SetValue(Grid.ColumnProperty,i);
button.SetValue(Grid.RowProperty,0);
button.HorizontalAlignment=HorizontalAlignment.Stretch;
button.VerticalAlignment=VerticalAlignment.Stretch;
button.Background=新的SolidColorBrush(Color.FromArgb(130,30,30,30));
grid.Children.Add(按钮);
i++;
}
}
scroll.Content=stackPanel;

提前感谢。

这段代码与MVVM有什么关系?这是我为达到预期效果而编写的代码。现在,我尝试在MVVM模式中实现这一点。对不起,如果我不清楚的话。你试过使用UniversalWrapPanel吗?不知道它是否适用于UWP应用程序,但它是WSA不支持的
WrapPanel
的替代品。但是,请注意,它可能不支持虚拟化。您需要研究mvvm模式。然后,您需要了解它如何在WP8上工作。绑定、数据模板等。我了解它的基础知识。我只是在布局部分遇到了问题。谢谢。这段代码与MVVM有什么关系?这是我为达到预期效果而编写的代码。现在,我尝试在MVVM模式中实现这一点。对不起,如果我不清楚的话。你试过使用UniversalWrapPanel吗?不知道它是否适用于UWP应用程序,但它是WSA不支持的
WrapPanel
的替代品。但是,请注意,它可能不支持虚拟化。您需要研究mvvm模式。然后,您需要了解它如何在WP8上工作。绑定、数据模板等。我了解它的基础知识。我只是在布局部分遇到了问题。谢谢你。