刚学windows phone开发,写了个记事本,有问题,求高手解答

nice_wp 2014-04-18 05:52:35
在page1页面保存文本,返回主页没有显示文本,已经用binding绑定了数据,但是就是不显示,请问怎么办,本人新手,真心搞不懂。望高手能抽空看下,谢谢。
这是主页面
<Grid x:Name="ContentPanel" Margin="0,124,10,0" Grid.RowSpan="2">
<ListBox Name="_LB_main" Margin="3.099,2.005,-4.229,437.534" RenderTransformOrigin="0.5,0.5"
UseLayoutRounding="False" d:LayoutRounding="Auto">
<ListBox.RenderTransform>
<CompositeTransform Rotation="0.232"/>
</ListBox.RenderTransform>
<ListBox.ItemTemplate >
<DataTemplate >
<Grid HorizontalAlignment="Left" Margin="12,192,0.0">
<Grid.RowDefinitions >
<RowDefinition Height="33"/>
<RowDefinition Height="67"/>
</Grid.RowDefinitions >
<HyperlinkButton Name="_hybtn_1" Content="{Binding titile_tbx1}" Height="80" Width="461" Click="_hybtn_1_Click"/>
<TextBlock Name="_tb1_1" TextWrapping="Wrap" Text="{Binding body_txb1}" Height="52" Width="461"/>
</Grid >
</DataTemplate >
</ListBox.ItemTemplate >
</ListBox >

这是page1页面
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<TextBox Name="_tbx_title" HorizontalAlignment="Left" Height="72" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="456" Margin="0,10,0,0"/>
<TextBox Name="_tbx_body" HorizontalAlignment="Left" Height="641" Margin="0,104,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="456" TextChanged="_tbx_body_TextChanged"/>

</Grid>
</Grid>
<phone:PhoneApplicationPage.ApplicationBar>
<shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
<shell:ApplicationBarIconButton IconUri="appbar.save.rest.png" Text="保存" Click="ApplicationBarIconButton_Click_1"/>
<shell:ApplicationBarIconButton IconUri="appbar.cancel.rest.png" Text="删除" Click="ApplicationBarIconButton_Click_2"/>
<shell:ApplicationBar.MenuItems>
</shell:ApplicationBar.MenuItems>
</shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>

下面是主页面 mainpage.xaml代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO;
using System.IO.IsolatedStorage;
using System.Data;
using System.Text;
using Microsoft.Phone.Shell;
using System.IO;
using System.IO.IsolatedStorage;

namespace text
{
public partial class MainPage : PhoneApplicationPage
{
public string filename;
public string[] filename_list;
public class note
{
public string titile_tbx1 { get; set; }
public string body_txb1 { get; set; }
}
public MainPage()
{
InitializeComponent();
}
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
List<note> notes = new List<note>();
using (IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication())
{
filename_list = iso.GetFileNames(); //获得根目录的所有文件名称列表
int i = filename_list.Length; //获取文件名列表长度
for (int a = 0; a < i; a++) //通过for循环 依次写入note 由a决定
{
using (IsolatedStorageFile file=IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream stream=file.OpenFile (filename_list[a],FileMode.OpenOrCreate ,FileAccess.ReadWrite ))
{
using (StreamReader reader = new StreamReader(stream))
{
notes.Add(new note { titile_tbx1 = filename_list[a], body_txb1 = reader.ReadToEnd() });
}
}
}
}

}
_LB_main.ItemsSource = notes; //

}

private void _hybtn_1_Click(object sender, RoutedEventArgs e)
{
HyperlinkButton myhybtn = e.OriginalSource as HyperlinkButton;
filename = myhybtn.Content.ToString();
this.NavigationService.Navigate(new Uri ("edit.xaml?file="+filename,UriKind.Relative ));

}
}
}

以下是page1代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.IO.IsolatedStorage;
using System.IO;
using System.Text;

namespace text
{
public partial class edit : PhoneApplicationPage
{
public string data;
public edit()
{
InitializeComponent();
}

private void _tbx_body_TextChanged(object sender, TextChangedEventArgs e)
{
using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream stream = file.OpenFile(_tbx_title.Text, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
StreamWriter writer = new StreamWriter(stream);
writer.WriteLine(_tbx_body.Text);
writer.Close();
}
}
}

private void ApplicationBarIconButton_Click_1(object sender, EventArgs e)
{
using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream stream = file.OpenFile(_tbx_title.Text, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
StreamWriter writer = new StreamWriter(stream);
writer.WriteLine(_tbx_body.Text);
writer.Close();
}
}
this.NavigationService.Navigate(new Uri("/Mainpage.xaml", UriKind.Relative));
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e); //设置一个data string 如果出现异常,就跳出代码块,直接进入catch return
try
{
data = NavigationContext.QueryString["file"];
}
catch (System.Exception ex)
{
return;
}
if (!string.IsNullOrEmpty(data))
{
using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream stream = file.OpenFile(data,FileMode.OpenOrCreate,FileAccess.Read))
{
using (StreamReader reader = new StreamReader(stream))
{
_tbx_title.Text = data;
_tbx_body.Text = reader.ReadToEnd();
}
}
}
}
}
private void ApplicationBarIconButton_Click_2(object sender, EventArgs e)
{
using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
{
file.DeleteFile(_tbx_title.Text);
this.NavigationService.Navigate(new Uri("/Mainpage.xaml", UriKind.Relative));
}
}
}
}


分不多,让各位见笑了,麻烦抽点儿时间帮小弟看下,小弟感激不尽。
...全文
268 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
vbfool 2014-04-21
  • 打赏
  • 举报
回复
没看到你代码里有任何通知的存在,这样的话,UI怎么可能会刷新呢?
starskun888 2014-04-21
  • 打赏
  • 举报
回复
建议使用ObservableCollection<T>
nice_wp 2014-04-19
  • 打赏
  • 举报
回复
就是在page1里面添加了文本然后才存入到主页面的,那个时候应该有值的
nice_wp 2014-04-19
  • 打赏
  • 举报
回复
记事本要用 datacontext属性吗,可不可心留个邮箱,我发给你替我看下
  • 打赏
  • 举报
回复
没有太细看你的代码,控件的DataContext属性有设置吗?
gnimgnot 2014-04-18
  • 打赏
  • 举报
回复
_LB_main.ItemsSource = notes; // 这一句执行之前,notes有值吗

7,655

社区成员

发帖
与我相关
我的任务
社区描述
Windows Phone是微软发布的一款手机操作系统,它将微软旗下的Xbox LIVE游戏、Zune音乐与独特的视频体验整合至手机中。
社区管理员
  • Windows客户端开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧