重用文本视图和网格 [英] Reusing Text views and Grid

查看:99
本文介绍了重用文本视图和网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows上工作的手机8应用。



我是动态创建中的多个TextView的和网格循环。

 为(INT J = 0; J< 300; J ++)
{

图像形象=新的图像();
image.Source =新的BitmapImage(新的URI(/图片/ sample256.png,UriKind.RelativeOrAbsolute));


image.Tag = i.ToString();

电网questionGrid =新的Grid();
questionGrid.Horizo​​ntalAlignment = Horizo​​ntalAlignment.Center;
questionGrid.VerticalAlignment = VerticalAlignment.Center;

TextBlock的问题=新的TextBlock();
question.TextWrapping = TextWrapping.Wrap;
question.TextAlignment = TextAlignment.Center;
question.Text =这样的问题,它的ID是问题+我;
question.FontSize = 30;
question.Foreground =新的SolidColorBrush(Colors.Black);
question.Horizo​​ntalAlignment = Horizo​​ntalAlignment.Center;
questionGrid.Children.Add(问题);

电网answerGrid =新的Grid();
answerGrid.Horizo​​ntalAlignment = Horizo​​ntalAlignment.Center;
answerGrid.VerticalAlignment = VerticalAlignment.Center;

TextBlock的答案=新的TextBlock();
answer.TextWrapping = TextWrapping.Wrap;
answer.TextAlignment = TextAlignment.Center;
answer.Text =这就是答案,它的ID是答案+ I;
answer.FontSize = 30;
answer.Foreground =新的SolidColorBrush(Colors.Black);
answer.Horizo​​ntalAlignment = Horizo​​ntalAlignment.Center;
answerGrid.Children.Add(答案);


LayoutRoot.Children.Add(图片);
LayoutRoot.Children.Add(questionGrid);
LayoutRoot.Children.Add(answerGrid);

}



正如你可以看到我有它进入大约300倍,所以因负载很重,有很多的延迟时,页面响应用户交互。



如何降低负荷?这样我可以resue的dyamically创建的视图。


解决方案

基本上,而不是直接在页面CS代码中创建复杂的对象,你应该创建用户控件。在你的用户控制你预设为你的元素,即位置,如果你希望它有一个图像和一对夫妇的TextBlocks,你只需将他们那里。这将是一个模板。



然后,您在一个循环中创建用户控制列表。



所以,您的代码将如下:

 为(INT J = 0; J< 300; J ++)
{
SomeUserControl someUserControl =新SomeUserControl(constuctorValue1,constuctorValue2);
ListOfSomeUserControls.Add(someUserControl);
LayoutRoot.Children.Add(someUserControl);
}



在哪里ListOfSomeUserControls就在你的页面代码中定义的列表。



在这里,你可以发送一些数据构造,例如someUserControl将是你ImageUri或文本值。



或者,您可以在用户创建某些属性控制在那里设置数据。



或者,您可以创建在用户控件的一些方法,如ChangeImageUri()或者类似的东西。



要使用这个动态创建的用户控件,你应该使用ListOfSomeUserControls [索引]。你也可以保存在某个地方这个列表前使用。



这是基本的想法。






在的链接,你共享时,OP创造了的链接的将图像列表。感觉上,的区别:他在第5页图片框和300链接列表。所以,当你开始你的应用程序你有这样的:



:: Image1的listOfUris [1] IMAGE2 :: listOfUris [2]的Image3 :: listOfUris [3]图片4 :: listOfUris [4]图像5 :: listOfUris [5]



当您点击下一步按钮,您只需按1取代这个名单。



Image1的:: listOfUris [2] IMAGE2 :: listOfUris [3]的Image3 :: listOfUris [4]图片4 :: listOfUris [5]图像5 :: listOfUris [6]

这一切,只是5张图片,而不是300。


I am working on windows phone 8 app.

I am dynamically creating multiple Textview and Grid inside For loop.

for (int j = 0; j < 300; j++)
                    {

                        Image image = new Image();
                        image.Source = new BitmapImage(new Uri("/Images/sample256.png", UriKind.RelativeOrAbsolute));


                        image.Tag = i.ToString();

                        Grid questionGrid = new Grid();
                        questionGrid.HorizontalAlignment = HorizontalAlignment.Center;
                        questionGrid.VerticalAlignment = VerticalAlignment.Center;

                        TextBlock question = new TextBlock();
                        question.TextWrapping = TextWrapping.Wrap;
                        question.TextAlignment = TextAlignment.Center;
                        question.Text = " this is the question and its id is Question" + i;
                        question.FontSize = 30;
                        question.Foreground = new SolidColorBrush(Colors.Black);
                        question.HorizontalAlignment = HorizontalAlignment.Center;
                        questionGrid.Children.Add(question);

                        Grid answerGrid = new Grid();
                        answerGrid.HorizontalAlignment = HorizontalAlignment.Center;
                        answerGrid.VerticalAlignment = VerticalAlignment.Center;

                        TextBlock answer = new TextBlock();
                        answer.TextWrapping = TextWrapping.Wrap;
                        answer.TextAlignment = TextAlignment.Center;
                        answer.Text = "this is answer and its id is Answer" + i;
                        answer.FontSize = 30;
                        answer.Foreground = new SolidColorBrush(Colors.Black);
                        answer.HorizontalAlignment = HorizontalAlignment.Center;
                        answerGrid.Children.Add(answer);


                        LayoutRoot.Children.Add(image);
                        LayoutRoot.Children.Add(questionGrid);
                        LayoutRoot.Children.Add(answerGrid);

                    }

As you can see that i have around 300 times its entering, so because of that the load is heavy and there is a lot of delay when page responds to user interaction.

How to reduce the load? so that i can resue the dyamically created views.

解决方案

Basically, instead of creating complicated objects directly at page cs code you should create a user control. On you user control you preset the positions for you element, i.e. if you want it to have an Image and a couple of textblocks, you just place them there. That would be a template.

Then, you create list of user controls in a loop.

So, your code would look like:

for (int j = 0; j < 300; j++)
{
    SomeUserControl someUserControl = new SomeUserControl(constuctorValue1, constuctorValue2);
    ListOfSomeUserControls.Add(someUserControl);
    LayoutRoot.Children.Add(someUserControl);
}

Where ListOfSomeUserControls is a List defined on your page code.

Here, you can send some data to constructor, for example someUserControl will be your ImageUri or text value.

OR, you can create some properties at user control to set data there.

OR, you can create some methods at user control, like ChangeImageUri() or something like that.

To use this dynamically created user controls you should use ListOfSomeUserControls[Index]. You could also save this list somewhere for former usage.

That's the basic idea.


In the link, you've shared, the OP creates a list of links to images. Feel, the difference: he has 5 Image boxes at the page and a list of 300 links. So, when you start your app you have this:

Image1::listOfUris[1] Image2::listOfUris[2] Image3::listOfUris[3] Image4::listOfUris[4] Image5::listOfUris[5]

When you tap next button, you just displace this list by 1.

Image1::listOfUris[2] Image2::listOfUris[3] Image3::listOfUris[4] Image4::listOfUris[5] Image5::listOfUris[6]

That's all, just 5 images, not 300.

这篇关于重用文本视图和网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆