首页 > 解决方案 > 我应该调用哪种方法来获取 c# Xamarin 表单中编辑器的标题?

问题描述

protected override void OnAppearing()
        {
            base.OnAppearing();
            var diary = new List<DiaryProperties>();
            var files = Directory.EnumerateFiles(App.FolderPath, "*.notes.txt");
            foreach(var filename in files)
            {
                diary.Add(new DiaryProperties 
                {
                    //all properties are defined in the Models folder.
                    Filename=filename,
                    //Title=how to get title ??? 
                    Text=File.ReadAllText(filename),
                    Date=File.GetCreationTime(filename)
                });
            }
            listview.ItemsSource = diary.OrderBy(d => d.Date).ToList();
        }
//xaml code
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MyDiary.AddNewNote"
             Title="Add New Note">
    <ContentPage.ToolbarItems>
        <ToolbarItem Text="+" 
                     Clicked="OnSaveClickedButton"/>
        <ToolbarItem Grid.Column="1"
                     Text="*"
                     Clicked="OnDeleteClickedButton"/>
    </ContentPage.ToolbarItems>
    <ContentPage.Content>
        <StackLayout Margin="20">
            <Editor Placeholder="Title"
                    PlaceholderColor="#000"
                    Text="{Binding Title}"
                    HeightRequest="50"/>
            <Editor Placeholder="Enter Your Note"
                    Text="{Binding Text}"
                    AutoSize="TextChanges"/>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

我已经在模型文件夹中定义了所有属性,我必须在列表视图中获取标题,如果我调用相同的函数 ReadAllText,所有内容都会显示,尽管有标题。我已经用 xaml 代码对其进行了编辑。请再次检查

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MyDiary.MyNotes"
             Title="My Notes">
    <ContentPage.ToolbarItems>
        <ToolbarItem Text="+"
                     Clicked="OnNoteAddedClicked"/>
    </ContentPage.ToolbarItems>
    <ListView x:Name="listview"
              Margin="20"
              ItemSelected="OnListViewItemSelected">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <StackLayout>
                        <Label Text="{Binding Title}"
                               FontSize="18"
                               TextColor="#f35e20" />
                        <Label Text="{Binding Text}"
                               FontSize="15"
                               TextColor="#503026" />
                        <Label Text="{Binding Date}"
                               FontSize="15"
                               TextColor="#503026" />
                    </StackLayout>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</ContentPage>

上面的代码是在列表视图中显示项目,这是应用程序的核心页面。

public AddNewNote()
        {
            InitializeComponent();
        }
        async void OnSaveClickedButton(object sender, EventArgs e)
        {
            var Diary = (DiaryProperties)BindingContext;
            if (string.IsNullOrWhiteSpace(Diary.Filename))
            {
                //save
                var filename = Path.Combine(App.FolderPath, $"{Path.GetRandomFileName()}.notes.txt");
                File.WriteAllText(filename, Diary.Text);
            }
            else
            {
                //update
                File.WriteAllText(Diary.Filename, Diary.Text);
            }

            await Navigation.PopAsync();
        }

上面的代码是为了保存新的笔记,正如你所说,请让我知道我应该怎么做才能将第一行作为标题。谢谢

标签: c#xamlxamarin.formsxamarin.androidmobile-application

解决方案


只需附加Title到数据的开头

string data = Diary.Title + Environment.Newline;
data += Diary.Text;
File.WriteAllText(filename, data);

推荐阅读