【windows phone】simple note
这已经是第三天的homework了,前两天总是可以很好的完成,但是今天缺卡克了。突然间发现自己缺失的东西太多了,比如using的一些用法,以前只知道using只是一个命名空间指令,并不知道using可以强制性释放对象,获取更多的内存。还有好多.net framework中的类库都不知道。比如IsolatedStorageFile类······现在只是可以看懂别人的一些代码,如果真正的让自己去写一些应用感觉真的很困难,突然想到数据结构老师说的一句话“中国的程序员好多都是在背代码,没有自己的创造力。”现在真的体会很深了,因为我就是其中的一个。以前社团让做一些东西,那些知识都是自己从来没学过的。或许做这些东西真的可以让自己很快的学会用某些知识。但是知识会用而已,真正的明白其中的原理吗?我不懂,知识看着别人是这样写的,自己也就跟着这样做,看着功能实现了,激动了半天。但是回头想想,这真的是自己的功劳?还不是窃取被人的代码来完成自己的功能?根据项目学东西真的只是只学到只是的点,而学不到知识的面。C#学好才是王道!!!唉!发了点牢骚,还是要把代码总结一下的!
simple note
前台代码:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Button Content="edit" Height="72" HorizontalAlignment="Left" Margin="12,500,0,0" Name="editButton" VerticalAlignment="Top" Width="110" Click="editButton_Click" />
<Button Content="save" Height="72" HorizontalAlignment="Left" Margin="128,500,0,0" Name="saveButton" VerticalAlignment="Top" Width="112" Click="saveButton_Click" />
<Button Content="delete" Height="72" HorizontalAlignment="Left" Margin="246,500,0,0" Name="deleteButton" VerticalAlignment="Top" Width="128" Click="deleteButton_Click" />
<TextBox Height="348" HorizontalAlignment="Left"
Margin="6,30,0,0"
Name="EditTextBox1"
Text=""
VerticalAlignment="Top"
Width="460"
Canvas.Left="-18"
Canvas.Top="-30"
TextWrapping="Wrap">
<TextBox.InputScope>
<InputScope>
<InputScopeName NameValue="Text"/>
</InputScope>
</TextBox.InputScope>
</TextBox>
<TextBlock Height="210" Name="myTextBlock" Text="" Width="410" FontSize="40" TextWrapping="Wrap" Canvas.Left="0" Canvas.Top="0" Margin="12,30,34,368"></TextBlock>
</Grid>
后台代码:
public partial class text : PhoneApplicationPage
{
public text()
{
InitializeComponent();
}
private void editButton_Click(object sender, RoutedEventArgs e)
{
////设置myCanvas可见
//myCanvas.Visibility = System.Windows.Visibility.Visible;
////设置TextBox焦点
//EditTextBox1.Focus();
////设置myTextBlock隐藏
//myTextBlock.Visibility = System.Windows.Visibility.Collapsed;
if (myTextBlock.Visibility == System.Windows.Visibility.Visible)
{
EditTextBox1.Text = myTextBlock.Text;
myTextBlock.Visibility = System.Windows.Visibility.Collapsed;
EditTextBox1.Visibility = System.Windows.Visibility.Visible;
EditTextBox1.Focus();
}
}
private void saveButton_Click(object sender, RoutedEventArgs e)
{
////设置TextBlock可见
//myTextBlock.Visibility = System.Windows.Visibility.Visible;
////设置myCanvas隐藏
//myCanvas.Visibility = System.Windows.Visibility.Collapsed;
//myTextBlock.Text = EditTextBox1.Text.ToString();
if (EditTextBox1.Visibility == System.Windows.Visibility.Visible)
{
savaNote();
myTextBlock.Text = EditTextBox1.Text;
myTextBlock.Visibility = System.Windows.Visibility.Visible;
EditTextBox1.Visibility = System.Windows.Visibility.Collapsed;
}
}
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
loadNote();
}
private void deleteButton_Click(object sender, RoutedEventArgs e)
{
EditTextBox1.Text = "";
savaNote();
myTextBlock.Text = "";
myTextBlock.Visibility = System.Windows.Visibility.Visible;
EditTextBox1.Visibility = System.Windows.Visibility.Collapsed;
}
private void loadNote()
{
var appStorage = IsolatedStorageFile.GetUserStoreForApplication();
using (var fileStream = appStorage.OpenFile("note.txt", System.IO.FileMode.OpenOrCreate))
{
using (StreamReader sr = new StreamReader(fileStream))
{
myTextBlock.Text = sr.ReadToEnd();
}
}
}
private void savaNote()
{
var appStorage = IsolatedStorageFile.GetUserStoreForApplication();
using (var fileStream = appStorage.OpenFile("note.txt", FileMode.Create)) //FileMode如果文件不存在则创建一个文件
{
using(StreamWriter sw = new StreamWriter (fileStream))
{
sw.WriteLine(EditTextBox1.Text);
}
}
}
}
最后再鼓励自己一句吧!“李子,不要走的太匆忙,一步一个脚印的往前走,成功最怕执着了。fighting!!!”