c# - 我应该调用哪种方法来获取 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();
}
上面的代码是为了保存新的笔记,正如你所说,请让我知道我应该怎么做才能将第一行作为标题。谢谢
解决方案
只需附加Title
到数据的开头
string data = Diary.Title + Environment.Newline;
data += Diary.Text;
File.WriteAllText(filename, data);
推荐阅读
- reactjs - 你应该从 redux slice reducer 中返回什么?
- ansible - 如何运行多个剧本,每个剧本都有一个调查?
- ckeditor - 如何在 CKEditor 5 中禁用 Shift+Enter
- ios - 如何调整圆圈的大小以动态调整到任何 iPhone 的宽度和高度?
- java - 如何以 3D 排列方式打印不同尺寸的矩阵?
- mongodb - 如何在 mongoDB 中通过 _id 将两个数组合并为一个并设置为特定字段?
- vector - 我正在尝试使用堆栈打印数字序列的反转。堆栈是使用 Vector 实现的。但我得到分段错误
- javascript - 为什么在 firebase.auth().createUserWithEmailAndPassword() 调用后 useRef() 挂钩设置为 null?
- mysql - Nodejs - MySQL - 如何解决此脚本中的“连接太多”错误?
- javascript - 对象可能是“未定义的”。从 typescript 3.7.2 升级到 3.8 后