简体   繁体   中英

Why can't I see the listbox in the PNG file?

What I want
I want to display a custom live tile in WP7.5, a textblock and a listbox with 4 strings as its listitem. So I create a image with all of the stuff and display it as a background of the live tile.

What I Code

        //prepare the background
        WriteableBitmap bmp = new WriteableBitmap(173, 173);
        BitmapImage logo = new BitmapImage(new Uri("/images/tile/SecondaryTile.png", UriKind.Relative));            
        Image img = new Image { Source = logo };

        // Force the bitmapimage to load it's properties so the transform will work
        logo.CreateOptions = BitmapCreateOptions.None;

        //create a textblock
        TextBlock taskNumberShow = new TextBlock();
        taskNumberShow.Foreground = new SolidColorBrush(Colors.White);
        taskNumberShow.FontSize = 35;
        taskNumberShow.HorizontalAlignment = HorizontalAlignment.Center;
        taskNumberShow.FontFamily = (FontFamily)Application.Current.Resources["PhoneFontFamilySemiBold"];
        taskNumberShow.Text = "35";

        //define the position
        TranslateTransform tt1 = new TranslateTransform();
        tt1.X = 45;
        tt1.Y = 7;
        bmp.Render(taskNumberShow, tt1);

        //create a listbox
        ListBox listBox1 = new ListBox();
        listBox1.Height = 100;
        listBox1.Width = 173;
        listBox1.HorizontalAlignment = HorizontalAlignment.Center;
        //listBox1.VerticalAlignment = VerticalAlignment.Bottom;
        //listBox1.Margin = new Thickness(50, 150, 0, 0);

        //put a string array in the listbox, the length of this array is 4
        for (int i=0; i < TaskList.Length; i++)
        {
            //先生成一堆textBlock
            TextBlock txtBlk = new TextBlock();
            txtBlk.Name = "txtBlk" + i.ToString();
            txtBlk.Text = TaskList[i];
            txtBlk.FontSize = 17;
            txtBlk.Width = 173;
            txtBlk.TextWrapping = TextWrapping.NoWrap;
            txtBlk.Foreground = new SolidColorBrush(Colors.White);
            listBox1.Items.Add(txtBlk);
        }

        //define the position
        tt1.X = 0;
        tt1.Y = 40;
        bmp.Render(listBox1, tt1);


        //add the backgroundpic
        tt1.X = 0;
        tt1.Y = 0;
        bmp.Render(img, tt1);
        bmp.Invalidate();

        ExtendedImage extendImage = bmp.ToImage();

What is the END
I can see the textblock, but I can't see the listbox. I tried to directly assign the item of the string array(TaskList[]) to the listbox using the code below

for (int i=0, i<TaskList.Length, i++)
{
 Listbox1.Items.Add(TaskList[i]);
}

but the result is I can't see it, too.

How do I solve this problem? Why can't I see the listbox in the picture?

This might be related to the Virtual nature of the ListBox. One option is to set the ItemsPanel to be a StackPanel. Another, maybe easier way would be to just use a StackPanel

//create a StackPanel
    StackPanel stackPanel1 = new StackPanel();
    stackPanel1.Height = 100;
    stackPanel1.Width = 173;
    stackPanel1.HorizontalAlignment = HorizontalAlignment.Center;

    //put a string array in the listbox, the length of this array is 4
    for (int i=0; i < TaskList.Length; i++)
    {
        //先生成一堆textBlock
        TextBlock txtBlk = new TextBlock();
        txtBlk.Name = "txtBlk" + i.ToString();
        txtBlk.Text = TaskList[i];
        txtBlk.FontSize = 17;
        txtBlk.Width = 173;
        txtBlk.TextWrapping = TextWrapping.NoWrap;
        txtBlk.Foreground = new SolidColorBrush(Colors.White);
        stackPanel1.Children.Add.Add(txtBlk);
    }

I would also recommend putting all of your controls into a parent container like a Grid and render the Grid as one piece.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM