none
Windows 8项目,Grid App,如何将自定义文本文档TXT文件绑定到ITEM页进而变成详细内容? RRS feed

  • 问题

  • 如题。Windows 8项目,Grid App,如何将自定义文本文档TXT文件绑定到ITEM页进而变成详细内容?使用VS2012编译。现有一个文本文档TXT文件,如何绑定到Grid app的ITEM详细内容页进而变为详细内容?请指点。谢谢!!!
    2014年1月2日 8:11

答案

  • Hi,

    你所说的详细页里面应该有一个RichTextBlock控件的吧,下面有个例子是能将Txt文件绑定到RichTextBlock的,你可以参考一下,你自己在你的详细页里面作相应的修改和处理:

    XAML:

     <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
            <RichTextBlock Name="testContent"></RichTextBlock>
        </Grid>

    Code-behind:

     public sealed partial class MainPage : Page
        {
            private ObservableCollection<string> _blocks = new ObservableCollection<string>();
    
            public ObservableCollection<string> blocks
            {
                get { return _blocks; }
                set { _blocks = value; }
            }
            public MainPage()
            {
                this.InitializeComponent();
                BindRichTextBlock();
            }
    
            /// <summary>
            /// Invoked when this page is about to be displayed in a Frame.
            /// </summary>
            /// <param name="e">Event data that describes how this page was reached.  The Parameter
            /// property is typically used to configure the page.</param>
            protected override void OnNavigatedTo(NavigationEventArgs e)
            {
            }
            private async void BindRichTextBlock()
            {
                FileOpenPicker openPicker = new FileOpenPicker();
                openPicker.ViewMode = PickerViewMode.List;
                openPicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
                openPicker.FileTypeFilter.Add(".txt");
                StorageFile File = await openPicker.PickSingleFileAsync();
                using (StreamReader sr = new StreamReader(await File.OpenStreamForReadAsync()))
                {
                    string value = await sr.ReadLineAsync();
    
                    while (value != null)
                    {
                        try
                        {
                            blocks.Add(value);
                            value = await sr.ReadLineAsync();
    
                        }
                        catch (Exception e)
                        {
    
                        }
    
                    }
                }
                foreach (string item in blocks)
                {
                    Run r = new Run();
                    r.Text = item;
                    r.FontSize = 24;
                    r.FontFamily = new Windows.UI.Xaml.Media.FontFamily("Cambria");
                    Paragraph p = new Paragraph();
                    p.Inlines.Add(r);
                    testContent.Blocks.Add(p);
                }
            }
    
        }
    祝您好运!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    2014年1月3日 2:33
    版主