将CheckBox渲染为图像以进行平铺

时间:2014-09-16 20:03:28

标签: c# windows-phone-8 checkbox tile writeablebitmap

我想在Windows Phone应用中的磁贴上显示一个或多个CheckBoxes。这已经适用于TextBlocks,但是使用CheckBox它只显示CheckBox的Text而不是Checkmark本身。

这是我的代码示例:

    public void CreateTile()
    {
        StackPanel panel = new StackPanel();
        panel.VerticalAlignment = VerticalAlignment.Top;
        panel.Margin = new Thickness(7.0, 7.0, 7.0, 0);
        panel.Width = 336;
        panel.Height = 336;
        panel.Orientation = Orientation.Vertical;

        // Create and add a CheckBox for each task
        foreach (var task in _tasks)
        {
            TextBlock textBlock = new TextBlock();
            textBlock.TextWrapping = TextWrapping.Wrap;
            textBlock.Style = App.Current.Resources["PhoneTextLargeStyle"] as Style;
            textBlock.Foreground = new SolidColorBrush(Colors.White);
            textBlock.Text = task.Text;

            CheckBox checkBox = new CheckBox();
            checkBox.IsChecked = task.IsDone;
            checkBox.Content = textBlock;

            panel.Children.Add(checkBox);
        }

        Grid layoutRoot = new Grid();
        layoutRoot.Background = new SolidColorBrush(Colors.Blue);
        layoutRoot.Width = 336;
        layoutRoot.Height = 336;
        layoutRoot.Children.Add(panel);
        layoutRoot.Measure(new Size(336, 336));
        layoutRoot.Arrange(new Rect(0, 0, 336, 336));
        layoutRoot.UpdateLayout();

        // Render grid into bitmap
        WriteableBitmap bitmap = new WriteableBitmap(336, 336);
        bitmap.Render(layoutRoot, null);
        bitmap.Invalidate();

        // Save background image for tile to isolated storage
        Uri backgroundImage = TileHelper.SaveTileImage(bitmap);
    }

如果我创建一个带有上述方法创建的背景图像的图块,则图块将如下所示:

enter image description here

正如您所见,文本显示但文本前没有复选标记/方格。

2 个答案:

答案 0 :(得分:1)

你总能" hack"它通过使用复选框控件的图像。您是否尝试在UI中显示创建的控件?即将其添加到页面?只是为了看你的代码是否正确执行。

或另一种解决方案是使用复选标记字符 - http://www.fileformat.info/info/unicode/char/2713/index.htm

我会尝试在我的测试应用中复制此问题,因为奇怪的是它不起作用。

答案 1 :(得分:1)

我个人喜欢在这种情况下使用 Segoe UI符号作为Font Family。这使我可以灵活地一起使用TextSymbols,同时不会过多地使用代码/图像。我会说,SUS有很棒的现代图标(或者你可以称之为字符),这些图标非常像Metroish。

打开Charmap(Win + R并输入charmap)和字体选择 - > Segoe UI符号。现在,您可以选择任何您喜欢的字符并粘贴到Visual Studio编辑器本身。是的,它有效! 该符号可能无法在编辑器本身中正确显示,但它将在Runtime

以下是一些建议: Some CheckBoxes

以下是相应的字符:

  1. 不要担心他们不正常在这里。他们应该按照上述步骤进行操作。